Java -cp difference with the java -jar

java -cp and -classpath as other classes classes specified path running over this, usually library, jar package and the like, need the full path to the jar package, the window semicolon ";"
Format:
java -cp;. myClass.jar packname.mainclassname    
expression support wildcards, for example:
the Java -cp; c:. \ classes01 \ myClass.jar; c: \ classes02 \ * JAR packname.mainclassname. 


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


Derived using maven package, if there is no play into dependencies pom in the file, there is no dependencies.
1. Packing designated main class, it can be directly used java -jar xxx.jar.
2. Packing main class is not specified, you 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.


Here pom-based configuration and META-INF \ MANIFEST.MF two files were tested three scenarios:
pom.xml of build configurations:

    <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.8target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                        <mainClass>test.core.Core</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef > 
                    </ descriptorRefs > 
                </ Configuration > 
                <-! Here is mvn package in order to use the command, if not using Assembly mvn -> 
                < Executions > 
                    < Execution > 
                        < ID > the make-Assemble </ ID > 
                        < Phase > Package </ Phase > 
                        < Goals > 
                            < Goal > SINGLE </ Goal > 
                        </ Goals > 
                    </ Execution >
                </executions>
            </plugin>
        </plugins>
    </build>

 

 


META-INF\MANIFEST.MF的内容:
Manifest-Version: 1.0
Main-Class: test.core.Core

 


MainClass 1.pom specified in build but META-INF \ MANIFEST.MF file is not specified in Class-Main: test.core.Core
the Java -jar the Test-JAR-with-dependencies.jar // execute successfully
java -cp test-jar -with-dependencies.jar test.core.Core // fails, there is no master list of tips jar property


2.pom in build but did not specify mainClass META-INF \ MANIFEST.MF file specified in the Class-Main: test.core.Core
the Java -jar the Test-jar-with-dependencies.jar // failed, suggesting that there is no jar property master list
java -cp test-jar-with- dependencies.jar test.core.Core // execute successfully


Specified in the build mainClass 3.pom && META-INF \ MANIFEST.MF file-Class adds Main: test.core.Core
java -cp the Test-JAR-with-dependencies.jar test.core.Core // successful implementation of
java -jar test-jar-with-dependencies.jar // execute successfully

Guess you like

Origin www.cnblogs.com/wqbin/p/11128596.html