views:

453

answers:

4

I am executing another JVM (java.exe) from the main application. Is there any way to share an object (rather large object) with the newly created process (at the time of creation or after it was created).

someObject sO= new someObject();

//sO is populated

//Creating new process

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("java  -cp " + tempDir +  jarsInPath  + " " + appMain);

Now I want the sO object to be available to the process denoted by the proc object

Does ProcessBuilder provide any utilities for this purpose?

A: 

No there is no shared memory support in Java.

The simplest way to tackle this would be to serialize the object into a temp file and then deserialize it back in the new JVM.

Gregory Mostizky
I am working with a compiler (an extension of Java). I have to preprocess the AST which outputs different ASTs. In order to process these I have to call the compiler again. Instead of executing them separately, I want to streamline this process. Currently, I am simply re-parsing the original java program in the new process. I tried serializing but its rather a large object (a complete program to be precise) with many data structures.
+1  A: 

You can expose a service to allow access to the data from the object. It's comparatively simple to set up inter-process communication using RMI. There's going to be an IPC overhead so this will not be as performant as local access, fine-grained access will get expensive, but if you're getting summary or other agregated data then this could be a decent model.

You don't say why these are separate processes. Do you have any opportunity to load the code of you child process directly into the parent? Dynamic loading and unloading is possible.

djna
How do I 'oad the code of you child process directly into the parent'. Also I dont want to obtain data from the child process only send data from the parent process to the child process?
Parent->child, child->parent, makes no difference, one provides the service the other calls it. But my primary question was why they are separate processes at all? Just have one big process, with threads doing different jobs. Depending upon your needs you can just link one big program, or dynamically load classes using standard Java APIs
djna
+1  A: 

If you want to share objects, the best way is to use threads instead of a separate process. Processes cannot share memory (except through JNI), so you'd have to copy the large object back and forth in serialized form, either via files or via RMI socket connection (with the latter being the better option since it results in inherent synchronization).

Michael Borgwardt
Can a thread be used to start another JVM (java.exe) like the code shown above? I have always seen Process being used to start another JVM.
No you cannot start another JVM - then it would be a process, not a thread. But do you actually NEED another JVM? Can't you just execute the main() method you'd start the new JVM with in a thread instead?
Michael Borgwardt
A: 

I'm think you can use distributed caches for this purposes (EHCache, memcached and so on...)

Alexey Sviridov