Java using “-cp” and “-jar” together

后端 未结 4 1707
温柔的废话
温柔的废话 2021-01-18 01:04

Folks,

Earlier, I had just one jar file and the manifest was set up such that I can simply run my program as:

 java -jar MyApp.jar

4条回答
  •  广开言路
    2021-01-18 01:21

    As per the Java cli documentation you can not combine -cp and -jar in the same command

    -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.

    In order to resolve this you will need to ensure the manifest in your application jar references both the Class that contains the main file and the classpath.

提交回复
热议问题