“java -cp"之间的区别和“java -jar"?
使用 java -cp CLASSPATH
和 java -jar JAR_FILE_PATH
运行 Java 应用程序有什么区别?在运行 Java 应用程序时,其中一个是否优于另一个?我的意思是哪一种方式对 JVM 来说更昂贵(根据它们的机器资源使用情况)?
What is the difference between running a Java application withjava -cp CLASSPATH
and java -jar JAR_FILE_PATH
? Is one of them preferred to the other for running a Java application? I mean which one of these ways is more expensive for JVM (according to their machine resources usage)?
哪一个会导致 JVM 在尝试运行应用程序时产生更多线程?
Which one will cause JVM to spawn more threads while trying to run the application?
推荐答案
我更喜欢第一个版本来启动 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.
相关文章