How to create a jar package in IDEA

How to build jar package with IntelliJ IDEA

1. Jar package without maven

1. After writing the Java project, click File --> Project Structure, and then follow the steps shown below to perform the packaging operation.

image-20230829092612128

image-20230829092726422

image-20230829092746132

image-20230829092845835

If there are some additional files in the project, you can add the files to the jar package in the following ways.

image-20230829093345476

//如果我们将项目打成jar包之后,a.log文件不识别,此时我们需要使用类加载器读取jar包中的文件  要求文件必须在一个resources格式的目录下
//这行代码只能在jar包中使用,如果项目没有打jar包这行代码无法识别a.log文件
InputStream inputStream = DataGenerator.class.getClassLoader().getResourceAsStream("a.log");
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

image-20230829092939055

image-20230829093003108

At this point, the project packaging is completed, and then go to the console server to enter the commandjava -jar data-generator.jar

You can run the jar package.

2. Maven creates jar package

Click Maven on the far right side of the interface, then click Lifecycle, click package until the console outputs BUILD SUCCESS, and the package is successful!

image-20230829101352842

Guess you like

Origin blog.csdn.net/weixin_57367513/article/details/132558195
Recommended