简体   繁体   中英

Create runnable jar from command line

In folder ~/code/ I have bin/ src/ lib/ A manifest.txt is created in ~/ with the content:

Main-class: test.MyMainClass
Class-Path: lib/*.jar

Then in ~ I used command

jar cfm d.jar manifest.txt code/

then I run

java -jar d.jar

it says

Exception in thread "main" java.lang.NoClassDefFoundError: test/MyMainClass Caused by: java.lang.ClassNotFoundException: test.MyMainClass

Why do you pack bin, src, and lib? You need to pack the compiled classes.

Ie first, compile your project. You'll get some classes dir. Go to that dir and pack those files with jar .

Then you need to have META-INF/MANIFEST.MF with the Main-class: ...

Check this nice article - last chapter. http://www.skylit.com/javamethods/faqs/createjar.html


If this is the first time you play with Java, it's good to learn the basics - how the files in .jar are organized etc.

But later, to be efficient when building, I'd suggest you to use Maven. http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

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