Search code examples
javamethodscallprogram-entry-point

Java main class initiate another main class


I have two different java packages, one with a main class which calls another public class and then a second package which just has a main class. The issue is that I would like the package with one main class to call the main class in the other package to then initiate it. I tried doing it using the code below, but it obviously does not work as the two different classes aren't in the same package.

String[] args = {};
myMainClassNumber2.main(args);

Solution

  • You need to tell the compiler what package the other class is in.

    Either

    import your.pkg.myMainClassNumber2;
    
    public static void main(String[] myArgs) {
      String[] args = {};
      myMainClassNumber2.main(args);
    }
    

    or

    public static void main(String[] myArgs) {
      String[] args = {};
      your.pkg.myMainClassNumber2.main(args);
    }