简体   繁体   中英

Java run jar from command line: Error could not find or load main class com.test.Main

Windows console command:

c:\..path..>java -jar TestApp.jar

results in: Error could not find or load main class com.test.Main

I'm using IntelliJ 14 and my artifact layout looks like this:

TestApp.jar/
--client.jar <----- com.test.Main is located here

--jogl/
----gluegen.jar
----jogl-all.jar

--META-INF/
----manifest.mf

Manifest file info:

Main-Class: com.test.Main
Class-Path: client.jar;jogl/gluegen-rt.jar;jogl/jogl-all.jar;

but when I run code in IntelliJ, it is succesfull.

I am afraid you can't, because default Classloader cannot load from a jar-within-a-jar. I suppose IntelliJ will expand those jars and thus working.

See here : Reference jars inside a jar

But there is a solution. I use to create autorun-jars with dependencies using maven-assembly-plugin . If I expand this jar I found that all jar dependencies are expanded inside the jar. This way, Classloader can load all classes.

If you don't use maven, IntelliJ must have an option to export project as autorun-jar or something similar.

Edit

If maven or exporting your project as autorun-jar is not ok for you, maybe One-JAR would serve your porpouse, but I have no experience with this project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM