在 Sublime Text 2 中编译和运行 Java 程序

在 Sublime Text 2 中编译和运行 Java 程序

英文原文:Compile and Run Java programs with Sublime Text 2

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

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

01 @ECHO OFF
02 cd %~dp1
03 ECHO Compiling %~nx1.......
04 IF EXIST %~n1.class (
05 DEL %~n1.class
06 )
07 javac %~nx1
08 IF EXIST %~n1.class (
09 ECHO -----------OUTPUT-----------
10 java %~n1
11 )

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
02 for file in $1.java
03 do
04 echo "Compiling $file........"
05 javac $file
06 done
07 if [ -f "$1.class" ]
08 then
09 echo "-----------OUTPUT-----------"
10 java $1
11 else
12 echo " "
13 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 

01 @ECHO OFF
02 cd %~dp1
03 ECHO Compiling %~nx1.......
04 IF EXIST %~n1.class (
05 DEL %~n1.class
06 )
07 javac %~nx1
08 IF EXIST %~n1.class (
09 ECHO -----------OUTPUT-----------
10 java %~n1
11 )

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

对于 Ubuntu

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

01 [ -f "$1.class" ] && rm $1.class
02 for file in $1.java
03 do
04 echo "Compiling $file........"
05 javac $file
06 done
07 if [ -f "$1.class" ]
08 then
09 echo "-----------OUTPUT-----------"
10 java $1
11 else
12 echo " "
13 fi

→ 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

  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

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

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

在 Windows 下使用以下命令替换

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

在 Ubuntu 下使用以下命令替换

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

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


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

原文地址:http://www.oschina.net/translate/compile-and-run-java-programs-in-sublime-text-2?cmp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值