您的位置 首页 java

把单个java文件打成可执行jar包

把单个java文件打成可执行jar包

1 概述

使用JDK自带的jar与java将单个java文件打成可执行jar包并运行。
当然也可以使用IDE完成,使用Maven只需要一个简单的package,但是单个文件嘛,没必要这么“凶狠”。

2 新建测试文件

著名的Hello World:

 public class Main
{
public static void main(String [] args)
{
System.out.println("Hello world.");
}
}  

3 编译

生成jar包需要class文件,换句话说需要先编译,建议先创建一个临时文件夹存放class文件。

 mkdir test && mv Main.java test && cd test;
javac Main.java  

4 打包

 jar --create --verbose --file Main.jar --main-class Main *.class  

说明一下各个参数:

  • –create:创建jar
  • –verbose:打包时产生输出
  • –file:打包的jar文件名
  • –main-class:指定入口类
  • *.class:打包所有的class文件,这里可以接受的参数可以是*,表示打包该目录下所有文件,也可以是目录名,打包指定目录下的所有文件

这里使用默认的包,若是自定义包,使用

 --main-class com.xxx.xxx.Main  

即可。

注意打包时网上有的教程使用简写:

 jar -cvf Main.jar *.class  

这样确实是能打包,但是直接运行的时候会提示:

 no main manifest attribute, in Main.jar  

可以加上–main-class参数或者直接更新打包后里面的MANIFEST.mf文件,加上:

 Main-Class: Main  

当然还是建议使用上述的方法一步打包到位。

5 运行

 java -jar Main.jar  

小编整理了一份Java基础视频、技术文档、电子书、面试题、简历模板等福利分享给大家。

需要的可以按照以下方式免费领取

1、关注我,转发+评论

2、私信发送:【学习】,即可领取!

文章来源:智云一二三科技

文章标题:把单个java文件打成可执行jar包

文章地址:https://www.zhihuclub.com/180181.shtml

关于作者: 智云科技

热门文章

评论已关闭

4条评论

  1. 2 Therefore, administration with inhibitors or inducers of these enzymes, such as capecitabine and tamoxifen, significantly affects exposure to the active drug It s important to note that the BMI metric is thought to be flawed

  2. 9 ОјM and 42 fold 5 Quality of life in adolescent and young adult cancer patients A systematic review of the literature

  3. To celebrate, we ve got some recommended banned books, a study on book censorship and a list of most frequently challenged books Diuretics, intravenous fluids, colloids, anti inflammatories and antibiotics are all common treatments for dogs suffering from noncardiogenic pulmonary edema

网站地图