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

I want to run “Test.java” through “Run.java”. This is the the code I tried:

Runtime.getRunTime().exec("C:\\"+path+"\\javac Test.java");
Runtime.getRunTime().exec("C:\\"+path+"\\java Test < In.txt > Out.txt");

But it did not worked.

Is there any other solution?

@aalhadkulkarni: If this is what u are looking for. :slight_smile: :slight_smile:

<<HAPPY NEW YEAR>>

Use this to Compile–>

Runtime rt = Runtime.getRuntime();

Process p = rt.exec(“javac -d . Test.java”);

//It will create the class file on the current working location, means it will create Test.class file where your Run.java file is located.

And to Run–>

Runtime rt1 = Runtime.getRuntime();

Process p1 = rt1.exec(“java Test<<in.txt>>out.txt”);

//Remember you never use .java extension while executing the class file all you need is just the name of the class nothing more than that.

1 Like

I’d seen that code. But could not understand how to give input to the 2nd program (Main.java)

HAPPY NEW YEAR to you too

i’ll search for a better explanation then :slight_smile:

thanks very much:)

Yes, it was a typing mistake (.java)

It is creating Test.class file. But not executing the program