String shpath="/test/test.sh" //程序路径
Process process =null
String command1 = “chmod 777 ” + shpath
try {
Runtime.getRuntime().exec(command1 ).waitFor()
} catch (IOException e1) {
e1.printStackTrace()
}catch (InterruptedException e) {
e.printStackTrace()
}
String var="201102" /参数
String command2 = “/bin/sh ” + shpath + ” ” + var
Runtime.getRuntime().exec(command2).waitFor()
在linux 下编写shell 脚本运行 java程序和在windows的命令行运行程序是一样的。命令行运行java程序的写法:
编译:javac xxx.java
执行:java xxx
在linux下相同:
1. 先vi一个shell脚本文件: vi executejava.sh
2. 文件的内容就把刚才的两句话写进去就ok了。
3. 执行的话直接:sh excecutejava.sh
在shell里面直接调用即可。1,编译一个java文件为a.class。
2,编写shell脚本b
.sh
。
#!/bin/bash
java a.class 3,放在同一目录下运行shell即可。如果要后台运行,bash
b.sh&