I want to run a Java program from another using ProcessBuilder
I used the code
Process pr = rt.exec("cmd /c cd C:\\Users\\naman\\Desktop & java CalculateSum");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
BufferedWriter output= new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
output.write("10");
output.write("30");
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
CalculateSum has following code:
System.out.print("Enter 1 st value : ");
a=Integer.parseInt(br.readLine());
System.out.print("\nEnter second number : ");
b=Integer.parseInt(br.readLine());
System.out.println("\nresult is : "+(a+b));
My basic motivation is to run a Java program from another Java program.
NOTE: I don't want to use command line arguments to take input. Also I have tried using ProcessBuilder for the same purpose, but that also did not work.
You could use ExpectJ (http://expectj.sourceforge.net/) to talk to another program using standard input/output.
Use this instead of trixing with BufferedReader/BufferedWriter in your first code block:
ExpectJ expectinator = new ExpectJ(5);
Spawn shell = expectinator.spawn("cmd /c cd C:\\Users\\naman\\Desktop & java CalculateSum");
// Talk to it
shell.expect("Enter 1 st value");
shell.send("10\n");
shell.expect("Enter second value");
shell.send("30\n");
Just blindly guessing at what the issue is, the problem may be flushing.
Try adding System.out.flush(); after each print in the CalculateSum.
In the first program, add newline to your output.write calls, such as output.write("10\n");, and also output.flush(); after that.
Related
I have a requirement, where I will be executing a batch file in java. On completion, the batch script waits for the user input. Then I have to pass two commands, each command followed by the enter key. Please find my code below,
File batchFile = new File("batch file path");
ProcessBuilder builder = new ProcessBuilder();
builder.redirectErrorStream(true);
builder.command(batchFile.getAbsolutePath());
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
process.getOutputStream()));
String s;
loop:
while ((s = reader.readLine()) != null) {
System.out.println("$: " + s);
if (s.contains("last line before user input. so i am breaking here")) {
break loop;
}
}
writer.write("Command1");
writer.newLine();
writer.flush();
writer.write("Command2");
writer.newLine();
writer.flush();
writer.write("/close");
writer.newLine();
writer.flush();
writer.close();
while ((s = reader.readLine()) != null) {
System.out.println("$: " + s);
}
Issue:
Though I am sending input through the bufferedwriter, the process is not getting the input.Can someone give me any pointers to fix this issue. The program runs till the last while loop. When it enters the last while loop statement, it is getting hanged. It looks like the process is still waiting for the user input, so reader.readLine() in the while statement waits infintely. I have been searching for a solution for the whole day, but still no luck. Any help is appreciated.
I have a weird problem when trying to execute a shell command from within a java program. Since there exist thousands of websites that explain how to do it I used the following recommended code:
public String executeShellCommand (String command)
{
try
{
StringBuffer sb = new StringBuffer();
String line = "";
Process p = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
p.waitFor();
return sb.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
Acutally, when I try to execute for instance ls -aF is works fine and I get some output as a result. Therefore I'm pretty sure that the above code is, in principal, correct. However, I got another program I'd like to run and that produces a file as an output. I would like to execute it the above way but it never is executed and no output file is generated. Also I do not get any error, warnings or whatsoever in java. When copy and pasting the actual command argument string into the console the execution of the programm/command directly in the shell works fine and the output file is generated. So the command I pass to the method is also correct.
Are there additional things I need to pay attention to when trying to execute a shell command from within java?
UPDATE: I modified my code according to the suggestions. However, it is still hanging:
public String executeShellCommand(List<String> command, String logfile, boolean waitForProcess) { try {
ProcessBuilder pb = new ProcessBuilder(command);
System.out.println("pb.toString() = " + pb.toString());
Process p = pb.start();
System.out.println("2");
BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader out = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("3");
StringBuilder errSb = new StringBuilder();
StringBuilder outSb = new StringBuilder();
String line;
System.out.println("4");
while ((line = err.readLine()) != null) { // <--- code hangs here
errSb.append(line + "\n");
System.out.println("errSb = " + errSb.toString());
}
System.out.println("4a");
while ((line = out.readLine()) != null) {
outSb.append(line + "\n");
System.out.println("outSb = " + outSb.toString());
}
System.out.println("5");
if(waitForProcess) {
System.out.println("Wait for process");
p.waitFor();
} else {
System.out.println("Sleep 5000");
Thread.sleep(5000);
}
System.out.println("6");
//Log result to file
if(logfile != null) {
OutputStreamWriter outWriter = new OutputStreamWriter(new FileOutputStream(logfile));
outWriter.write(errSb.toString());
outWriter.close();
}
return errSb.toString();
} catch(Exception e) { e.printStackTrace(); } return null; }
This will block if your command writes too many characters to stderr. Like for sdtout, Java redirect stderr through a pipe, and if you do not read the pipe, it can fill up and block (size of the pipe is probably less than 256 bytes). To avoid that, you need to read from the Process.getErrorStream(), preferable from another thread as the main thread is busy reading from the Process.getInputStream().
A simpler way to avoid that is to use the ProcessBuilder class instead of Runtime.exec() and ProcessBuilder.redirectErrorStream(true) so that both stdout and stderr are merged into the Process.getInputStream()
As per Process javadoc :
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.
You are calling p.waitFor(). If we carefully read the waitFor() documentation:
Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.
You are waiting for a process which hangs, because its error stream and output stream are never read.
What you should do, is to read these streams:
p.start();
BufferedReader err= new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader out = new BufferedReader(new InputStreamReader(p.getOutputStream()));
StringBuilder errSb = new StringBuilder();
StringBuilder outSb = new Stringbuilder();
String line;
while ((line = err.readLine()) != null) {
errSb.append(line);
}
while ((line = out.readLine()) != null) {
outSB.append(line);
}
int retCode = p.waitFor(); //0 for success
System.out.println(retCode);
System.err.println(errSB.toString());
You should always read the error stream when calling external programs via the Process class, else you may find yourself in this odd situation where a process hangs forever. (well until someone else -the operating system, another application, etc- kills it, more exactly).
I've also noticed that you use the Runtime.getRuntime() which is not the recommended way to run external programs, starting with java 1.5, as per javadoc:
As of 1.5, ProcessBuilder.start() is the preferred way to create a Process.
ProcessBuilder pb = new ProcessBuilder("ls" , "-aF");
Process p = pb.start();
I've a Java program to call a python script. I've used exec method. Please find the code snippet below:
Python program (which is to gather a portion of text from wikipedia), when run separately, gives me proper output. When called from Java, I'm not getting the complete output from python program.
I checked the status of BufferedReader Object using ready() method ( as explained here, and the code entered into infinite loop.
I think others also have faced similar problems-https://stackoverflow.com/a/20661352/3409074
Can anyone help me?
public String enhanceData(String name,String entity) {
String s = null;
StringBuffer output = new StringBuffer();
try{
String command="python C://enhancer.py "+name+" "+entity;
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdError=new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
output.append(s);
}
The while loop condition has actually already read a line so you are double reading it for every time in the loop.
while ((s = stdInput.readLine()) != null) {
//s=stdInput.readLine(); <- don't need this
System.out.println(s);
output.append(s);
}
/Nick
I'm trying to run /usr/bin/perl -e 'for(my $i=0;$i<1000;$i++){print 1x1000;print STDERR 2x1000;}' (which works in terminal) with my program.
ProcessBuilder pb = new ProcessBuilder(go); //go is the command
process = pb.start();
BufferedReader incommandbuf = new BufferedReader(new InputStreamReader(process.getInputStream()),1024*1000);
BufferedReader errcommandbuf = new BufferedReader(new InputStreamReader(process.getErrorStream()),1024*1000);
stdString = "";
while ((line = incommandbuf.readLine()) != null)
{
stdString += line + "\n";
}
String errorstrtemp = "";
while ((line = errcommandbuf.readLine()) != null)
{
errorstrtemp += line + "\n";
}
If I try to run this it hangs on while ((line = incommandbuf.readLine()) != null). The program runs if I change the command to /usr/bin/perl -e 'for(my $i=0;$i<64;$i++){print 1x1000;print STDERR 2x1000;}'. If it goes up to 65 and higher it doesn't work. At first I thought I just have to change the size of the my BufferedReaders but it didn't help. Any clue on what is causing this? I will provide any additional info if needed.
Thanks.
You are reading one stream at a time. When the other stream fills up the buffer, your Process will stop waiting for you to read it. The solution is to either read the streams in different threads or use ProcessBuilder.redirectErrorStream
First, I saw a few Q's about this issue in the site, but didn't see any answer that solve my problem.
I have a program written in Java and it calls a cmd program written in C++. (this is an assumption since I don't have the actual source) I know the expected I/O of the C++ program, in the cmd it is two lines of output and then it waits for string input.
I know that the first output line of the program is through error stream, and I receive it properly (this is expected), but I don't get the second line in error or input stream.
I tried to write to the program right after the first line ( the error line) and didn't got stuck, but there was no response.
I tried using 3 different threads, for each stream, but again, nothing was received in input/error stream after the first line, and the program didn't respond to writing through output stream.
My initializers are:
Process p = Runtime.getRuntime().exec("c:\\my_prog.exe");
BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
Is it possible at all or maybe it depends on the C++ program?
Thanks,
Binyamin
If you want to call native applications like C and C++ from Java, you need to use JNI.
I would suggest to put the input in the program when it has started, it will propably use that as input when it wants it.
Here is how I execute any command line in Java. This command line may execute any program:
private String executionCommandLine(final String cmd) {
StringBuilder returnContent = new StringBuilder();
Process pr;
try {
Runtime rt = Runtime.getRuntime();
pr = rt.exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
returnContent.append(line);
}
input.close();
LOG.debug(returnContent.toString());
// return the exit code
pr.waitFor();
} catch (IOException e) {
LOG.error(e.getMessage());
returnContent = new StringBuilder();
} catch (InterruptedException e) {
LOG.error(e.getMessage());
returnContent = new StringBuilder();
}
return returnContent.toString();
}