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?

3
  • With two different commands? java A, and then java B. Commented May 13, 2013 at 7:43
  • 3
    or create class C that starts a Thread to run A and immediatly starts a Thread to run B Commented May 13, 2013 at 7:44
  • "it should start at the same time". even if there is a way to do it, it doesn't guarantee concurrent execution. Commented May 13, 2013 at 7:45

4 Answers 4

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

: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
You are right, null (no this) for a static call, but forgotten the parameter String[] args.
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.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.