How to execute one Java Program from another Java program?

greenspun.com : 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 (tapan_sanghvi@yahoo.com), December 05, 2001

Answers

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...

bye

-- Java Expert Dr. Feber (hellotou@hotmail.com), 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 programname.java" );

-- Mario (mario@openroad.org), September 05, 2002.


some more alternatives:

java.lang.Runtime.exec() or http://www.rgagnon.com/javadetails/java-0014.html Process p = Runtime.getRuntime().exec(cmdline); or System.exec()

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

-- Leo Darman (suby56@yahoo.com), 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 (naeemghuman@yahoo.com), September 07, 2004.


Moderation questions? read the FAQ