简体   繁体   English

java.class.path没有带来Manifest.mf Class-Path属性

[英]java.class.path doesn't bring Manifest.mf Class-Path property

I'm trying to get my application classpath. 我正在尝试获取我的应用程序类路径。

I have a jar (named application.jar) and it have in its Manifest.mf other jar files, like Class-Path: a.jar b.jar . 我有一个jar (名为application.jar),它在Manifest.mf中有其他jar文件,比如Class-Path: a.jar b.jar

Why when I use System.getProperty("java.class.path") my jars a.jar and b.jar are not listed? 为什么当我使用System.getProperty("java.class.path")我的jars a.jarb.jar没有列出?

It possibly has to do with the fact that the java.class.path is a system property that is set from the classpath environment variable ( $CLASSPATH or -classpath ). 它可能与java.class.path是从类路径环境变量( $CLASSPATH-classpath )设置的系统属性这一事实有关。 These are ignored with the -jar option is used. 使用-jar选项时会忽略它们。

As per the java -jar documentation, when that jar option is used to run an application, only the manifest Class-Path is considered and other settings are ignored. 根据java -jar文档,当使用该jar选项运行应用程序时,只考虑清单Class-Path并忽略其他设置。 From http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html : 来自http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html

-jar -罐

Execute a program encapsulated in a JAR file. 执行封装在JAR文件中的程序。 The first argument is the name of a JAR file instead of a startup class name. 第一个参数是JAR文件的名称而不是启动类名称。 In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. 为了使此选项起作用,JAR文件的清单必须包含Main-Class:classname形式的一行。 Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. 这里,classname标识具有public static void main(String [] args)方法的类,该方法充当应用程序的起点。 See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests. 有关使用Jar文件和Jar文件清单的信息,请参阅Jar工具参考页面和Java Tutorial的Jar跟踪。

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. 使用此选项时,JAR文件是所有用户类的源,并忽略其他用户类路径设置。

That's excactly the question I came along as well. 这也是我出现的问题。 Even if when using java -cp ..;myTest.jar test2.Needer I get only "..;myTest.jar" as a result for java.class.path property. 即使在使用java -cp ..;myTest.jar test2.Needer我只得到“..; myTest.jar”作为java.class.path属性的结果。

Note : Even when using the -cp parameter the given class-path in the MANIFEST.MF is searched! 注意 :即使使用-cp参数,也会搜索MANIFEST.MF的给定类路径! (Couldn't find this information on google and tested myself) (无法在谷歌上找到此信息并自行测试)

So i don't think that is has something to do with the -jar parameter. 所以我认为这与-jar参数无关。 In Link you can find 链接中,您可以找到

Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. 在类加载过程本身期间,在调用程序的main方法之前,而不是迟到,扩展通配符是在早期完成的。

Interestingly i found out during my tests: The classpath in MANFIFEST.MF is searched recursivly. 有趣的是,我在测试期间发现: MANFIFEST.MF中的类路径被递归搜索。 So if there is an given test.jar File in the classpath in MANIFEST.MF of myTest.jar, the class-path in the MANIFEST.MF of the test.jar will be looked up as well (when using java -cp "myTest.jar" test2.Needer ). 因此,如果myTest.jar的MANIFEST.MF中的类路径中有一个给定的test.jar文件,那么test.jar MANIFEST.MF中的类路径也会被查找(当使用java -cp "myTest.jar" test2.Needer )。

As a result, when the java.class.path property would support showing the the classpath of the MANIFEST.MF, it should also show the classpathes of all subsequently depending .jar files. 因此,当java.class.path属性支持显示MANIFEST.MF的类路径时,它还应显示所有后续依赖.jar文件的类路径。 Since the classpath is only searched until classes are found, this wouldn t refer nicely to the lazy loading mechanism. 由于仅在找到类之前才搜索类路径,因此这不会很好地引用延迟加载机制。

TL;DR : I think that this has nothing to do with the -jar Parameter ( -cp is concerned as well). TL; DR :我认为这与-jar参数无关( -cp也是如此)。 In my explanation, the support for showing the classpath from the MANIFEST.MF would only come with additional, senseless recursive search costs (because there needn't to be an actual dependency, respectivly what is used from the .jar). 在我的解释中,显示来自MANIFEST.MF的类路径的支持只会带来额外的,无意义的递归搜索成本(因为不需要实际的依赖,分别是从.jar中使用的)。 And since this sensless search would delay the program start (since the recursive search could be really deep), it is not implemented. 而且由于这种无意义的搜索会延迟程序启动(因为递归搜索可能非常深),因此没有实现。

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

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