0

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 Answers2

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.

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.

Community
  • 1
  • 1
T.G
  • 1,913
  • 1
  • 16
  • 29