简体   繁体   English

Java类动态加载过程

[英]Java class dynamic loading process

I am having an argument with my professor which I cant seem to find an answer to in google... My question is, when I use a method that is in another class, the JVM will try and find the class's .class file right? 我正在和我的教授争论,我似乎无法在google中找到答案。我的问题是,当我使用另一个类中的方法时,JVM会尝试找到该类的.class文件吗? and the argument is this: if the .class file is not found will there be a search for the source file and if found it will be recompiled or will it throw an exception? 参数是这样的:如果未找到.class文件,将搜索源文件,如果找到该文件,将对其进行重新编译或将引发异常? Thanks! 谢谢!

UPDATE: I rephrased the question, I will be very grateful for your help! 更新:我改掉了这个问题,非常感谢您的帮助!

It is not at all the standard behavior, and will not work in a normal environment. 它根本不是标准行为,并且在正常环境下将无法运行。

However, I have heard of some commercial app server distributions that were able to do that... someone told me about older weblogic versions, configured in development mode, but maybe just an urban legend :P 但是,我听说一些商业应用服务器发行版可以做到这一点...有人告诉我有关在开发模式下配置的较旧的weblogic版本,但也许只是城市传说:P

I guess that you could write a tuned classloader that looked for .java files in the classpath and, given it runs on a JDK with a java compiler, do what you say. 我猜您可以编写一个调优的类加载器,该类加载器在类路径中查找.java文件,并且鉴于它在具有Java编译器的JDK上运行,请按照您说的做。

The standard JRE does not even include a compiler for Java source code. 标准JRE甚至不包含Java源代码的编译器。 To compile Java source code you need a JDK or an IDE which includes its own Java compiler. 要编译Java源代码,您需要一个JDK或一个包含自己的Java编译器的IDE。 If you compile your source code using javac or the IDE's built-in compiler, these compilers will search for source files of the referenced classes and compile them if the .class file was not found or if the source file is newer than the class file. 如果使用javac或IDE的内置编译器编译源代码,则这些编译器将搜索所引用类的源文件,并在未找到.class文件或源文件比类文件新的情况下进行编译。 But if you, eg delete the class file of the referenced class afterwards and run your program, the JVM won't search for a source file. 但是,如果您随后删除了所引用类的类文件并运行程序,则JVM将不会搜索源文件。 It will throw an NoClassDefFoundError . 它将抛出NoClassDefFoundError

JVM will not search for source file for the class whose method we are trying to invoke. JVM不会在我们试图调用其方法的类中搜索源文件。 JVM will throw a exception like: JVM将引发类似以下的异常:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Employee cannot be resolved to a type
    Employee cannot be resolved to a type
    at com.test.TestIt.main(TestIt.java:7)

Employee is the class whose method we want to call(.class file is not created for Employee) Employee是我们要调用其方法的类(未为Employee创建.class文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM