How to create a process in Java [closed]
Asked Answered
M

5

33

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?

Mcclure answered 5/1, 2010 at 12:44 Comment(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.Corabelle
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.Mcclure
B
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.

Broad answered 5/1, 2010 at 12:47 Comment(5)
So this means I need to compile the code as a separate application and call it?Mcclure
It allows you to start any system process, not just a new JVM, unless that's the new process you'd like to start.Aruwimi
@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.Broad
Hi Michael, you didn't mentioned about "ProcessBuilder" in your answer, which is another way of process creation in java.Pagoda
@mohammadshamsi, I haveSusceptible
D
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.

Dynameter answered 5/1, 2010 at 12:49 Comment(4)
His question is not about java.lang.Process but how write an application that can clone itself.Plead
@Aaron: What makes you believe the poster wants the app to "clone itself"? And what does that even mean? Something like fork() in Unix?Corabelle
@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().Mcclure
@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.Glyptography
B
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.

Broad answered 5/1, 2010 at 12:47 Comment(5)
So this means I need to compile the code as a separate application and call it?Mcclure
It allows you to start any system process, not just a new JVM, unless that's the new process you'd like to start.Aruwimi
@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.Broad
Hi Michael, you didn't mentioned about "ProcessBuilder" in your answer, which is another way of process creation in java.Pagoda
@mohammadshamsi, I haveSusceptible
S
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().

Susceptible answered 3/1, 2014 at 12:31 Comment(0)
P
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.

Plead answered 5/1, 2010 at 12:54 Comment(2)
The system property java.class.path does not contain java.exe, you probably mean java.home/bin.Gerrygerrymander
@AndreasÅgren: The text was correct but confusing. Improved it.Plead
C
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.

Charmainecharmane answered 10/3, 2015 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.