How to execute one Java Program from another Java program? : LUSENET : Java Programming : One Thread

I am running a Java program and i need to call another java program from this program. I have the file name and the path of the Java program to be executed. It is '.java' file. so I first need to compile it and then execute it from within the running program. Is there a way to do it?

Waiting for replies. Tapan.

-- Tapan Sanghvi (, December 05, 2001


u can call a C program using native functions, and then in that C program u can compile and then run another Java program very easily.

i think that this will help u...


-- Java Expert Dr. Feber (, January 19, 2002.

It's an old post, but I'll answer anyway for future surfers: The RunTime class has an exec function to permit you to execute other programs. This function isn't static, but you can get a Runtime object using the Runtime.getRuntime() static function. Something like:

Runtime.getRuntime().exec( "java" );

-- Mario (, September 05, 2002.

some more alternatives:

java.lang.Runtime.exec() or Process p = Runtime.getRuntime().exec(cmdline); or System.exec()

or call another class using Class.forName()";

-- Leo Darman (, November 15, 2002.

try { Runtime.getRuntime().exec("C:\\my.exe"); } catch(IOException ex) { }

This will executes your .exe file. But if you want to load any type of file rathern than .exe then you may use BAT file. Make a .bat file save your command in that file then executing my.bat file. I think this is better.

-- Naeem Shehzad Ghuman (, September 07, 2004.

Moderation questions? read the FAQ