简体   繁体   中英

Eclipse - Assigning each main method to a separate console

I'm currently writing a program and I was wondering if it's possible for eclipse to open up separate consoles automatically for each main when I run the following main method:

public static void main(String[] args) {
    object1.main(args);
    object2.main(args);
    object3.main(args);
    object4.main(args);
}

My current solution is to run each main method and select the appropriate one to view within the "Display Selected Consoles" option although it's a very tedious process everytime I want to test my program out. I'd be very grateful for any suggestions you have.

Thanks

If you do multiple calls to your main method from within another main method, this will not be different from calling any other static method multiple times. Particularly, all those instances of your program will be executed in the same JVM.

Instead, you could use a simple Ant script to start multiple instances of your program, for example:

<?xml version="1.0"?>
<project name="Test" default="run_external">
    <target name="compile">
        <delete dir="bin" />
        <mkdir dir="bin" />
        <javac srcdir="src" destdir="bin" />
    </target>
    <target name="run_many" depends="compile">
        <parallel>
            <java classname="test.Main" classpath="bin" />
            <!-- copy-paste 'java' block for more instances -->
        </parallel>
    </target>
    <target name="run_external" depends="compile">
        <parallel>
            <exec executable="xterm" dir="bin">
                <arg value="-e" />
                <arg value="java test.Main" />
            </exec>
            <!-- copy-paste 'exec' block for more instances -->
        </parallel>
    </target>
</project>

This script defines three targets -- which of them is executed is determined in the default parameter.

  • The first target, compile , just invokes javac to build all the sources in the src directory. This is automatically executed before any of the other targets are run.
  • The second target, run_many , simply starts your Main class several times in parallel. Each instance will run in a separate JVM, but the output of all those instances will be mixed up in Eclipse's console window.
  • The third target, run_external , starts an xterm terminal emulator running the respective Java processes, ie for each instance of your program a new terminal should pop up. (When doing this on Windows you may have to use cmd.exe or similar.)

Not sure whether this is what you were looking for. Hope this helps.

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