JVM 只会获取 .jar 文件吗?

Will JVM only pick up .jar files?

当我将 -classpath 参数提供给 java 启动时,它会只选择具有 .jar 扩展名的文件,还是会尝试查看 [=11] 中的其他文件=] 路径也是如此?

意思是如果我指定 -classpath 为包含

/mypath
/mypath/IAmANormalJar.jar
/mypath/IAmAJarWithoutExtension

其中 IAmAJarWithoutExtension 是一个实际的 jar 文件,但没有 .jar 扩展名。 JVM 只会加载 /mypath/IAmANormalJar.jar 还是也会加载 /mypath/IAmAJarWithoutExtension

Java classpath 要求 jar 完成指定扩展名。如果没有扩展名,Java 会将其视为存在 class 个文件的目录,这些文件将被添加到 Java class 路径中。 Java class 路径上的维基百科文章在这方面提供了一些很好的信息:

https://en.wikipedia.org/wiki/Classpath_(Java)

它只会提取扩展名为 .class、.jar 或 .zip 的文件。当我想在两个 jar 之间切换以进行测试时,我曾经使用过的一个技巧是向我想要的文件添加另一个扩展名 "out"。如果我想测试的版本是 mylib.jar,我会把旧版本修改为 mylib.jar.old 他们可以在目录中 co-exist.

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html

Classpath entries that are neither directories nor archives (.zip or .jar files) nor * are ignored.

通配符解释:

Class path entries can contain the basename wildcard character , which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/ specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. Files will be considered regardless of whether or not they are hidden (that is, have names beginning with '.').