【发布时间】:2010-10-06 11:32:05
【问题描述】:
我尝试从 java 代码运行 shell 脚本,但我遇到了问题。该脚本在 batchstart.sh 文件中 -
#!/bin/ksh
export DISPLAY=:0.0
现在脚本在命令行上运行一个点 -- 。批处理启动.sh
如何从 java 运行它?我的java代码如下。它抛出以下异常 -
java.io.IOException: .: 未找到 在 java.lang.UNIXProcess.forkAndExec(本机方法) 在 java.lang.UNIXProcess.import java.io.*;
public class SetDisplay {
public static void main(String[] args) {
File wd = new File("/myhomedir/");
System.out.println("Working Directory: " +wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec(". batchstart.sh", null, wd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如何让 shell 脚本运行?
我也尝试了以下代码,但也不起作用。
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd /home/");
out.println(". batchstart.sh");
out.println("exit");
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】: