我必须从我的java应用程序中打开(或在后台执行)一个cmd窗口。之前它做到了这一点并且完全正常:
public String getEnvVar(String envVarName) throws Exception {
String varName = "%" + envVarName + "%";
Process process = Runtime.getRuntime().exec(
new String[]{"cmd.exe", "/C", "echo " + varName}
);
BufferedReader input = new BufferedReader(
new InputStreamReader(process.getInputStream())
);
varName = input.readLine();
process.destroy();
...
我的问题是:我有一个不同类的不同实例,并尝试按照上面代码中的方式打开cmd窗口:
private void execute() {
try {
String batchData = batchContent();
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", batchData});
new File(sourceFile).delete();
new File(sourceFolder).delete();
process.destroy();
} catch (IOException ie) {
}
}
但这不起作用......我用谷歌找出原因,找不到真正的原因。
甚至
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "cd c:\\temp"});
不会更改为临时文件夹(我出于测试原因预先创建的)
怎么了?
EDIT //
我现在尝试过:
private void upload() {
try {
//Process process = Runtime.getRuntime().exec(batchDataFile);
//Process process = Runtime.getRuntime().exec("cmd.exe /C " + batchDataFile);
Process process = Runtime.getRuntime().exec("cmd.exe /C " + "\"" + batchDataFile + "\"");
//new File(sourceCodeFile).delete();
//new File(batchDataFile).delete();
//new File(sourceCodeFolder).delete();
process.destroy();
} catch (IOException ie) {
ie.printStackTrace();
}
}
我还尝试在没有Java应用程序的情况下运行生成的批处理skript,它按预期工作