The same code behaves differently on two different packages

I have a set of codes that are common to two different products, but some of the code behaves differently for each product

URL wsdlURL = IntegrationLandscapeService.class.getClassLoader().getResource("IntegrationLandscapeService.wsdl");

      

For one setting, it gives an absolute path, and for another, a relative path, and this leads to problems. My requirement is to get the absolute path for the problem.

Packaging: enter image description here

Code flow:

The call is made from the main class file, and from there CommonCode looks in the general code for the WSDL located in LandScapeService.jar

Update:

Next line of code

IntegrationLandscapeService  landscapeService = new IntegrationLandscapeService(wsdlURL);

      

but i am getting the following error

failed [Failed to access the WSDL at: jar:file:/

tracker.jar!/lib/t24-IF_IntegrationLandscapeService-IntegrationLandscapeService-jwc.jar!/IntegrationLandscapeService.wsdl

.It failed with:
        \tracker.jar (The system cannot find the file specified).]

      

Screen Shot of Jar

enter image description here

+3


source to share


3 answers


The error shows two '!' in a path indicating that the resource is in an embedded / nested / inner jar file. A product that uses the fat / bundled-jar method (where one jar file contains other jar files) will require some trickery to load classes and resources from the bundled jar files. You can read about it at http://www.jdotsoft.com/JarClassLoader.php (*)

You cannot do this anyway, as loading resources from inline banners is not supported by Java. The "cheat" implementation I mentioned above will have to fix this (and some do it better than others, see the link above).



Another product with Par-file indicates the use of OSGi, which only needs to be configured correctly to support loading resources. There is an answer here that explains your situation and your solution options.

(*) Spring-boot also has good documentation and a solution, but I haven't tried this solution with a Spring app. See https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html for details .

+2


source


You can use getAbsolutePath

File file = new File(url.getPath());
String path = file.getAbsolutePath();

      



Isn't that what you are looking for?

0


source


This is because the files inside the JAR are not treated as regular files, as they are not expanded and are not directly accessible to the file explorer.

In a Jar, if files are uploaded, you will get the path from the root of the JAR file.

Please make sure you have a classpath entry for the Jar location. This will help you find the resource. Otherwise try the following code (not sure if it will work in your case, try it).

String resource = "/com/example/IntegrationLandscapeService.wsdl"; //package structure of the file
URL res = IntegrationLandscapeService.class.getResource(resource);

      

Post this answer for more Stack Overflow comment

0


source







All Articles