Differences between quot;java -cpquot; and quot;java -jarquot;?(“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.
这篇关于“java -cp"之间的区别和“java -jar"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“java -cp"之间的区别和“java -jar"?


- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01