-
Notifications
You must be signed in to change notification settings - Fork 40.2k
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
Can not load class in classpath when package a executable jar #4375
Comments
Hi @GrapeBaBa I have had this same problem as you described, to solve it I had to package the executable JAR as ZIP type.
Cheers, |
thanks, but it still not work. only change is in manifest, main class change to propertieslauncher |
Anybody can give some advice? |
Is your project a web module? If yes, you may need to change the code to load resources using Classpath.getResourceAsStream(). Just to tell you on IDE I have got many successful cases, but when I move to create executable jars I am suffering about many issues like yours. It seems the class loader from jar is working different. |
no, it is not a web project. |
I imagine that Guava isn't supporting our nested jar format. Are you able to use Spring's |
Hi phil, |
Sorry, my mistake. I meant |
You mean like this? |
Take a look at SpringPackageScanClassResolver for an example. This was how we got Liquibase to play nicely with fat jars. Specifically: private Resource[] scan(ClassLoader loader, String packageName) throws IOException {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
loader);
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ ClassUtils.convertClassNameToResourcePath(packageName) + "/**/*.class";
Resource[] resources = resolver.getResources(pattern);
return resources;
} Which finds all classes in a given package. |
You might also want to look at |
Thanks, Phil. It works fine, why not export some public interface for this function? |
If you don't find this convenient enough, we could provide a package-name-based method variant in Spring's core |
Actually, I need the findAllClasses method in SpringPackageScanClassResolver, it is not just three lines. I feel that you could provide a method with package name arg and return a set of class. |
I'll close this one since there's not much we can do about Guava. If you're interested in getting a a convenience method in Spring please raise a JIRA. |
FWIW, I've also encountered this issue, but had success with the Reflections library. |
same happened to me. |
I tried many methods and finally succeeded. This is my experience. |
@yaohwu As long as you're using the ClassLoader that's created by Spring boot, |
@wilkinsona It looks just like this issue description. |
Correct. The system class loader cannot see classes in |
Thanks. I will try later. |
Hi,
I use Guava ClassPath class to scan and load some class in my project. It works fine when I run application in my IDE, but when I package it in a executable jar, it cannot load class. Spring boot package use a separate catelog 'lib' to setup dependency jars, is it not in classpath? How can I load these classes?
my code below:
The text was updated successfully, but these errors were encountered: