I would like to write a java program that can run an another java programs Main class in runtime. How can I do that?
Asked
Active
Viewed 222 times
2 Answers
0
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.

Anil Reddy Yarragonda
- 747
- 5
- 19
-
`main` is Undefined in `B`. – joey rohan Jul 08 '15 at 12:29
-
The two programs are in different directory, package, and file. – Szalai Mihály Jul 08 '15 at 12:52
-
if there are in different package then we have to import the members into current package. See my update. – Anil Reddy Yarragonda Jul 08 '15 at 12:57
0
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.