我正在执行一个下面的程序,它通过java调用shell,我得到了除了请帮帮我。
程序:
import java.io.*;
import java.util.*;
public class ProcessExample {
/**
* @param args
*/
public static void main(String args[]) throws IOException {
File file=new File("/opt/nilesh/fazal");
ProcessBuilder processBuilder = new ProcessBuilder("./LicenseGen.sh --batchfile commands.txt");
processBuilder.directory(file);
processBuilder.redirectErrorStream(true);
System.out.println("nilesh");
Process process=processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:",
Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
OutputStream os=process.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
bw.write("create licensekey -x license-input.xml");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
和例外
Excepton:Exception in thread "main" java.io.IOException: Cannot run program "./LicenseGen.sh --batchfile commands.tx": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at ProcessExample.main(ProcessExample.java:23)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 1 more
答案 0 :(得分:7)
ProcessBuilder processBuilder =
new ProcessBuilder("./LicenseGen.sh","--batchfile","commands.txt");
您需要将命令和args作为单独的字符串或列表传递。
http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html