简体   繁体   中英

create executable jar from command line with external jars

I have the command below, it worked perfectly, test2 is the main class:

java -cp "client-combined-3.141.59.jar:client-combined-3.141.59-sources.jar:libs/byte-buddy-1.8.15.jar:libs/guava-25.0-jre.jar:libs/okio-1.14.0.jar:libs/commons-exec-1.3.jar:libs/okhttp-3.11.0.jar:." test2

Then I created a jar as such:

me@me:/javaProj$ jar cvfm test2.jar ./MAINFEST.MF -C ./ .
added manifest
adding: MAINFEST.MF(in = 241) (out= 156)(deflated 35%)
adding: client-combined-3.141.59-sources.jar(in = 529743) (out= 477209)(deflated 9%)
adding: LICENSE(in = 11365) (out= 3961)(deflated 65%)
adding: test2.class(in = 1575) (out= 934)(deflated 40%)
adding: libs/(in = 0) (out= 0)(stored 0%)
adding: libs/okio-1.14.0.jar(in = 85756) (out= 82065)(deflated 4%)
adding: libs/guava-25.0-jre.jar(in = 2738171) (out= 2438570)(deflated 10%)
adding: libs/okhttp-3.11.0.jar(in = 413639) (out= 391155)(deflated 5%)
adding: libs/byte-buddy-1.8.15.jar(in = 2987269) (out= 2613396)(deflated 12%)
adding: libs/commons-exec-1.3.jar(in = 54423) (out= 47600)(deflated 12%)
adding: CHANGELOG(in = 121465) (out= 43027)(deflated 64%)
adding: test2.java(in = 1325) (out= 528)(deflated 60%)
adding: client-combined-3.141.59.jar(in = 1527879) (out= 1438576)(deflated 5%)
adding: NOTICE(in = 89) (out= 78)(deflated 12%)
adding: geckodriver(in = 7008696) (out= 2390356)(deflated 65%)

MANIFEST.MF: me@me:/javaProj$ cat MAINFEST.MF

Manifest-Version: 1.0
Class-Path: client-combined-3.141.59.jar:client-combined-3.141.59-sources.jar:libs/byte-buddy-1.8.15.jar:libs/guava-25.0-jre.jar:libs/okio-1.14.0.jar:libs/commons-exec-1.3.jar:libs/okhttp-3.11.0.jar:.
Main-Class: test2

Then when I run:

me@me:/javaProj$ java -jar test2.jar
Error: Could not find or load main class test2

Can anyone help? My purpose is to ship a single jar executable. Thanks

Provide main class along with its package name. Example:

Main-Class: YourPackage.YourMainClass

For more info: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Hope it helps.

I changed the java source file with a package test defined/recompiled, then changed the manifest to:

Manifest-Version: 1.0
Class-Path: client-combined-3.141.59.jar client-combined-3.141.59-sources.jar libs/byte-buddy-1.8.15.jar libs/guava-25.0-jre.jar libs/okio-1.14.0.jar libs/commons-exec-1.3.jar libs/okhttp-3.11.0.jar .
Main-Class: test.test2

The : was replaced with a space, then a CR was entered at the end of the last line. The command is:

jar cvfm test2.jar ./MAINFEST.MF

This is now working. Thanks to all reponses

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