Sublime run Java code after compiling

  • 作者:scsidisk
  • 最后编辑:2012年12月12日
  • 标签: Java, Sublime

Sublime run Java code after compiling

1. 创建执行脚本

1
$ vi /usr/lcoal/bin/javacr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
file=$1
file_dir=`dirname ${file}`
file_name=`basename ${file}`
cd ${file_dir}

if [ ! -f "${file_name%.*}.class" ]; then
        rm -f ${file_name%.*}.class
fi

echo "Compiling ${file_name} ......."
javac ${file_name%.*}.java

echo "----------- OUTPUT ----------- "
java ${file_name%.*}
1
$ chmod u+x /usr/lcoal/bin/javacr

2. sublime text 2 设置

Preferences–browse packages–打开java 文件夹–编辑Java.sublime-build

1
2
3
4
5
6
{
     "cmd": ["javacr", "$file"],
     "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
     "selector": "source.java",
     "encoding": "cp936"
}

3. 测试

1
2
$ cd ~/Desktop/
$ vi Demo.java
1
2
3
4
5
6
7
8
9
public class Demo{
    public static void main(String[] args){
        System.out.println("This is my test program.");
        int a = 10;
        int b = 20;
        int c = a + b;
        System.out.println("Result : " + c);
    }
}

使用sublime text2 打开Demo.java

按 command+b 应该看到调试结果

1
2
3
4
5
Compiling Demo.java.......
-----------OUTPUT-----------
This is my test program.
Result : 30
[Finished in 0.8s]