0

I want my java program to execute a different java program. I used following method.

(The program I want to run is Example.java)

class RunJava
{
public static void main(String a[])throws Exception
{
Runtime.getRunTime().exec("c:\\"+path+"\\javac Example.java");
Runtime.getRunTime().exec("c:\\"+path+"\\java Example");
}

But it is not working. Is there any other way of doing this?

3
  • 2
    javac should be the first.. before that path (and java too). Commented Jan 1, 2014 at 11:58
  • its not the classpath. It is path of where javac is located in the system Commented Jan 1, 2014 at 11:59
  • 2
    If the 2nd app. is on the run-time class-path of the first, it is both simpler and cleaner to just create an instance of it, or directly call the main(String[]).. Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces. Commented Jan 1, 2014 at 12:00

2 Answers 2

1

please refer how to compile & run java program in another java program?. Same solution available !!

Sign up to request clarification or add additional context in comments.

1 Comment

It works fine, but I'm unable to understand how to give input to the 2nd program. And I am unable to post comment on the link you suggested. So could you answer this too?
0

Why don't you create an executable jar for the Example.java and place it in the path then execute it with:

Runtime.getRunTime().exec("c:/"+path+"/java -jar Example.jar");

1 Comment

That is a typically fragile implementation of exec that is bound to break.. See my earlier comment and link.

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.