以下是用 Intellij 制作 jar 程序,并运行的方法。
【1】新建工程,保持默认选项,Next

【2】保持默认选项,Next

【3】给工程命名,设置保存位置,Finish

【4】新建工程结束,进入开发界面

【5】展开工程树 Project->untitled,在 src 文件夹图标上右键,选中 New->Package

【6】设置包名,一般是三段格式,com.your.untitled

【7】在新建的包上再右键,选中 New->Java Class

【8】设置类名,一般就是 Main,OK 后将生成一个 Main.java 文件,这就是我们的程序文件

【9】打开新建的 Main.java,添加代码
java
public class Main {
// main()
public static void main(String[] args) throws ClassNotFoundException{
String str1 = "Hello Java!";
System.out.println(str1);
}
}

【10】工具栏,File -> Project Structure

【11】在弹出的 Project Structure 对话框中,选中 Artifacts -> + -> JAR -> Empty

【12】按下图数字顺序,依次:1,设置编译输出文件名,这里仍设为 untitled,默认是 unnamed。2,点击按钮 Create Manifest...。3,在弹出的对话框中选中 src 目录。OK

【13】回到上一级对话框,继续按顺序执行:1,点击 Main Class 后面的按钮。2,在弹出的对话框中,选中 Project。3,选中 Main。4,OK。

【14】回到上一级对话框,点 + ,添加 Module Output

【15】在弹出的对话框中,选中 untitled,OK(注:这个对话框中还有一个 Class Path,本例留空,它是用来设置工程中引用到的库的路径的,本例从略)

【16】Artifacts 设置完成如下

【17】工具栏,Build -> Build Artifacts...

【18】继续选中 untitled -> Build

【19】Build 完成后,在窗口左下角有提示信息

【20】从文件管理器定位到编译输出目录:untitled -> out -> artifacts -> untitled,可以看到已经生成了【19】的编译结果 untitled.jar。然后新建一个 run.bat 文件,其内容如下:
vbnet
@echo off
cd /d %~dp0
java -classpath untitled.jar com.your.untitled.Main
pause

【21】在文件管理器的路径栏,键入 cmd 并回车,以进入命令行控制台

【22】在 cmd 中,可见已自动定位到当前路径,这时键入 run 并回车,就可以执行【19】的编译结果 untitled.jar,并输出了【9】的代码中设定的输出 Hello Java!

结束。