简体   繁体   English

使用清单文件中的 -cp 选项和类路径运行 java 程序

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

I plan to run a java class like我计划运行 java class

java -cp myjar.jar MyClass

myjar.jar will have a manifest file with classpath entry in it. myjar.jar 将有一个清单文件,其中包含类路径条目。 Is it a valid use case?它是一个有效的用例吗? I have a doubt since I have only seen usage of manifest when using java -jar myjar.jar我有一个疑问,因为我只在使用java -jar myjar.jar时看到了清单的用法

You cannot use -cp (or the CLASSPATH enviromment variable) together with -jar .您不能将-cp (或CLASSPATH环境变量)与-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.如果您需要指定类路径以使可执行 JAR 文件工作,您可以在 JAR 文件的清单中指定Class-Path属性。

On the other hand, the Class-Path attribute in a JAR file manifest is only used for classes loaded from the JAR file itself.另一方面,JAR 文件清单中的Class-Path属性用于从 JAR 文件本身加载的类。 (But it is not restricted to use with -jar option.) (但不限于与-jar选项一起使用。)

For more details, refer to the JAR file specification and How classes are found .有关更多详细信息,请参阅JAR 文件规范如何找到类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM