Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested URLs return null from classLoader.getResource("") causing ClassPathResource failures #38524

Closed
n12c opened this issue Nov 23, 2023 · 1 comment
Labels
type: regression A regression from a previous release
Milestone

Comments

@n12c
Copy link

n12c commented Nov 23, 2023

My application has no default configuration file, only profile specific ones (application-profile1.properties, application-profile2.properties, etc.). The user must activate a specific profile to successfully run the application.

To improve usability, I check whether a profile has been activated (using spring.profiles.active or SPRING_PROFILES_ACTIVE) in my main method (before SpringApplication#run). If not, I display an error message and exit.

To improve usability even further, the error message attempts to list the available profiles from which the user must choose. It does so by passing "classpath:application-*.properties" to org.springframework.core.io.support.PathMatchingResourcePatternResolver#getResources to enumerate the available configuration files.

In 3.1 and 3.2 with loaderImplementation=CLASSIC this works but in 3.2 without loaderImplementation=CLASSIC it fails with:

Exception in thread "main" java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:91)
	at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:53)
	at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:58)
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
	at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:230)
	at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:574)
	at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:349)
	at com.example.MyApplication.getAvailableProfiles(MyApplication.java:138)
	at com.example.MyApplication.validateProfile(MyApplication.java:121)
	at com.example.MyApplication.main(MyApplication.java:112)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	... 4 more

Not sure if this is a regression or intended (and if so how best to achieve my end with the new implementation).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 23, 2023
@philwebb philwebb added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 23, 2023
@philwebb philwebb added this to the 3.2.x milestone Nov 23, 2023
@philwebb philwebb changed the title Unexpected behaviour with the new nested jar implementation Nested URLs return null from classLoader.getResource("") causing ClassPathResource failures Nov 23, 2023
@philwebb
Copy link
Member

The following code is enough to trigger the problem. The result should not be null.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
System.out.println(classLoader.getResource(""));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

3 participants