Reputation: 329
I am executing a below program which call the shell through java and I am getting the excepton please help me.
program:
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);
}
}
}
and the exception
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
Upvotes: 3
Views: 4902
Reputation: 76908
ProcessBuilder processBuilder =
new ProcessBuilder("./LicenseGen.sh","--batchfile","commands.txt");
You need to pass in the command and args as separate strings or a list.
http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html
Upvotes: 7