运行 以编程方式来自特定位置的脚本
run script from specific location programatically
我有一个 java class 运行 我需要的服务器上的任何脚本,它运行良好。
但是我有一个问题,我需要从特定位置 运行 一个脚本,但不确定如何操作。在我的 java 方法中 运行 宁我的脚本之前,有没有办法 'cd' 到一个目录?
我目前的方法:
public String runScriptFile(String pathname) throws IOException{
Runtime rt = Runtime.getRuntime();
String output="";
Process proc = rt.exec(pathname);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
output+=s;
}
while ((s = stdError.readLine()) != null) {
output+=s;
}
return output;
}
答案是使用Runtime.exec中的location参数如下
Process proc = rt.exec(pathname, null, new File("C:\files\"));
也许this SO answer can help you
您可以为exec method
设置工作目录
我有一个 java class 运行 我需要的服务器上的任何脚本,它运行良好。
但是我有一个问题,我需要从特定位置 运行 一个脚本,但不确定如何操作。在我的 java 方法中 运行 宁我的脚本之前,有没有办法 'cd' 到一个目录?
我目前的方法:
public String runScriptFile(String pathname) throws IOException{
Runtime rt = Runtime.getRuntime();
String output="";
Process proc = rt.exec(pathname);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
output+=s;
}
while ((s = stdError.readLine()) != null) {
output+=s;
}
return output;
}
答案是使用Runtime.exec中的location参数如下
Process proc = rt.exec(pathname, null, new File("C:\files\"));
也许this SO answer can help you
您可以为exec method
设置工作目录