java -cp 与 java -jar
java -cp(或java -classpath)
Designated to perform operational dependency jar and master classes require absolute path. When specifying a plurality jar, linux use of ":", windows ";" separated. You can also use the full name -classpath. Support wildcard "*." java -cp classpath, behind -cp classpath, the interpreter will find the path to a specified class file.
Format java -cp;. MyClass.jar packagename.mainclassname
e.g.
java -cp .;c:/classes/myClass.jar;d:/classes/*.jar packagename.mainclassname
java -cp D:\spark\spark-2.3.3\assembly\target\scala-2.11\jars\* org.apache.spark.deploy.master.Master
jar on the classpath may use wildcards.
packagename.mainclassname fully qualified class name mian method comprising, if there are a plurality of classes having the classpath main method by -cp inlet may conveniently selected program.
Use -jar option, java.exe will ignore -cp, -classpath and the parameters of the environment variable CLASSPATH.
java -jar
Designated to run jar, will be used MANIFEST.MF file in the META-INF generated when packaged, Main-Class which specifies the main class. You can not specify other dependent jar. ava -jar myClass.jar, when the command is executed, will use the directory META-INF \ MANIFEST.MF file in the file, there is a parameter called Main-Class, which describes the class java -jar command execution.
Java -jar myClass.jar format
e.g.
java -jar myClass.jar
java -jar E:\workspace\idmapping\target\idmapping-1.0-SNAPSHOT.jar
Specifies the main-class of two ways
- When packaged Maven main-class specified in the build pom.xml
- Specified in the META-INF \ MANIFEST.MF file
- pom.xml of the build configuration
<build>
<!--<finalName>test-1.0-SNAPSHOT</finalName>-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>packagename.mainclassname</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<!--下面是为了使用 mvn package命令,如果不加则使用mvn assembly-->
<executions>
<execution>
<id>make-assemble</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- META-INF \ MANIFEST.MF file specified:
Manifest-Version: 1.0
Main-Class: packagename.mainclassname
Derived using maven package, if there is no play into dependencies pom in the file, there is no dependencies.
1. Packing designated main class, can be directly used java -jar xxx.jar.
2. Packing main class is not specified, can java -cp xxx.jar main class name (absolute path).
3. To refer to other jar package, you can use java -classpath $ CLASSPATH: xxxx.jar main class name (absolute path). Wherein -classpath specified class need to be introduced.
reference