加载中

Sublime Text 2 is my Favorite text editor for coding. You also like after using this awesome and light weight editor. In this article we are discussing about Compile and run java programs with Sublime Text 2.

Sublime Text 2 是我最喜欢用来编码的文本编辑器,如果你尝试使用后相信你也会喜欢上它的。在这篇文章中我们将讨论如何在 Sublime Text 2 中编译和运行 Java 程序。

Step #1 – Set Java PATH variable
This is the first and basic step to compile and run java programs because it helps to find required files when you run or compile program like java, javac, etc

第一步:设置 Java PATH 变量

这是第一步也是最基本的一步,用来设置编译和运行 Java 程序基本命令如 javac 和 java 的存放路径。

Steps to set PATH variable in Windows.

  1. Right click on My Computer
  2. Select Properties
  3. Go to Advanced system settings
  4. Find button Environment Variables… and click
  5. Go to System variables and find Path variable
  6. Paste jdk bin directory path in value filed in last

在 Windows 下设置 PATH 路径的步骤:

  1. 右键“我的电脑”
  2. 选择“属性”
  3. 进入高级系统设置
  4. 找到并点击“环境变量”按钮
  5. 进入系统变量并找到 PATH 变量
  6. 粘贴 JDK 的 bin 目录到 PATH 变量值的最后


for example if your jdk bin path is C:\Program Files\Java\jdk1.6.0_17\bin paste it like in below image.

Set java path in windows

Set java path in windows

Steps to set PATH variable in Ubuntu
1. Go to File System > etc
2. Open environment file with admin(root) permissions

Sudo gedit /etc/environment

3. Paste jdk bin directory path in last before ” and save file;
4. Log off system to take effect changes.

例如你的 JDK 的 bin 路径是 C:\Program Files\Java\jdk1.6.0_17\bin ,粘贴的效果如下图所示:

Set java path in windows

在 Ubuntu 下设置 PATH 变量

1. 在控制台中进入 /etc 目录
2. 使用管理员权限打开环境文件:

Sudo gedit /etc/environment

3. 粘贴 jdk 的 bin 目录到 path 变量的最后并保存文件退出编辑器
4. 注销并重新登录

Step #2. Create Batch or Bash Shell script file
To Compile and run java program create a batch or shell file

For Windows
Open text editor and save file as runJava.bat after pasting below code

@ECHO OFF
cd %~dp1
ECHO Compiling %~nx1.......
IF EXIST %~n1.class (
DEL %~n1.class
)
javac %~nx1
IF EXIST %~n1.class (
ECHO -----------OUTPUT-----------
java %~n1
)

Copy this file to jdk bin directory.

For Ubuntu
Open text editor and save file as runJava.sh after pasting below code

[ -f "$1.class" ] && rm $1.class
for file in $1.java
do
echo "Compiling $file........"
javac $file
done
if [ -f "$1.class" ]
then
echo "-----------OUTPUT-----------"
java $1
else
echo " "
fi

Note: If you want to Compile all programs in directory replace $1.java with *.java in second line

Step #2. 创建批处理或者Shell脚本
要想编译运行Java程序,需要创建一个批处理或者shell脚本

对于 Windows

使用下面代码创建文件runJava.bat 

@ECHO OFF
cd %~dp1
ECHO Compiling %~nx1.......
IF EXIST %~n1.class (
DEL %~n1.class
)
javac %~nx1
IF EXIST %~n1.class (
ECHO -----------OUTPUT-----------
java %~n1
)

将这个文件复制到JDK的bin目录下

对于 Ubuntu

使用下面代码创建文件 runJava.sh 

[ -f "$1.class" ] && rm $1.class
for file in $1.java
do
echo "Compiling $file........"
javac $file
done
if [ -f "$1.class" ]
then
echo "-----------OUTPUT-----------"
java $1
else
echo " "
fi

Note: 如果你想编译所有的java文件,需要把第二行中的 $1.java 替换成 *.java 


Copy or Move this file to jdk bin directory using below command
Sudo mv runJava.sh /usr/lib/jvm/jdk1.6.0_17/bin

Above command used as Sudo mv SourcePath DestinationPath
After Moving file to jdk bin set its permission 755 by right clicking on file and select permission tab and check bottom check box to make it auto executable file.

使用下面的命令将这个脚本文件移动到jdk的bin目录下
Sudo mv runJava.sh /usr/lib/jvm/jdk1.6.0_17/bin

在移动完之后要设置文件的可执行权限为755,以确保此文件是可执行的,在ubuntu下可以通过右键属性,在权限的tab上勾选可执行选项。

Step #3 – Changes in Javac.sublime-build
To set these batch or shell scripts in Build system of sublime text 2 follow below instructions

  1. Open Sublime package directory using Preferences > Browse Packages..
  2. Go to Java Folder
  3. Open JavaC.sublime-build and replace line
"cmd": ["javac", "$file"],

in Windows with

"cmd": ["runJava.bat", "$file"],

in Ubuntu with

"cmd": ["runJava.sh", "$file_base_name"],

Step #4 – Now write programs and Run usingCTRL+B

Build and Run Java Programs

Build and Run Java Programs

Step #3 – 修改 Javac.sublime-build
按照以下的步骤修改sublime text 2的编译系统脚本。

  1. 在选项卡Preferences > Browse Packages.. 打开sublime的包目录
  2. 转到Java Folder
  3. 打开 JavaC.sublime-build 替换下面的命令行
  4. "cmd": ["javac", "$file"],

在 Windows 下使用以下命令替换

"cmd": ["runJava.bat", "$file"],

在 Ubuntu 下使用以下命令替换

"cmd": ["runJava.sh", "$file_base_name"],

Step #4 – 现在写个测试程序,使用CTRL+B 运行下试试吧!

Build and Run Java Programs

可以看到控制台编译并运行了程序

返回顶部
顶部