翻译于 2013/01/04 23:43
3 人 顶 此译文
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.
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
Steps to set PATH variable 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.
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
对于 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
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.
Step #3 – Changes in Javac.sublime-build
To set these batch or shell scripts in Build system of sublime text 2 follow below instructions
"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
Step #3 – 修改 Javac.sublime-build
按照以下的步骤修改sublime text 2的编译系统脚本。
"cmd": ["javac", "$file"],
在 Windows 下使用以下命令替换
"cmd": ["runJava.bat", "$file"],
在 Ubuntu 下使用以下命令替换
"cmd": ["runJava.sh", "$file_base_name"],
Step #4 – 现在写个测试程序,使用CTRL+B 运行下试试吧!
可以看到控制台编译并运行了程序