“java -cp"之间的差异和“java -jar"? [英] Differences between "java -cp" and "java -jar"?

查看:31
本文介绍了“java -cp"之间的差异和“java -jar"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用
java -cp CLASSPATHjava -jar JAR_FILE_PATH 运行Java 应用程序有什么区别?在运行 Java 应用程序时,其中一个比另一个更受欢迎吗?我的意思是这些方法中的哪一种对 JVM 来说更昂贵(根据他们的机器资源使用情况)?

What is the difference between running a Java application with
java -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.

这篇关于“java -cp"之间的差异和“java -jar"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆