I'm using IntelliJ IDEA to create a JAR. I selected "JAR from modules with dependencies" and "extract to the target JAR" for library JARs - the generated JAR looks just fine:
myJar.jar
|
+- META-INF
| +- MANIFEST.MF
+- com
| +- my
| +- package
| +- Main.class
+- some dependencies...
I checked twice: all the needed dependencies are present. The Main-Class
field in MANIFEST.MF
points to the correct main class (com.my.package.Main
in this example). I've opened the file in my archive tool and inspected it using
jar tf myJar.jar
and both show that the classes needed are available (also Main.class
). I've left the Classpath
field in IntelliJ's wizard empty, as I do not need any external libraries. However, when I issue
java -jar myJar.jar
it throws the following exception:
Error: could not find or load main class com.my.package.Main
Caused by: java.lang.ClassNotFoundException: com.my.package.Main
I've followed this guide by JetBrains
, and IntelliJ automatically added all dependencies from the Maven pom.xml
to be extracted into the JAR, which is correct.
Am I missing something I should configure here and how comes that this does not work?
(Note: Generating a JAR from that project also does not work when I choose "copy to output folder and link via manifest" for the library JARs.)
(Note 2: Using maven-assembly-plugin
is sadly not an option since I referenced other IntelliJ workspace modules which would then not get included.)
Update: That it should work as-is shows also the following phenominum: When I unzip my JAR and do
java -cp . com.my.package.Main
in the created directory, it works without any problems, which is strange given that Java refuses to load it...