且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“java -cp”之间的差异和“java -jar”?

更新时间:2023-01-14 21:02:09

我更喜欢第一个版本启动java应用程序,因为它有较少的陷阱(欢迎来到类路径地狱 )。第二个需要一个可执行的jar文件,并且必须在jar的清单中定义该应用程序的类路径(所有其他类路径声明将被静默忽略...)。因此,对于第二个版本,您必须查看jar,读取清单并尝试查明类路径条目是否有效存储jar的位置......这是可以避免的。

I prefer the first version to start a java application just because it has less pitfalls ("welcome to classpath hell"). The second one requires an executable jar file and the classpath for that application has to be defined inside the jar's manifest (all other classpath declaration will be silently ignored...). So with the second version you'd have to look into the jar, read the manifest and try to find out if the classpath entries are valid from where the jar is stored... That's avoidable.

我不认为这两个版本都有任何性能优势或劣势。它只是告诉jvm哪个类用于主线程以及它可以在哪里找到库。

I don't expect any performance advantages or disadvantages for either version. It's just telling the jvm which class to use for the main thread and where it can find the libraries.