Run java application from command line with external jar files

廉价感情. 提交于 2019-12-05 23:33:29

问题


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 in test.class. How can I specify the test.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.


回答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... ;-)




回答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 &



回答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


来源:https://stackoverflow.com/questions/13657849/run-java-application-from-command-line-with-external-jar-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!