How to run a java class with a jar in the classpath?

喜欢而已 提交于 2019-12-17 07:30:56

问题


So, I can do this very well:

java mypackage.MyClass

if ./mypackage/MyClass.class exists. I can also happily do this:

java -cp myjar.jar mypackage.MyClass

if the class file exists in the appropriate part of the jar. Easy stuff. But I can't for the life of me manage to do something like this:

java -cp utilities.jar mypackage.MyClass

where ./mypackage/MyClass.class exists, and where ./utilities.jar exists (not containing MyClass, of course).

Am I about to feel stupid?


回答1:


Possibly :)

# On Unix
java -cp utilities.jar:. mypackage.MyClass

# On Windows
java -cp utilities.jar;. mypackage.MyClass

Basically that's just including . (the current directory) on the classpath as well as the jar file.




回答2:


Try this if you're on Windows:

java -cp .;utilities.jar mypackage.MyClass

Or this if you're on Linux:

java -cp .:utilities.jar mypackage.MyClass

The current directory is not in the CLASSPATH by default when you specify a value for -cp.




回答3:


You should include the mypackage.MyClass into the CLASSPATH or the -cp parameter. For example:

java -cp utilities.jar;myjar.jar mypackage.MyClass

The path separator is ; on windows and : on Unix/Linux



来源:https://stackoverflow.com/questions/6409510/how-to-run-a-java-class-with-a-jar-in-the-classpath

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