views:

99

answers:

3

I've been looking for an api for simple java-based multiprocessing and couldn't find anything.

I have legacy code which I need to integrate into my java application. Since this legacy (native) code crashes sometimes, the whole jvm crashes with it. So what I want to do, is to run this code and its adapter in a different process (not thread!).

There's the ProcessBuilder in the newer java jdks, it lets you start a new process and gives you a In/Outputstream; so solving my problem is possible by hand. In order to do so, you have to find the path to your jvm and start it up with the for your process. Then you have to use the stream to communicate.

Is there something which takes over that job? Or do I really have to do it by hand?

+1  A: 

AFAIK, people usually do this by hand.

The problem is that there's no truly portable way of doing it ... what with the difficulty (impossibility) of identifying where the currently running JVM was launched from, and the fact that JVM command-line options are vendor, version and (potentially) platform specific.

The simple solution is just to put the JVM path and options for launching the child JVM into some configuration file.

Stephen C
A: 

You can use the -Dprocess.name=$1 and let your main class take in some command line args. You can invoke by calling something like this:

java -cp $CLASSPATH $VM_ARGS $MAIN_CLASS

and your VM_ARGS can be defined something as

VM_ARGS=" -Dprocess.name=$1"

jayanth
A: 

FWIW, I wrote a replacement class to take care of a lot of the I/O stream redirection nastiness, at david.tribble.com/src/java/tribble/util/RuntimeExec.java

Loadmaster
to my knowledge, the I/O redirecting is handled by Process/ProcessBuilder in newer JDKs (http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html)
juwwtldn