Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:46:34+00:00 2026-06-15T16:46:34+00:00

I want to run a Java program from another using ProcessBuilder I used the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:46:35+00:00Added an answer on June 15, 2026 at 4:46 pm

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a Maven compilation by my Java code. Thus, I've used
I want to run Protovis javascript from Java and get the evaluated SVG code.
I want to run Prolog code using Java. I found some engines, but the
Possible Duplicate: Execute another jar in a java program Basically I want to run
I have the below code to run a java program from a file. import
I want to run a a java program on a remote computer 24 x
I want run a script as follows: runner: ssh 'java program &' ssh 'java
I want to package a piece of code that absolutely must run on Java
I'm trying to get my java program to run an svn command from the
I'm modifying a java program which was written in 2004 using javaw.exe from J2re1.4.2_03.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.