简体   繁体   中英

run a java program using -cp option and classpath in manifest file

I plan to run a java class like

java -cp myjar.jar MyClass

myjar.jar will have a manifest file with classpath entry in it. Is it a valid use case? I have a doubt since I have only seen usage of manifest when using java -jar myjar.jar

You cannot use -cp (or the CLASSPATH enviromment variable) together with -jar . If you need to specify a classpath to make an executable JAR file work, you can specify a Class-Path attribute in the JAR file's manifest.

On the other hand, the Class-Path attribute in a JAR file manifest is only used for classes loaded from the JAR file itself. (But it is not restricted to use with -jar option.)

For more details, refer to the JAR file specification and How classes are found .

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