Search code examples
javaprocessexecutable-jarjna

Is it possible to execute a runnable JAR and have it run as part of the same process from which it was called?


I have a client application and a server application. The server, using JNA, is a wrapper for some dll's I need to use because and long story short there is not enough memory to run both the client and server logic in a 32-bit JVM, hence the separate applications.

I need to be able to share memory between these two applications - specifically, I need to be able to send pointers from one to the other and vice versa. I know that I can execute the runnable JAR for the server from within the client jar - as a separate process. But is it possible to execute it as part of the same process, just on a different thread? That way I might be able to send pointers from one to the other.

Here's a bit of background:

Sending a JNA Pointer from one Java application to another Java application


Solution

  • Yes it is possible. Sort of.

    1. Create a new classloader for the JAR file.
    2. Using the classloader, read the MANIFEST.MF resource and extract the name of the JAR's main class.
    3. If there is a class-path attribute, parse it and create the corresponding classloaders.
    4. Use the classloader(s) to load the main class.
    5. Lookup the Method object for main(String[])
    6. Use Method.invoke(...) to call it with the appropriate "command line" arguments.

    The problem is that when you do this the application that you have started will share the standard i/o streams with the original one, as well as things like the system properties, default charset, default timezone and so forth.

    And I don't think this is going to solve your "not enough memory" or "not enough address space" problems.

    And finally, a 64 bit JVM cannot load 32 bit DLLs or vice versa.


    One possible alternative might be to use shared memory: