bat文件测试
echo hello world!
pause
md e:\test
工程需要的bat文件
C:\**\**\**\**为python的地址
@echo off
::set INTERVAL = 10
::timeout %INTERVAL%
::Again
echo local_cap
C:
cd C:\**\**\**\**
start python chengxu\juzhentezheng.py
pause
rem 使用ping命令暂停3s,这样可以看到调用python后的结果
::ping -n 10 127.0.0.1 > nul
java调用代码
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
try {
StringBuilder sb = new StringBuilder();
String batPath = "C:/helloworld.bat";
Process child = Runtime.getRuntime().exec(batPath);
InputStream in = child.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
System.out.println(e);
}
} catch (IOException e) {
System.out.println(e);
}
运行结果
注:在java中,RunTime.getRuntime().exec()实现了调用服务器命令脚本来执行功能需要