如何指定要在其文件系统中动态加载的 .class 文件的路径?

huangapple go评论72阅读模式
英文:

How to specify the path of a .class file to be dynamically loaded in its filesystem?

问题

当一个 Java 程序动态加载一个 .class 文件时,

  • 只需要 .class 文件的完全限定类名吗?ClassLoaderloadClass() 方法是否只需要动态加载的 .class 文件的完全限定类名?

  • 如果 .class 文件可以在文件系统的任何位置找到,我如何在文件系统中指定它的路径名?

  • 当使用 java 命令运行由 Java 程序生成的这种字节码时,我需要在 -cp 中指定要动态加载的 .class 文件的路径吗?

英文:

When a java program dynamically load a .class file,

  • Is only the fully qualified classname of the .class file needed? Does ClassLoader
    s method loadClass() only require the fully qualified classname of the .class file to be dynamically loaded?

  • If the .class file can be located anywhere in a filesystem, how can I specify its pathname in its filesystem?

  • When running such the bytecode create from a Java program with command java, do I need to specify the path of the .class file to be dynamically loaded in -cp?

Thanks.

答案1

得分: 2

你混淆了类和".class"文件。

> 只需要".class"文件的完全限定类名吗?ClassLoader的loadClass()方法是否只需要".class"文件的完全限定类名来动态加载类?

需要类的完全限定类名。

> 如果".class"文件可以在文件系统的任何位置找到...

不行!与类对应的".class"文件需要在相关类加载器的类路径上。

如果您想从文件系统中的任意文件加载类,您需要创建一个新的类加载器实例:

  • 如果您使用标准类加载器实现之一,它将使用标准方案来基于类的包名解析".class"文件的位置。

  • 可以实现一个自定义类加载器,将FQ类名解析为文件系统对象...其他方式。但这似乎是不必要的,因为标准的Java编译器会按照标准方案生成".class"文件。

> 当使用java命令从Java程序运行这些字节码时,是否需要在-cp中指定要动态加载的".class"文件的路径?

不需要提供".class"文件的位置。您需要提供一个可以解析".class"文件的目录位置,就像上面提到的。

但是,如果正在运行的Java程序生成、编译然后加载一个类,它将需要动态创建一个新的类加载器来可靠地加载它。

为什么?因为类加载器通常会缓存类路径上目录和JAR索引的内容。因此,当程序写入新文件时,类加载器可能不会知道它。

还有一个问题是,类加载器不能加载相同的类(从任何位置)两次。

最后,请注意,如果两个类加载器加载具有相同完全限定名称的类,则运行时类型系统将其视为不同的类/类型。您不能在这两种类型的实例之间进行强制转换。

英文:

You are conflating classes and ".class" files.

> Is only the fully qualified classname of the .class file needed? Does ClassLoader s method loadClass() only require the fully qualified classname of the .class file to be dynamically loaded?

The fully qualified class name of the class is required.

> If the .class file can be located anywhere in a filesystem ...

It cannot be! The .class file corresponding to the class needs to be on the relevant classloader's classpath.

If you want to load a class from an arbitrary file in the file system, you need to create a new classloader instance:

  • If you use one of the standard classloader implementations, it will use the standard scheme for resolving a .class file location based on the classes package names.

  • It would be possible to implement a custom classloader that resolves FQ classnames to file system objects ... some other way. But that seems unnecessary, since a standard Java compiler will emit .class files as per the standard scheme.

> When running such the bytecode create from a Java program with command java, do I need to specify the path of the .class file to be dynamically loaded in -cp?

You don't give the location of the .class file. You give the location of a directory where the .class file can be resolved, as above.

But if a running Java program generates, compiles and then loads a class, it will need to dynamically create a new classloader to load it ... reliably.

Why? Because classloaders typically cache the contents of directories and JAR indexes on the classpath. So when a program writes a new file, the classloader may not get to know about it.

There is also the issue that a classloader cannot load the same class (from any location) twice.

Finally, be aware that if two classloaders load a class with the same fully qualified name, the runtime type system treats them as distinct classes / types. You can't cast between the two incarnations of the type.

huangapple
  • 本文由 发表于 2020年3月17日 02:16:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/60711220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定