Run java application from command line with external jar files
Solution 1
If the class is in bin directory :
java -cp xxx.jar;bin pck1.pck2.MainClass
If the class is in the current directory :
java -cp xxx.jar;. pck1.pck2.MainClass
and so on...
More info in the manual, please read it at least one time... ;-)
Solution 2
In Linux System
Compile and Run Java Program along with External JARs.
javac -cp </path/jar1>:<path/jar2>:<path/jar3> MainClass.java
If Compiler throws deprecation warning. You can recompile with -Xlint:deprecation argument.
javac -Xlint:deprecation -cp </path/jar1>:<path/jar2>:<path/jar3> MainClass.java
Finally, run the Java Program:
java -cp </path/jar1>:<path/jar2>:<path/jar3>:. MainClass
If you want to run Java Process in Background. You can use nohup.
nohup java -cp </path/jar1>:<path/jar2>:<path/jar3>:. MainClass &
Solution 3
You can use following commands to run java main class or java jar file:
java -Djava.ext.dirs=E:\lib E:\Executable\MyMainClass
java -Djava.ext.dirs=E:\lib E:\Executable\purging.jar
LifeOnCodes
Updated on September 06, 2020Comments
-
LifeOnCodes over 3 years
I have an external jar file(have package structure), which contains the main class, and I can run the app from command line like this:
java -jar example.jar
But I still have another
test.class
file outside this jar file, and some classes inside this jar file will invoke the methods intest.class
. How can I specify thetest.class
file to be used by jar file in command line? Tried many ways, always show:NoClassDefFoundError for test.class
NB: the test.class file also use class files in example.jar file, has its own package structure.
I know I can put them together in one jar file, unfortunately I need separate the test.class file.
-
LifeOnCodes over 11 yearsSorry, not in workspace, no bin directory. Assume test.class file is in the same folder with example.jar file
-
Andrew Thompson over 11 years"Assume test.class file is in the same folder with example.jar file" How about you 'pick up the ball & run with it' rather than try to get us to code this to your exact specification?
-
LifeOnCodes over 11 yearsI think I forgot to mention one important thing: the test.class also use the class files in example.jar, and has its own package structure too...and you use ; to separate class path to search the class files?
-
Qix - MONICA WAS MISTREATED over 11 yearsSeriously how hard is
java.exe -?
-
Qix - MONICA WAS MISTREATED over 11 years@Aubin I was asking LifeOnCodes lol. This question could have been summed up for him if he had just done
java.exe -?
. -
kayochin about 4 yearsIt works. I want to stress that the options
-cp xxx.jar;.
should come before the class containing the main method, otherwise the annoying "Error: Could not find or load main class" appears.