tags:

views:

238

answers:

3

Hi,

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 code example or tutorial that any of you can recommend? Thanks.

+3  A: 

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
So this means I need to compile the code as a separate application and call it?
HH
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
@Dave: Yes, of course.
Michael Borgwardt
@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
+5  A: 

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.

The MYYN
His question is not about java.lang.Process but how write an application that can clone itself.
Aaron Digulla
@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
@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
@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
+2  A: 

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 classpath which you should find in the System property java.class.path.

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.

Aaron Digulla