user2916888
user2916888

Reputation: 83

How to run a java program through a different java program?

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?

Upvotes: 0

Views: 175

Answers (2)

Jamsheer
Jamsheer

Reputation: 3753

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

Upvotes: 1

karimyafi
karimyafi

Reputation: 504

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");

Upvotes: 0

Related Questions