3

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.

3
  • 3
    What is the question? Please add actual question to the... question. Commented Dec 4, 2012 at 12:36
  • 3
    Run a Java program using another Java program!!?? What for dude ? What are you trying to achieve ? Commented Dec 4, 2012 at 12:37
  • Why do you need this? How about sockets programming? Commented Dec 4, 2012 at 12:41

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

3 Comments

In the example given above, I have the source code so I can edit it with expectj.sourceforge.net But in reality I don't have the source code. All I have is the code to access another program.
Wait, what code do you have? The client (first in your question) or CalculateSum?
So, what I suggested was to use ExpectJ in the first code block (see edit), not the second (which was CalculateSum). I think it could really work!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.