5

I have a JVM which is already running. I have its processID with me. Now I wish to run some other Java code inside this JVM,i.e., the code should run in this existing JVM rather than spinning up its own JVM.

I have already looked at the Attach API. However it requires code to be packaged in a JAR.

Is there any other way?

6
  • 3
    If it's not in a jar, then what is it? You'd need a java compiler to be able to run .java source code files, which usually isn't included in JREs. Commented Mar 19, 2015 at 14:02
  • 1
    If the code you want to run is not in a jar, then where is it? Commented Mar 19, 2015 at 14:11
  • I think you can evaluate bytecode through the various debug API too. Commented Mar 19, 2015 at 14:13
  • Maybe using the Attach API + Instrumentation API, then you could load an agent on the VirtualMachine, like JMX do. Commented Mar 19, 2015 at 14:52
  • This previous answer should help you to debug your application stackoverflow.com/questions/10899675/… Commented Mar 19, 2015 at 15:22

1 Answer 1

1

The easiest way seems to be with the Attach API. However since you don't want to use it, you might want to google about RMI/JMS/JMX which would also allow you to do similard manipulation.

If you start the program using the standard java command, then a new VM will be created for each program.

However, since this look like an XY problems, here an easier alternative that would probably allow you to do what you want.

It is possible to run programs on different threads of the VM.

Here an interesting snippet that would create a simple launcher, then you can start the program giving the main class of each program you want to start as argument to the main method which will create a new thread for each program, but everything will be running on the same VM as the launcher.

public class Launcher {
  public static void main(String[] args) throws Exception {
    for (int i = 0; i<args.length; i++) {
      final Class clazz = Class.forName(args[i]);
      new Thread(new Runnable() {
        @Override
        public void run() {
           try{
             Method main = clazz.getMethod("main", String[].class);
             main.invoke(null, new Object[]{});
           } catch(Exception e) {
             // improper exception handling - just to keep it simple
           }
        }
      }).start();
    }
  }
}

Note : I don't know what you are really trying to do, but if it is to be used with big applications, you might want to increase the heap properties to avoid problems.

Sign up to request clarification or add additional context in comments.

8 Comments

This seems like something that is best not done. But if it really has to be done, this is an elegant way to do it.
This is answering a different question. The question is about running code in an existing JVM from the outside, not launching a main in a class on the classpath.
@ThorbjørnRavnAndersen I know what is the question, and I state at the beginning that it is impossibe. However, I give an alternative that will probably solve the real problem. I'm pretty sure this is an XY problem.
Sure it is possible. That is what the Attach API was created to allow and how e.g. VisualVM works.
If the java code wasn't on the original classpath, you could use a the above approach with URLClassLoader to load in the new classes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.