0 注意
- java的runtime不會加載系統(tǒng)的環(huán)境變量簸淀,需要自己手動設(shè)置,在.bat文件中
set path=C:\Program Files\R\R-3.4.2\bin
- java調(diào)用.bat時委可,當前路徑是jar包上級目錄或者是項目的根目錄
1 出現(xiàn)cmd窗口
public static void runbat(String batPath) {
String cmd = "cmd /c start "+ batPath ;// pass
try {
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("child thread donn");
}
2 不出現(xiàn)cmd窗口
public static void runBatWithoutCmd(String batPath) {
try {
Process ps = Runtime.getRuntime().exec(batPath);
InputStream in = ps.getInputStream();
int c;
while ((c = in.read()) != -1) {
}
in.close();
ps.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}