question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not load class in classpath when package a executable jar

See original GitHub issue

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:

   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:closed
  • Created 8 years ago
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
cbellezacommented, Nov 4, 2015

Hi @GrapeBaBa

I have had this same problem as you described, to solve it I had to package the executable JAR as ZIP type.

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Cheers,

1reaction
philwebbcommented, Nov 6, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found