我正在尝试使用Runtime exec(Java)运行几个.bat命令。问题是在设置环境变量后,我无法使用这些变量。
Runtime rt = Runtime.getRuntime();
Process pr;
pr = rt.exec("cmd.exe /c echo %JAVA_HOME%");
InputStream is;
InputStreamReader isr;
BufferedReader reader ;
String line;
is = pr.getInputStream();
isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
pr.getOutputStream().close();`
JAVA_HOME不返回实际的env路径。
答案 0 :(得分:1)
代码对我来说很完美,
try {
Runtime rt = Runtime.getRuntime();
Process pr;
pr = rt.exec("cmd.exe /c echo %JAVA_HOME%");
InputStream is;
InputStreamReader isr;
BufferedReader reader;
String line;
is = pr.getInputStream();
isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
pr.getOutputStream().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果它不适合你,
cmd.exe /c echo %JAVA_HOME%