• java cp与java jar的区别


    java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库,jar包之类,需要全路径到jar包,window上分号“;”
    格式:
    java -cp .;myClass.jar packname.mainclassname    
    表达式支持通配符,例如:
    java -cp .;c:\classes01\myClass.jar;c:\classes02\*.jar  packname.mainclassname 


    java -jar myClass.jar
    执行该命令时,会用到目录META-INF\MANIFEST.MF文件,在该文件中,有一个叫Main-Class的参数,它说明了java -jar命令执行的类。


    用maven导出的包中,如果没有在pom文件中将依赖包打进去,是没有依赖包。
    1.打包时指定了主类,可以直接用java -jar xxx.jar。
    2.打包是没有指定主类,可以用java -cp xxx.jar 主类名称(绝对路径)。
    3.要引用其他的jar包,可以用java -classpath $CLASSPATH:xxxx.jar 主类名称(绝对路径)。其中 -classpath 指定需要引入的类。


    下面基于pom和META-INF\MANIFEST.MF两个文件的配置,进行了三种情况的测试:
    pom.xml的build配置:

    复制代码
        <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>test.core.Core</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的内容:
    Manifest-Version: 1.0
    Main-Class: test.core.Core


    1.pom中build指定mainClass 但是 META-INF\MANIFEST.MF文件中没有指定Main-Class: test.core.Core
    java -jar test-jar-with-dependencies.jar //执行成功
    java -cp test-jar-with-dependencies.jar  test.core.Core  //执行失败,提示jar中没有主清单属性


    2.pom中build没有指定mainClass 但是 META-INF\MANIFEST.MF文件中指定了Main-Class: test.core.Core
    java -jar test-jar-with-dependencies.jar //执行失败,提示jar中没有主清单属性
    java -cp test-jar-with-dependencies.jar  test.core.Core  //执行成功


    3.pom中build指定mainClass && META-INF\MANIFEST.MF文件中增加了Main-Class: test.core.Core
    java -cp test-jar-with-dependencies.jar  test.core.Core  //执行成功
    java -jar test-jar-with-dependencies.jar  //执行成功

    转自:https://www.cnblogs.com/wqbin/p/11128596.html
  • 相关阅读:
    MySQL binlog中 format_desc event格式解析
    位bit和字节Byte
    MySQL利用mysqlbinlog模拟增量恢复
    mysqldump参数 --master-data详解
    开启MySQL二进制日志
    设置花里胡哨的Xshell字体与背景颜色(超全)
    Python操作MySQL数据库
    给定一个由括号([{)]}其中之一或多个组成的字符串判断是否符合左右括号成对标准,不同括号可任意嵌套
    给定一个字符串str,将str中连续两个字符为a的字符替换为b(一个或连续超过多个字符a则不替换)
    不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串
  • 原文地址:https://www.cnblogs.com/javalinux/p/15722393.html
Copyright © 2020-2023  润新知