Is the Java classpath final after JVM startup?

后端 未结 7 571
灰色年华
灰色年华 2021-02-02 11:11

I have read a lot about the Java class loading process lately. Often I came across texts that claimed that it is not possible to add classes to the classpath during runtime and

7条回答
  •  死守一世寂寞
    2021-02-02 11:50

    Since nobody could give my a definite answer nor a link to a corresponding part of the documentation I provide a answer myself. Nevertheless I would like to thank everybody that tried to answer the question.

    Short answer:

    The classpath is not final upon JVM start.

    You actually can put classes in the classpath after the JVM started and they will be loaded.


    Long answer:

    To answer this question I went after user unknowns suggestion and wrote a little test program.

    The basic idea is to have two classes. One is the main class which instantiates the second class. On startup the second class is not on the classpath. After the cli program started it'll prompt you to press enter. Before you press enter you copy the second class on the classpath. After you press enter the second class is instantiated. If the classpath would be final on JVM startup this would throw an Exception. But it doesn't. So I assume the classpath is not final on JVM startup.

    Here are the source codes:

    JVMTest.java

    package jvmtest;
    
    import java.io.Console;
    import jvmtest.MyClass;
    
    public class JVMTest {
      public static void main(String[] args) {
        System.out.println("JVMTest started ...");
    
        Console c = System.console();
        String enter = c.readLine("Press Enter to proceed");
        MyClass myClass = new MyClass();
        System.out.println("Bye Bye");
      }
    }
    

    MyClass.java

    package jvmtest;
    
    public class MyClass {
      public MyClass() {
        System.out.println("MyClass v2");
      }
    }
    

    The folder structure looks like this:

    jvmtest/
      JVMTest.class
      MyClass.class
    

    I started the cli program with this command:

    > java -cp /tmp/ jvmtest.JVMTest
    

    As you can see I had my jvmtest folder in /tmp/jvmtest. You obviously have to change this according to where you put the classes.

    So here are the steps I performed:

    1. Make sure only JVMTest.class is in jvmtest.
    2. Start the program with the command from above.
    3. Just to be sure press enter. You should see an Exception telling you that no class could be found.
    4. Now start the program again.
    5. After the program started and you are prompted to press enter copy the MyClass file into the jvmtest folder.
    6. Press enter. You should see "MyClass v1".

    Additional notes:

    This also worked when I packed the MyClass class in a jar and run the test above.

    I ran this on my Macbook Pro running Mac OS X 10.6.3

    > Java -version
    

    results in:

    java version "1.6.0_20"
    Java(TM) SE Runtime Environment (build 1.6.0_20-b02-279-10M3065)
    Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01-279, mixed mode)
    

提交回复
热议问题