在 netbeans 中一次运行多个 java 主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13714233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run multiple java main classes at once in netbeans
提问by David Sonnenfeld
I have several main classes with different arguments. I also added the arguments to each class successfully.
我有几个具有不同参数的主要类。我还成功地为每个类添加了参数。
But the problem is: I have to start each class manually every time (e.g. click on Run-File).
但问题是:我每次都必须手动启动每个类(例如单击运行文件)。
Is there a solution where I can start all the classes with one click in netbeans? And the classes should also follow a specific order.
有没有一种解决方案,我可以在 netbeans 中一键启动所有类?并且类也应该遵循特定的顺序。
回答by user3559041
Open a new project with a specific name (File -> New project and complete the walk-thru) in NetBeans.
You can create any number of new classes under one project by going to File -> New File -> and completing the walk-thru. At this point of time you should not include main method in these classes. Do not open a new project each time.
Create yet another file (by going thru File -> New File etc.). This time in this new class include code for the main method. From the main method you can call any number of classes by creating instances of those classes. The classes will be executed in the order you called them under the main method as long as all those classes are included in the same folder, that is under the same project.
在 NetBeans 中打开一个具有特定名称的新项目(文件 -> 新项目并完成演练)。
您可以通过转至文件 -> 新建文件 -> 并完成演练,在一个项目下创建任意数量的新类。此时,您不应在这些类中包含 main 方法。不要每次都打开一个新项目。
创建另一个文件(通过文件 -> 新文件等)。这次在这个新类中包含了 main 方法的代码。在 main 方法中,您可以通过创建这些类的实例来调用任意数量的类。只要所有这些类都包含在同一个文件夹中,即同一个项目下,这些类将按照您在 main 方法下调用它们的顺序执行。
It looks like you are writing java programs just as do it in the procedural languages. To some extent java classes are like subroutines of the procedural languages. Calling them is done by creating an instance of that class.
看起来您正在编写 Java 程序,就像在过程语言中一样。在某种程度上,java 类就像过程语言的子程序。调用它们是通过创建该类的实例来完成的。
回答by Robbin Singh
Set one class as main class through properties and run and in that you can Use following code:
通过属性将一个类设置为主类并运行,您可以使用以下代码:
ClassName variableName = new ClassName();
variableName.setVisible(true);
ex- Suppose my class name is Dog and I use frame as variable name
ex- 假设我的类名是 Dog 并且我使用 frame 作为变量名
Dog frame = new Dog();
frame.setVisible(true);*emphasized text*
回答by Jeutnarg
Drawing from comments and the question, I understand that you want to run ~5 different java programs simultaneously in your IDE (netbeans,) and the startup sequence has to be in a particular order. I assume you don't need CLI input for these programs once they're running.
根据评论和问题,我了解到您希望在 IDE(netbeans)中同时运行约 5 个不同的 Java 程序,并且启动顺序必须按特定顺序排列。我假设这些程序一旦运行就不需要 CLI 输入。
I'm unaware of a netbeans way to accomplish your goal, although in Eclipse a Launch Group would satisfy your requirements.
我不知道实现您的目标的 netbeans 方式,尽管在 Eclipse 中 Launch Group 可以满足您的要求。
IDE's aside, we can programmatically accomplish this goal anyway. The main() method in Java is just a static method, so if all your main methods are in one project, then you can just make a LaunchSequence class or something and do the following:
IDE 不谈,无论如何我们都可以通过编程实现这个目标。Java 中的 main() 方法只是一个静态方法,所以如果你所有的主要方法都在一个项目中,那么你可以创建一个 LaunchSequence 类或其他东西,然后执行以下操作:
public static void main(String[] args)
{
Thread t1 = new Thread()
{
@Override
public void run()
{
ServerOneClass.main(whateverArgsItNeeds);
}
};
t1.start();
Thread t2 = new Thread()
{
@Override
public void run()
{
ClientOneClass.main(whateverArgsItNeeds);
}
};
t2.start();
//and so on.
//The order is enforced by the order you call start(), not the order in which you declare the threads
}
If you have the code for these things in different projects, then you could make a new project and add them as dependencies.
如果您在不同的项目中有这些东西的代码,那么您可以创建一个新项目并将它们添加为依赖项。
If you do actually need user input for all the programs, you may benefit from looking at Running a java program from another java program
如果您确实需要所有程序的用户输入,您可能会受益于从另一个 java 程序运行 java 程序
回答by CodeAddict
Maybe call to each class seperately? For instance:
也许分别打电话给每个班级?例如:
FirstClass.java
FirstClass.java
SecondClass.java
SecondClass.java
ThirdClass.java
ThirdClass.java
In FirstClass, you could call on SecondClass to pop-up, simply with a setVisible(true)
if that's all you want it to do. Then in the SecondClass call to ThirdClass to pop-up the same way.
在 FirstClass 中,您可以调用 SecondClass 来弹出,只需简单地使用setVisible(true)
if 这就是您想要的。然后在SecondClass中调用ThirdClass以同样的方式弹出。
I'm not sure if this is what you wanted as there's no code to go off of, but just something to get you thinking.
我不确定这是否是您想要的,因为没有代码可以关闭,但只是让您思考的东西。
回答by xelamitchell
You can attempt to run multiple main classes via different run configurations.
您可以尝试通过不同的运行配置运行多个主类。