Java类路径在JVM启动之后是最终的吗? [英] Is the Java classpath final after JVM startup?

查看:77
本文介绍了Java类路径在JVM启动之后是最终的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了很多有关Java类加载过程的信息。我经常碰到一些文字,这些文字声称在运行时无法将类添加到类路径并在没有类加载器黑客(URLClassLoaders等)的情况下加载它们。

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 load them without class loader hackery (URLClassLoaders etc.)

我知道类是动态加载的。这意味着它们的字节码表示形式仅在需要时才加载并转换为java.lang.Class对象。

As far as I know classes are loaded dynamically. That means their bytecode representation is only loaded and transformed to a java.lang.Class object when needed.

因此不应该添加JAR或*。 JVM启动并加载这些类后,是否将类文件保存到类路径,前提是尚未加载这些类? (要清楚:在这种情况下,类路径只是文件系统上的文件夹。添加JAR或* .class文件只是意味着将它们放在此文件夹中。)

So shouldn't it be possible to add a JAR or *.class file to the classpath after the JVM started and load those classes, provided they haven't been loaded yet? (To be clear: In this case the classpath is simply folder on the filesystem. "Adding a JAR or *.class file" simply means dropping them in this folder.)

如果不是,这是否意味着在JVM启动时搜索类路径,并且将所找到类的所有全限定名称缓存在内部列表中?

And if not, does that mean that the classpath is searched on JVM startup and all fully qualified names of the found classes are cached in an internal "list"?

它如果您能在回答中为我提供一些参考资料,对您会很好。最好是正式的SUN文档: Sun JVM Spec 。我已经阅读了规范,但是找不到有关类路径的任何信息,并且在JVM启动时还没有完成。

It would be nice of you if you could point me to some sources in your answers. Preferably the offical SUN documentation: Sun JVM Spec. I have read the spec but could not find anything about the classpath and if it's finalized on JVM startup.

Ps

这是一个理论问题。我只想知道是否有可能。我没有想要实现的实用方法。只是我对知识的渴求:)

This is a theoretical question. I just want to know if it is possible. There is nothing practical I want to achieve. There is just my thirst for knowledge :)

推荐答案

因为没人能给出明确的答案,也没有指向我相应部分的链接我自己提供答案的文档。尽管如此,我还是要感谢所有试图回答这个问题的人。

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.

简短的回答:

在JVM启动时,类路径不是最终的。

实际上,您可以在JVM启动后将类放在类路径中,然后将其加载。

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

长答案:

要回答这个问题,我在用户未知的建议,并编写了一些测试程序。

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

基本想法是有两个班级。第一类是实例化第二类的主要类。启动时,第二个类不在类路径中。 cli程序启动后,将提示您按Enter。在按Enter之前,您将第二个类复制到类路径上。按Enter后,将实例化第二个类。如果类路径在JVM启动时是最终的,则将引发Exception。但事实并非如此。因此,我认为类路径在JVM启动时不是最终的。

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.

以下是源代码:

JVMTest。 java

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

MyClass.java

package jvmtest;

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

文件夹结构如下:

jvmtest/
  JVMTest.class
  MyClass.class

我使用以下命令启动了cli程序:

I started the cli program with this command:

> java -cp /tmp/ jvmtest.JVMTest

您可以看到我在/ 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. 确保jvmtest中只有JVMTest.class。

  2. 使用上面的命令启动程序。

  3. 只是要确保按回车键。您应该看到一个异常,告诉您找不到任何类。

  4. 现在再次启动程序。

  5. 程序启动后,提示您按Enter,将MyClass文件复制到jvmtest文件夹。

  6. 按Enter。您应该会看到 MyClass v1。

  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".

附加说明:

此当我将MyClass类包装在罐子中并运行上面的测试时,它也起作用。

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

我在运行Mac OS X 10.6.3的Macbook Pro上运行了此程序

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

> Java -version

结果为:

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)

这篇关于Java类路径在JVM启动之后是最终的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆