Can not load class in classpath when package a executable jar
See original GitHub issueHi, 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:
private static final ImmutableSet<Class<?>> REQUEST_CLASSES;
static {
try {
REQUEST_CLASSES = from(
from(currentThread().getContextClassLoader()).getTopLevelClassesRecursive(
OSS_REQUEST_PACKAGE)).transform(
new Function<ClassPath.ClassInfo, Class<?>>() {
@Override
public Class<?> apply(ClassPath.ClassInfo input) {
return input.load();
}
}).toSet();
}
catch (IOException e) {
LOGGER.error("Load OSS request classes exception", e);
throw new RuntimeException(e);
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:22 (7 by maintainers)
Top Results From Across the Web
Java JAR unable to load class on Class-Path? - Stack Overflow
Right click on your project -> Export... -> Runnable JAR file -> Launch configuration (choose you main class) and check Library handing: Extract ......
Read more >Here's How to Fix the "Could Not Find or Load Main Class ...
The error “Could not find or load main class” is indeed dreadful and difficult to fix but not impossible. Learn how to prevent...
Read more >Java Guide: How to Fix "Could not find or load main class"
The Java “Could not find or load main class” error is thrown when the JVM fails to find or load the main class...
Read more >Could Not Find or Load Main Class Java? Here's How to Fix it!
To run the .class file, run the Java command with the fully qualified class name and specify the local classpath. Every path is...
Read more >Adding Classes to the JAR File's Classpath
To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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,
Take a look at SpringPackageScanClassResolver for an example. This was how we got Liquibase to play nicely with fat jars.
Specifically:
Which finds all classes in a given package.