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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

main is Undefined in B.
The two programs are in different directory, package, and file.
if there are in different package then we have to import the members into current package. See my update.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.