简体   繁体   English

Java主类启动另一个主类

[英]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. 我有两个不同的Java程序包,一个具有一个主类,该主类调用了另一个公共类,然后是另一个具有一个主类的程序包。 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);

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM