56

Is it possible to do a "C like" fork in java, using an new independent jvm process ?

How?

Marko
  • 30,263
  • 18
  • 74
  • 108
sakana
  • 4,251
  • 5
  • 25
  • 20
  • 1
    Maybe you could go into greater detail about what you're trying to accomplish? As mentioned, you can exec() another process, which could be another instance of the JVM, but that doesn't give you the same state-sharing as fork() does in C. On the other hand, you can run threads, but there you get more sharing than you would with fork(). – Mark Bessey Nov 13 '08 at 17:56
  • I simply want to avoid the sharing that thread implies. Thks for your concern :) – sakana Nov 14 '08 at 13:42

3 Answers3

15

This answer is probably a little late but:

http://akuma.kohsuke.org/

seems to be exactly what your looking for

Michael Deardeuff
  • 10,386
  • 5
  • 51
  • 74
user525362
  • 151
  • 1
  • 2
  • it seems that it doesn't truly fork but exec's new child processes in a way that gives some benefits like fork. – rogerdpack Dec 23 '11 at 00:07
  • 3
    Note that it works only on POSIX-like system (so roughly everything but Windows) – CharlesB Jan 18 '12 at 15:41
  • You can read the source code of Akuma, and use posix API using JNA like akuma does. – Arnaud Feb 15 '17 at 14:54
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Ferrybig Jul 02 '17 at 10:03
12

Funnily, I am just working on this: a Java process running other Java processes. I used the article From Runtime.exec() to ProcessBuilder as a solid base, and When Runtime.exec() won't as a good advice how to gobble the output streams.

PS.: For those wondering, I had to do that (instead of spawning new threads) because yet another Java process is checking the presence of these processes which are, normally, ran separately with shell commands.

Basilevs
  • 22,440
  • 15
  • 57
  • 102
PhiLho
  • 40,535
  • 6
  • 96
  • 134
4

The Application Isolation API (JSR 121) introduces Isolate which addresses this use case.

Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67
  • 1
    As far as I can see, the isolation API is not supported under the current (1.8) JDK. There is an experimental MVM (Multitasking Virtual Machine) project that does, but it did not find its way in the mainline. Moreover it seems to me that the the isolation API does not allow to do a fork, where the state of the forking process is cloned in to the child: As far as I could understand the isolate starts with a main method, and (of course) different isolates do not share state. – Pietro Braione Oct 15 '14 at 12:23