脚本如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
set -e

fileName=$1
if [ -z "$fileName" ]; then
echo "Parameters error!"
exit
fi

if [ ! -d classes ]; then
mkdir classes
fi
className=${fileName%%.*}
javac $className.java -d ./classes/
java -classpath ./classes $className
1
2
3
4
5
6
7
public class FinalTest
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}

配合Tab键、↑应该可以高效地操作

1
2
3
4
bogon:Java fang$ ls
FinalTest.java test.sh
bogon:Java fang$ ./test.sh FinalTest.java
Hello world!