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 的存放路径。 |
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
Steps to set PATH variable in Ubuntu 1. Go to File System > etc 2. Open environment file with admin(root) permissions
1 | 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 ,粘贴的效果如下图所示:
在 Ubuntu 下设置 PATH 变量
1. 在控制台中进入 /etc 目录 2. 使用管理员权限打开环境文件:
1 | 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
03 | ECHO Compiling %~nx1....... |
09 | ECHO -----------OUTPUT----------- |
Copy this file to jdk bin directory. For Ubuntu Open text editor and save file as runJava.sh after pasting below code
01 | [ -f "$1.class" ] && rm $1.class |
04 | echo "Compiling $file........" |
09 | echo "-----------OUTPUT-----------" |
→ 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
03 | ECHO Compiling %~nx1....... |
09 | ECHO -----------OUTPUT----------- |
将这个文件复制到JDK的bin目录下 对于 Ubuntu 使用下面代码创建文件 runJava.sh
01 | [ -f "$1.class" ] && rm $1.class |
04 | echo "Compiling $file........" |
09 | echo "-----------OUTPUT-----------" |
→ Note: 如果你想编译所有的java文件,需要把第二行中的 $1.java 替换成 *.java |
Copy or Move this file to jdk bin directory using below command
1 | 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目录下
1 | 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
- Open Sublime package directory using Preferences > Browse Packages..
- Go to Java Folder
- 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
| Step #3 – 修改 Javac.sublime-build 按照以下的步骤修改sublime text 2的编译系统脚本。
- 在选项卡Preferences > Browse Packages.. 打开sublime的包目录
- 转到Java Folder
- 打开 JavaC.sublime-build 替换下面的命令行
"cmd": ["javac", "$file"], 在 Windows 下使用以下命令替换 "cmd": ["runJava.bat", "$file"], 在 Ubuntu 下使用以下命令替换 "cmd": ["runJava.sh", "$file_base_name"], Step #4 – 现在写个测试程序,使用CTRL+B 运行下试试吧!
可以看到控制台编译并运行了程序
|