Java打包設定與運行命令

來源:互聯網
上載者:User

標籤:manifest   jar   lan   main   情況下   app   arch   str   com   

一、Java運行命令

java程式的啟動方式有三種:

1、java -jar

  產生的jar包中,manifest檔案定義了Main Class,可使用該命令

java -jar test.jar

2、java -cp

  產生的jar包中,無Main Class配置,可使用該命令

java -cp test.jar cn.matt.Test

  該命令也可從class檔案中啟動java程式,對於依賴的jar包,須明確列出,並用分號(windows)或冒號(Linux)分隔

java -cp ./target/classes;test2.jar cn.matt.Test

3、java -Djava.ext.dirs

  當jar中不包含依賴包時,使用java -cp並不方便,此時可使用該命令,只需指定依賴包目錄

java -Djava.ext.dirs=. cn.matt.maven_test.App

 

二、Java打包設定

使用Maven打包,預設情況下,產生的jar包不能直接運行(無Main Class),並且不包含依賴jar包

指定Main Class,並包含依賴的jar包配置:

<build>     <plugins>         <plugin>             <artifactId>maven-assembly-plugin</artifactId>             <configuration>                 <appendAssemblyId>false</appendAssemblyId>                 <descriptorRefs>                     <descriptorRef>jar-with-dependencies</descriptorRef>                 </descriptorRefs>                 <archive>                     <manifest>                         <mainClass>cn.matt.maven_test3.App</mainClass>                     </manifest>                 </archive>             </configuration>             <executions>                 <execution>                     <id>make-assembly</id>                     <phase>package</phase>                     <goals>                         <goal>assembly</goal>                     </goals>                 </execution>             </executions>         </plugin>     </plugins> </build>

 

參考:

Maven打包含有Main方法jar並運行

java命令列運行jar裡的main類

 

Java打包設定與運行命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.