1

Is it possible to execute two separate classes with one single java command?

I would like to run a few java programs concurrently (it should start at the same time) for my project.

Example: I have two java programs A.java and B.java.

Compile

javac A.java B.java

Run

java A B

However this doesn't work. How else can I do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
N2M
  • 199
  • 1
  • 15

4 Answers4

1

No, the java command simply does not work like that.

Instead have a C.java that calls both A & B classes.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Make a class, say Parallel having as command line arguments the other class names. Then start for each class a new thread and call its main.

Probably can be done neater.

java Parallel A B

public class Parallel {

    public static void main(String[] args) {
        for (String arg : args) {
            try {
                Class<?> klazz = Class.forName(arg);
                final Method mainMethod = klazz.getDeclaredMethod("main",
                    String[].class);
                if (mainMethod.getReturnType() != void.class) {
                    throw new NoSuchMethodException("main not returning void");
                }
                int modifiers = mainMethod.getModifiers();
                if (!Modifier.isStatic(modifiers)
                    || !Modifier.isPublic(modifiers)) {
                    throw new NoSuchMethodException("main not public static");
                }
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            mainMethod.invoke(null, (Object)new String[0]);
                        } catch (IllegalAccessException
                            | IllegalArgumentException
                            | InvocationTargetException ex) {
                            Logger.getLogger(Parallel.class.getName())
                                .log(Level.SEVERE, null, ex);
                        }
                    }
                });
                thread.start();
            } catch (ClassNotFoundException
                | NoSuchMethodException
                | SecurityException ex) {
                Logger.getLogger(Parallel.class.getName())
                    .log(Level.SEVERE, null, ex);
            }                        
        }
    }
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • :thankyou for the code. However it doesnt work correctly. I get the following error while executing it. java.lang.IllegalArgumentException: wrong number of arguments ...... at java.lang.reflect.Method.invoke(Method.java:601) at Parallel$1.run(Parallel.java:28) at java.lang.Thread.run(Thread.java:722) This line corresponds to --> mainMethod.invoke(null); in the code – N2M May 13 '13 at 09:49
  • You are right, null (no `this`) for a static call, but forgotten the parameter `String[] args`. – Joop Eggen May 13 '13 at 10:07
0

No, the CPU will allways start one one command as first, what you can do is to let both threads wait for some notification which signals them to start, but even then i would guess internally one of them will start befor the other.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

No matter how hard u try to start them simultaneously, the output will not be consistent. This won't work even if you try to load them through the third class.

What you can do is you need to handle this logic in your code. Possible options are -

  1. Run the objects in separate threads and make one join other.(this should be the ideal scenario. Both ways dependency is not good)
  2. Add logic in both programs to execute critical code at a same given time.(e.g. make both run at 16:00 EST). (Still i will say #1 option is better)
Kshitij
  • 8,474
  • 2
  • 26
  • 34