How to use multiprocessing with Java - NOT multithreading

I was looking for an api for simple Java-based multiprocessing and found nothing.

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

There ProcessBuilder in the new java jdks, it allows you to start a new process and gives you In / Outputstream; therefore, a solution to my problem is possible manually. To do this, you need to find the path to your jvm and run it using your process. Then you need to use the stream for communication.

Is there anything that takes on this job? Or do I really need to do this manually?

+3
source share
4 answers

AFAIK, people usually do it manually.

The problem is that there is no really portable way to do this ... that it is difficult (inability) to determine where the running JVM is running, and the fact that the JVM command-line options are providers, versions, and (potentially) a specific platform.

A simple solution is to simply set the JVM path and parameters to start the child JVM in some configuration file.

+2
source

You can use -Dprocess.name = $ 1, and let your main class take some command line arguments. You can call by calling something like this:

java -cp $ CLASSPATH $ VM_ARGS $ MAIN_CLASS

and your VM_ARGS can be defined as

VM_ARGS = "-Dprocess.name = $1"

0

FWIW, I wrote a replacement class to take care of the large oversaturation of I / O redirection, david.tribble.com/src/java/tribble/util/RuntimeExec.java

0
source

You are looking for Java RMI technology (Remolt method call).

This allows one JVM to call a method in another JVM. It can be on the same machine or on the network.

0
source

Source: https://habr.com/ru/post/1769062/


All Articles