<small id='uWCuh'></small><noframes id='uWCuh'>

      <bdo id='uWCuh'></bdo><ul id='uWCuh'></ul>

  • <legend id='uWCuh'><style id='uWCuh'><dir id='uWCuh'><q id='uWCuh'></q></dir></style></legend>
    <tfoot id='uWCuh'></tfoot>
      <i id='uWCuh'><tr id='uWCuh'><dt id='uWCuh'><q id='uWCuh'><span id='uWCuh'><b id='uWCuh'><form id='uWCuh'><ins id='uWCuh'></ins><ul id='uWCuh'></ul><sub id='uWCuh'></sub></form><legend id='uWCuh'></legend><bdo id='uWCuh'><pre id='uWCuh'><center id='uWCuh'></center></pre></bdo></b><th id='uWCuh'></th></span></q></dt></tr></i><div id='uWCuh'><tfoot id='uWCuh'></tfoot><dl id='uWCuh'><fieldset id='uWCuh'></fieldset></dl></div>

      1. Java ClassLoader:两次加载相同的类

        时间:2023-10-15
          <bdo id='l2v9P'></bdo><ul id='l2v9P'></ul>

              <tfoot id='l2v9P'></tfoot>

              <legend id='l2v9P'><style id='l2v9P'><dir id='l2v9P'><q id='l2v9P'></q></dir></style></legend>

                <tbody id='l2v9P'></tbody>
              1. <small id='l2v9P'></small><noframes id='l2v9P'>

                  <i id='l2v9P'><tr id='l2v9P'><dt id='l2v9P'><q id='l2v9P'><span id='l2v9P'><b id='l2v9P'><form id='l2v9P'><ins id='l2v9P'></ins><ul id='l2v9P'></ul><sub id='l2v9P'></sub></form><legend id='l2v9P'></legend><bdo id='l2v9P'><pre id='l2v9P'><center id='l2v9P'></center></pre></bdo></b><th id='l2v9P'></th></span></q></dt></tr></i><div id='l2v9P'><tfoot id='l2v9P'></tfoot><dl id='l2v9P'><fieldset id='l2v9P'></fieldset></dl></div>
                  本文介绍了Java ClassLoader:两次加载相同的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 ClassLoader 从源文件加载由 JavaCompiler 编译的类.但是当我更改源文件,保存并重新编译时,ClassLoader 仍然会加载该类的第一个版本.

                  I have a ClassLoader which loads a class compiled by JavaCompiler from a source file. But when I change the source file, save it and recompile it, the ClassLoader still loads the first version of the class.

                     ClassLoader cl = Thread.currentThread().getContextClassLoader();
                     Class<?> compiledClass = cl.loadClass(stringClass);
                  

                  我错过了什么?比如 newInstance 什么的?

                  What am I missing? like a newInstance or something?

                  推荐答案

                  类加载器不能替换已经加载的类.loadClass 将返回现有 Class 实例的引用.

                  A classloader can't replace a class that already has been loaded. loadClass will return the reference of the existing Class instance.

                  您必须实例化一个新的类加载器并使用它来加载新类.然后,如果你想替换"这个类,你就必须扔掉这个类加载器并创建另一个新的.

                  You'll have to instantiate a new classloader and use it to load the new class. And then, if you want to "replace" the class, you'll have to throw this classloader away and create another new one.

                  回应您的评论:做类似的事情

                  In response to your comment(s): do something like

                  ClassLoader cl = new UrlClassLoader(new URL[]{pathToClassAsUrl});
                  Class<?> compiledClass = cl.loadClass(stringClass);
                  

                  该类加载器将使用默认委托父类加载器",您必须注意,该类(由其完全限定的类名标识)尚未加载并且无法由该父类加载器加载.所以pathToClassAsUrl"不应该在类路径上!

                  This classloader will use the "default delegation parent ClassLoader" and you have to take care, the class (identified by it fully qualified classname) has not been loaded and can't be loaded by that parent classloader. So the "pathToClassAsUrl" shouldn't be on the classpath!

                  这篇关于Java ClassLoader:两次加载相同的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 ECLIPSE 中调试 .class 文件? 下一篇:如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?

                  相关文章

                • <i id='4ujD8'><tr id='4ujD8'><dt id='4ujD8'><q id='4ujD8'><span id='4ujD8'><b id='4ujD8'><form id='4ujD8'><ins id='4ujD8'></ins><ul id='4ujD8'></ul><sub id='4ujD8'></sub></form><legend id='4ujD8'></legend><bdo id='4ujD8'><pre id='4ujD8'><center id='4ujD8'></center></pre></bdo></b><th id='4ujD8'></th></span></q></dt></tr></i><div id='4ujD8'><tfoot id='4ujD8'></tfoot><dl id='4ujD8'><fieldset id='4ujD8'></fieldset></dl></div>

                  • <bdo id='4ujD8'></bdo><ul id='4ujD8'></ul>

                    <small id='4ujD8'></small><noframes id='4ujD8'>

                    <legend id='4ujD8'><style id='4ujD8'><dir id='4ujD8'><q id='4ujD8'></q></dir></style></legend>

                    <tfoot id='4ujD8'></tfoot>