当前位置: 首页 > news >正文

idea环境下如何打包可运行jar?

工作中有时候偶尔写一些工具类、小程序,可是java程序员制作一个可运行jar实在折腾,利用idea开发环境,可以快速打包自己的可运行jar。具体怎么操作呢?

创建一个空白的java项目并完成自己的程序开发

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
完成java代码:

/*** 测试窗口* @author binbin* @date 2023/9/27 10:29*/
public class InfoFrame extends JFrame {public InfoFrame() {setTitle("System Information");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(300, 200);//居中显示Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();setLocation((screenSize.width - getWidth())/2, (screenSize.height - getHeight())/2);//初始化菜单JMenuBar bar = new JMenuBar();JMenu menu = new JMenu("帮助");JMenuItem exitItem = new JMenuItem("退出");exitItem.addActionListener(e -> {System.exit(0);});menu.add(exitItem);bar.add(menu);setJMenuBar(bar);//初始化系统信息JTextArea infoTextArea = new JTextArea(6, 10);infoTextArea.setText(getSystemInfo());infoTextArea.setEditable(false);add(new JScrollPane(infoTextArea));}private String getSystemInfo() {StringBuffer b = new StringBuffer();b.append("系统系统:").append(System.getProperty("os.name")).append("\r\n");b.append("系统版本:").append(System.getProperty("os.version")).append("\r\n");b.append("系统架构:").append(System.getProperty("os.arch")).append("\r\n");b.append("用户名称:").append(System.getProperty("user.name")).append("\r\n");b.append("用户主目录:").append(System.getProperty("user.home")).append("\r\n");b.append("当前工作目录:").append(System.getProperty("user.dir")).append("\r\n");return b.toString();}
}
public class App
{public static void main( String[] args ){EventQueue.invokeLater(() -> {new InfoFrame().setVisible(true);});}
}

代码结构如下:
在这里插入图片描述

引入maven-assembly-plugin插件打包

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.hbin</groupId><artifactId>info</artifactId><version>1.0-SNAPSHOT</version><name>info</name><url>www.binbin.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><build><plugins><!-- 使用maven-assembly-plugin插件打包 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.2.0</version><configuration><archive><manifest><!--主类 --><mainClass>org.hbin.App</mainClass></manifest></archive><descriptorRefs><!-- 可执行jar名称结尾--><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>
</project>

在这里插入图片描述

执行maven package

执行maven package命令,target目录将生成一个以jar-with-dependencies结尾的可直接执行jar。
运行命令:

> java -jar info-1.0-SNAPSHOT-jar-with-dependencies.jar

在这里插入图片描述

文档包和源码包

<!--生成doc jar包-->
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><executions><execution><id>attach-javadocs</id><goals><goal>jar</goal></goals><!-- 不让像@Param 这种后面没写值的东西 报错。--><configuration><additionalJOption>-Xdoclint:none</additionalJOption></configuration></execution></executions>
</plugin><!--生成源码jar包-->
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions>
</plugin>

下次再有测试、运营或者其他部门的同事找你做工具,知道怎样快速制作可执行jar了吧?

http://www.lryc.cn/news/179274.html

相关文章:

  • 基于FFmpeg的Android播放器
  • osgPBR(十五)镜面IBL--查看不同级别的HDR环境贴图
  • Docker的学习记录
  • Android Jetpack组件架构:ViewModel的原理
  • 数据分析(python)学习笔记1.0
  • SW免安装的toolbox只读问题
  • nodejs在pdf中绘制表格
  • 使用不同尺寸的传感器拍照时,怎么保证拍出同样视场范围的照片?
  • 01-工具篇-windows与linux文件共享
  • 医疗实施-住院流程详解
  • 本地连接服务器 jupyter notebook
  • Android 使用Kotlin封装RecyclerView
  • WPF 实现点击按钮跳转页面功能
  • 关于http网络通信数据包封装的过程
  • 关于RabbitMQ你了解多少?
  • Vulkan-着色器及编译SPIR-V
  • 从MVC到DDD,该如何下手重构?
  • 论文阅读:基于隐马尔可夫模型的蛋白质多序列比对方法研究
  • Vim同时打开多个文件
  • SpringCloudStreamkafka接收jsonarray字符串失败
  • 面向对象特性分析大全集
  • 【数据结构】队列和栈
  • WordPress主题开发( 十)之—— 条件标签函数(上)
  • vue学习-10vue整合SpringBoot跨域请求
  • ElasticSearch - 基于 JavaRestClient 查询文档(match、精确、复合查询,以及排序、分页、高亮)
  • 简易实现通讯录(2.0)
  • Jasypt 实现自定义加解密
  • Leetcode 554. 砖墙
  • Python 内置函数详解 (3) 进制转换
  • SPSS列联表分析