0

Am developing application in spring boot.I have the two packages inside src/main/java 1. com.project.test and 2. wsdl. Inside first package I have spring boot main class. inside second package I have test.wsdl file. I try to load the test.wsdl file in main class using below code

URL wsdl = MainClass.class.getClassLoader().getResource(/wsdl/test.wsdl); system.out.println("wsdl: "+wsdl);

It return null while run in Eclipse. When build application as jar and run application using java -jar app.jar. It return correct path. Why it return null in Eclipse. But when I run below code without prefix '/' like below. Its working fine in both Eclipse and JAR

URL wsdl = MainClass.class.getClassLoader().getResource(wsdl/test.wsdl); system.out.println("wsdl: "+wsdl);

But my requirement is to load resource using path /wsdl/test.wsdl

Fifi
  • 467
  • 6
  • 17
Jai
  • 352
  • 2
  • 18
  • Do you really need to use classloader to read the file or can you use @Resource annotation? Also the static resource files has to reside inside src\main\resources – CuriousMind Aug 26 '19 at 14:25
  • No, The project already developed by some other developer. I can't change the flow. Could you tell me why Eclipse return null – Jai Aug 26 '19 at 14:27

2 Answers2

1

You need to move wsdl/test.wsdl inside src/main/resources in order to load resource.

When you package you app.jar, wsdl/test.wsdl goes into root path inside jar file, so .getClassLoader().getResource("wsdl/test.wsdl") works as expected.

Valijon
  • 12,667
  • 4
  • 34
  • 67
  • But My requirement is to use it in src/main/java. But why its working fine in JAR with /wsdl/test.wsdl – Jai Aug 26 '19 at 14:25
  • Open your jar file with 7zip or other app and locate where is `/wsdl/test.wsdl`. Do the same inside target/classes (where Eclipse deploys before executing your app). You will find the difference. It's because of resource location. BTW, storing `/wsdl/test.wsdl` inside src/main/java is no sense, cuz it's used only for java code – Valijon Aug 26 '19 at 14:27
  • Yup. Is there any configuration change in Eclipse to make it work.? – Jai Aug 26 '19 at 14:30
  • 1
    If you use MAVEN project, you may use some plugin like [this](https://stackoverflow.com/questions/16372374/move-a-text-file-into-target-folder-when-compiling-a-maven-project) – Valijon Aug 26 '19 at 14:34
1

To quote relevant Java documentation (emphasis mine):

Resource Names

A common convention for the name of a resource used by a class is to use the fully qualified name of the package of the class, but convert all periods (.) to slashes (/), and add a resource name of the form name.extension. To support this, and to simplify handling the details of system classes (for which getClassLoader returns null), the class Class provides two convenience methods that call the appropriate methods in ClassLoader.

The resource name given to a Class method may have an initial starting "/" that identifies it as an "absolute" name. Resource names that do not start with a "/" are "relative".

Absolute names are stripped of their starting "/" and are passed, without any further modification, to the appropriate ClassLoader method to locate the resource. Relative names are modified according to the convention described previously and then are passed to a ClassLoader method.

Link: Accessing Resources

Community
  • 1
  • 1
M. Prokhorov
  • 3,894
  • 25
  • 39
  • Ya I understand. But in JAR why its working? it return correct path.. why its not take it as absolute when starting with "/".? – Jai Aug 27 '19 at 06:53
  • 1
    @Jai, it does, these are always absolute, It just treats the root of the jar as a root folder of sorts. – M. Prokhorov Aug 27 '19 at 10:12
  • @Jai, just to clarify, I haven't actually checked why it's not working in Eclipse (probably because there's no JAR, but that sounds like a bug in classpath definition or implementation). However, since in class path most JARs represented by their own Loader instance, and that creates a thing called ZipFS, each JAR should indeed be a root folder. – M. Prokhorov Aug 27 '19 at 10:57
  • So, Is there any way to fix classpath definition.? – Jai Aug 27 '19 at 11:01
  • @Jai, depends on what it is. If you really need it, you should investigate it, and maybe ask another question if the fix is unclear. – M. Prokhorov Aug 27 '19 at 11:03