0

I have a project (Java application with a main) that runs fine from within Eclipse but when I export it to a (packed) runnable Jar and try to launch it from the command line with java -jar I get the following error message:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.NoClassDefFoundError: com/github/javafaker/Faker
    at jultävlingar2020.Jultävlingar2020.useChrome(Jultävlingar2020.java:154)
    at jultävlingar2020.Jultävlingar2020.main(Jultävlingar2020.java:57)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: com.github.javafaker.Faker
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 7 more

There seems to be a problem with the inclusion of javafaker but using it works fine within Eclipse.

The project is Gradle-based and in my Project and External dependencies-folder I have javafaker-1.0.2.jar.

My build.gradle looks like this:

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:28.2-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
    
    // Selenium
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
    
    // Javafaker
    implementation 'com.github.javafaker:javafaker:1.0.2'
}

How do I fix this?

  • If there are modules (which requires Java 9 or higher), exporting to a runnable JAR will not work because each module must be in its own JAR. – howlger Dec 12 '20 at 15:37
  • Also add gradle task to refresh dependencies, see https://stackoverflow.com/questions/13565082/how-can-i-force-gradle-to-redownload-dependencies – pburgr Dec 13 '20 at 14:44
  • @howlger I don't know what modules are (except that you get that option when you create a new project in Eclipse and checking it breaks everything…) so I don't think I have used that feature. –  Dec 13 '20 at 22:15
  • @pburgr Will try that, thank you. –  Dec 13 '20 at 22:16

0 Answers0