31

I would like to create a process in my application. But after looking around and from Java's API I still don't quite get it.

Basically I want to create a multi process application. But the new process is a class in my application.

I know some of you might ask why not create a thread? Because the class is calling a matlab code, the problem and the Java class is Here

Is there any way to do this?

RamenChef
  • 5,557
  • 11
  • 31
  • 43
HH.
  • 769
  • 2
  • 10
  • 21
  • 2
    Note that the answer you linked does *not* say that you need a separate process. You can use a thread, you just need to make sure you only use MATLAB from one thread. – sleske Jan 05 '10 at 13:08
  • Well, Its not working for me. Did you read the link I posted on that thread? From what I interpreted from the article, I really need a process. Unless you can point me in the right direction. – HH. Jan 05 '10 at 14:06

5 Answers5

27

Maybe java.lang.Process could help here ..

The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.

miku
  • 181,842
  • 47
  • 306
  • 310
  • His question is not about java.lang.Process but how write an application that can clone itself. – Aaron Digulla Jan 05 '10 at 12:54
  • 6
    @Aaron: What makes you believe the poster wants the app to "clone itself"? And what does that even mean? Something like fork() in Unix? – sleske Jan 05 '10 at 13:06
  • @sleske: Aaron is almost right, I want to run a class in my code as a process while the rest of the classes as another process. In some sense its fork(). – HH. Jan 05 '10 at 13:58
  • @HH: Please not that on *nix machines calling the Runtime.exec and I suppose the ProcessBuilder.start() this is a fork(). That is your java process undergoes a fork() in order to create the new process. – Yaneeve Jan 05 '10 at 16:56
21

There is only one way to create processes in Java, Runtime.exec() - basically it allows you to start a new JVM just as you would via the command line interface.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • So this means I need to compile the code as a separate application and call it? – HH. Jan 05 '10 at 12:50
  • 9
    It allows you to start any system process, not just a new JVM, unless that's the new process you'd like to start. – Dave Jan 05 '10 at 12:51
  • @HH: as I said, you'd start it just like you start any Java program via the command line: "java -cp classpath package.name.MainClass". but "compile the code as a separate application" does not make much sense, of course it needs to be compiled, but Java does not really have the concept of "applications" as distinct entities. – Michael Borgwardt Jan 05 '10 at 12:58
  • 11
    Hi Michael, you didn't mentioned about "ProcessBuilder" in your answer, which is another way of process creation in java. – mhshams Feb 28 '11 at 06:44
  • @mohammadshamsi, [I have](http://stackoverflow.com/a/20903725/924313) – corazza Jan 03 '14 at 12:31
8

If you want more fine-grained control, you could use ProcessBuilder - this class allows you to set environment variables and configure the project's pipes (stdout, in, err).

Once you've configured it, you can call ProcessBuilder#start() as many times as you want in order to create new processes (it returns an instance of Process). You can change the configuration for new processes in between these calls to start().

corazza
  • 31,222
  • 37
  • 115
  • 186
5

I guess you know how to create a new process. If not, see here or here.

Now you need to run java.exe with your current classpath. You can find this classpath in the System property java.class.path. To locate java.exe, look in new File( System.getProperty("java.home"), "bin").

If you have problems with this approach, I suggest to write a wrapper script and call it with enough arguments so the code in main() can decide which actual class to invoke.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

My recommendation is to take a look at zt-exec: https://github.com/zeroturnaround/zt-exec

It wrapped java.lang.ProcessBuilder and and Apache Commons Exec, and could manage process life cycle easily.

Mason Zhang
  • 3,423
  • 24
  • 35