3

I have a set of codes which are common for two different products, but a part of the code is behaving differently for each product

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

For One set up it is giving absolute path and for the other its giving relative path and this is leading to problems, my requirement is to get absolute path for the issue.

Packaging: enter image description here

Code flow:

The call is made from the Main Class File and from there CommonCode in common code it is looking for the WSDL located in LandScapeService.jar

Update:

The next line of the code is

IntegrationLandscapeService  landscapeService = new IntegrationLandscapeService(wsdlURL);

but I get the below 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

User27854
  • 824
  • 1
  • 16
  • 40

3 Answers3

2

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

In any case, there is not much you can do about it since loading resources from embedded jars is not supported natively by Java. The implementation providing the "trickery" I mentioned above will need to fix that (and some do it better than others, see the link above).

The other product with a Par-file indicates the use of OSGi which only requires proper configuration to keep resource-loading working. There is an answer here that explains your situation and solution options.

(*) Spring-boot also has some good documentation and a solution, but I have not tried using the solution with a non-Spring application. See https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html for more information.

vanOekel
  • 6,358
  • 1
  • 21
  • 56
  • I have gone through the links that you have given, As class loading is new to me, I am struggling with the implementation part. Will get back to you on that. Soon Thanks :) – User27854 Jul 14 '17 at 08:05
0

You can use getAbsolutePath

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

Isn't that what you are looking for?

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
0

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

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

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

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

Refer this answer to understand more Stack Overflow comment

Sanjay S
  • 9
  • 3
  • It failed with: Got \tracker.jar (The system cannot find the file specified) while opening stream from jar:file:/tracker.jar!/lib/IntegrationLandscapeService.jar!//IntegrationLandscapeServiceWS.wsdl.] I have even attached the screen shot of jar. – User27854 Jul 11 '17 at 06:00