I would like to write a java program that can run an another java programs Main class in runtime. How can I do that?
2 Answers
read this.
Basically you run new process and execute
Process tr = Runtime.getRuntime().exec( new String[]{ "XXX" } );
where XXX is phrase just like you would type in commandline. Remember that program might be in different location than your current execution so you might have to type command like java \path\to\program\program or such.
Comments
Call main() method of Other class inside Static Initialization block (SIB) of the class.
import packagename.B.*; // all class B members are available in Class A
class A
{
static
{
B.main(new String[9]);
}
public static void main(String[] args)
{
//do something
}
}
class B
{
public static void main(String[] args)
{
//do something
}
}
It will execute main() method of Class B before executing Class A.
3 Comments
joey rohan
main is Undefined in B.Szalai Mihály
The two programs are in different directory, package, and file.
Anil Reddy Yarragonda
if there are in different package then we have to import the members into current package. See my update.