0

Let's say that I have Client class with a main method that does something (maybe connecting to class Server).

Is there a way to create a class (let's say ClientLauncher) that launch n clients (n passed as a parameter) as different java applications?

Note that, I do not want these clients to be different threads inside one application. I want to obtain the same effect that I would get by pressing the run button in Eclipse several times (launching different main methods).

Kami
  • 1,079
  • 2
  • 13
  • 28
  • It's possible, though I would recommend doing that using shell/batch script rather than writing a program. – RealSkeptic Jun 10 '15 at 07:48
  • You can run additional programs using the command line. This can be called from within Java. Is that along the lines you're thinking? – Mapsy Jun 10 '15 at 07:48
  • 2
    So you want to run a jvm per client? You'll need to use the Runtime to launch separate processes. see http://stackoverflow.com/questions/8496494/running-command-line-in-java – Romski Jun 10 '15 at 07:48
  • [ProcessBuilder](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html) allows to launch new process. You could launch a `java myProgram` for example. – Michael Laffargue Jun 10 '15 at 07:50

1 Answers1

3

I'm not sure why you don't want to run clients using threads, Runtime.getRuntime().exec() can invoke extarnal jars within a class

Process run= Runtime.getRuntime().exec("java -jar jarpath_here");

If you put this in loop you'd have multiple processes.

dogant
  • 1,376
  • 1
  • 10
  • 23