Java同时使用 "-cp"和 "-jar"

标签 java

之前,我只有一个 jar 文件, list 的设置使得我可以简单地运行我的程序:

 java -jar MyApp.jar

现在,我将我的功能分成两个 jar 文件 - MyCore.jar 和 MyApp.jar。

以下命令有效:

java -cp "*" com.mycompany.mypackage.App

但是我不能让下面的工作正常

java -cp "*" -jar MyApp.jar

我得到一个 ClassNotFoundException。

我更喜欢使用“-jar”开关。有没有办法让它发挥作用?

最佳答案

根据 Java cli documentation你不能在同一个命令中结合使用 -cp 和 -jar

-jar Execute a program encapsulated in a JAR archive. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class:classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file mani- fests. When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. Note that JAR files that can be run with the "java -jar" option can have their execute permissions set so they can be run without using "java -jar". Refer to Java Archive (JAR) Files.

为了解决这个问题,您需要确保应用程序 jar 中的 list 引用包含主文件和类路径的类。

关于Java同时使用 "-cp"和 "-jar",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12047758/

相关文章:

java - 无法在 IntelliJ 中编译

java - 下载适用于 Mac OS X 的 Java 32 位

java - 奇怪的 Java 错误放置

java改变变量参数值

java - 通用多维数组转换函数

java - 在 JNI 代码中抛出异常的最佳方法?

java - 如何编写扩展数组容量的通用方法?

java - REST Controller 是多线程的吗?

java - 如何使用 JOption.pane 在我的程序中输入 If 语句?

java - 用 Java 或 Android 编写小数