简体   繁体   中英

Run multiple java main classes at once in netbeans

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 (eg 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.

  1. Open a new project with a specific name (File -> New project and complete the walk-thru) in NetBeans.

  2. 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.

  3. 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.

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.

Maybe call to each class seperately? For instance:

FirstClass.java

SecondClass.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.

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.

You can attempt to run multiple main classes via different run configurations.

See http://wiki.netbeans.org/FaqTwoMainClassesWithArguments

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

Dog frame  = new Dog();
frame.setVisible(true);*emphasized text*

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.

I'm unaware of a netbeans way to accomplish your goal, although in Eclipse a Launch Group would satisfy your requirements.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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