Java_Java 如何执行jar包

博主之前都很少直接执行 jar 包, 最近遇到的需求需要 直接执行 jar.

通过一段时间的了解:


现在发现了3种方法:



方法一:

修改jar 中的 META-INF/MANIFEST.MF 文件,在文件中指定主类:

对于打开 jar 的操作:

Linux : 可使用vim 直接编辑jar包

Windows:  使用WinRAR打开jar包,并编辑文件


引用 http://stackoverflow.com 上面的回答:


Second, to make a jar executable... you need to jar a file called META-INF/MANIFEST.MF

the file itself should have (at least) this one liner:


Main-Class: com.mypackage.MyClass



示例: 

一个 META-INF/MANIFEST.MF 文件的内容

Manifest-Version: 1.0
Created-By: Plexus Archiver 3.4
Main-Class: test.szh.curl.TestHttpClient


运行jar包

#!/bin/bash
java -jar target/curl-0.0.1-SNAPSHOT-jar-with-dependencies.jar



-----------------------------------------


方法二:


方法一中的  -jar 选项只有在JAR文件是可执行JAR文件时才起作用,这意味着它必须具有 Main-Class 属性。 即META-INF/MANIFEST.MF 文件的必须指定Main-Class

如果它不是一个可执行jar,那么你需要用类似以下内容的程序运行程序:


运行jar包

#!/bin/bash
java -classpath target/curl-0.0.1-SNAPSHOT-jar-with-dependencies.jar test.szh.curl.TestHttpClient




----------------------------------------


方法三 :

对于项目使用了Maven管理的情况:


在 pom.xml 中像下例一样指定:

<build>
  <plugins>
	  <plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-shade-plugin</artifactId>
		<version>2.0</version>
		<executions>
		  <execution>
			<phase>package</phase>
			<goals>
			  <goal>shade</goal>
			</goals>
			<configuration>
			  <transformers>
				<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
				  <mainClass>org.lazydevs.veetle.api.VeetleAPI</mainClass>
				</transformer>
			  </transformers>
			</configuration>
		  </execution>
		</executions>
	  </plugin>
 </plugins>
</build>



下面贴出maven 下打包的脚本

#!/bin/bash
rm -rf target/*
mvn package -Dmaven.test.skip=true





附录:

贴出

java -help  (方法一与方法二其实可以在其中找到答案,第1,2行)


Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32	  use a 32-bit data model if available
    -d64	  use a 64-bit data model if available
    -server	  to select the "server" VM
                  The default VM is server,
                  because you are running on a server-class machine.


    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose:[class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.





遇到的坑:


坑一:

错误提示no main manifest attribute

错误原因:

META-INF/MANIFEST.MF 没有指定Main-Class

即没有类似于   Main-Class: com.mypackage.MyClass 这样的行

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值