美文网首页
读取Class文件

读取Class文件

作者: 言西枣 | 来源:发表于2017-07-04 11:34 被阅读102次
对于已经出现在类路径上的类,如果如需要动态装载,可以使用classloader#loadClass或者Class#forname来实现。
那么对于不在类路径的.class文件,需要装载的时候就要用到URLClassLoader了。
I believe it's a ClassLoader you're after.

I suggest you start by looking at the example below which loads class files that are not on the class path.

// Create a File object on the root of the directory containing the class file
File file = new File("c:\\myclasses\\");

try {
    // Convert File to a URL
    URL url = file.toURL();          // file:/c:/myclasses/
    URL[] urls = new URL[]{url};

    // Create a new class loader with the directory
    ClassLoader cl = new URLClassLoader(urls);

    // Load in the class; MyClass.class should be located in
    // the directory file:/c:/myclasses/com/mycompany
    Class cls = cl.loadClass("com.mycompany.MyClass");
} catch (MalformedURLException e) {
} catch (ClassNotFoundException e) {
}

这里有两个点需要注意

Package name is a part of full class name, not classpath item, so you need the following:

URL[] urls = {new URL("file:/F:/badge-dao/bin")}; 
...
selectedClass = classLoader.loadClass("com.badge.dao.impl.BadgeDaoImpl"); 
In your original code classloader can find a file named BadgeDaoImpl.class in file:/F:/badge-dao/bin/com/badge/dao/impl/, but its full class name (com.badge.dao.impl.BadgeDaoImpl) doesn't match the requested one (BadgeDaoImpl), therefore classloader throws a NoClassDefFoundError. Since you are catching only ClassNotFoundException, it looks like control silently passes to the finally block. When you change folder or class names so that .class file can't be found, ClassNotFoundException is thrown as expected.
  • URL:只能包含类路径,不能包括包名,虽然包名也是以文件夹的形式呈现,但包名是类名的一部分,所以类路径与包名中的文件夹名要严格区分,类路径就是类的根目录,也就是包名的第一个部分所在的目录
  • URL与jar : URL如果以'/'(windows'')结尾,那么这个URL代表了搜索类的目录,反之代表了jar文件
From just glancing at the javadocs of [URLClassLoader](http://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html#URLClassLoader%28java.net.URL%5b%5d%29):
Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.

The URL you're specifying ends with a slash, so the class loader expects to see .class
 files in that directory. Try specifying the full path to the JAR in the URL

装载了类文件之后,这里有个坑。。

类中的各个字段

 // Caches for certain reflective results
    private static boolean useCaches = true;
    private volatile transient SoftReference<Field[]> declaredFields;
    private volatile transient SoftReference<Field[]> publicFields;
    private volatile transient SoftReference<Method[]> declaredMethods;
    private volatile transient SoftReference<Method[]> publicMethods;
    private volatile transient SoftReference<Constructor<T>[]> declaredConstructors;
    private volatile transient SoftReference<Constructor<T>[]> publicConstructors;
    // Intermediate results for getFields and getMethods
    private volatile transient SoftReference<Field[]> declaredPublicFields;
    private volatile transient SoftReference<Method[]> declaredPublicMethods;

可以看到都是软引用,目的应该是缓存
而在debug的时候,装载了类文件,在没有调用相关函数的时候,这些字段都显示为null,调用了之后才会有对象,并不是读到了空的类,这么设计的目的可能是为了方便JVM回收吧,以后再详细看看。

相关文章

  • 读取Class文件

    对于已经出现在类路径上的类,如果如需要动态装载,可以使用classloader#loadClass或者Class#...

  • ResourceBundle

    ResourceBundle读取properties文件 public class Test { public s...

  • 读取resources下的文件

    方法一: 读取文件流 ClassPathResource classPathResource =new Class...

  • JVM基础-类加载器ClassLoader

    java是逻辑程序,class是虚拟机指令程序。类加载器:将我们class文件读取到内存中。 class文件的来源...

  • TestNG通过在.txt文件中读取参数,测试加法

    public class Exercise { //读取文件的函数 public static Map> read...

  • JAVA学习笔记0x03

    类加载 把.class文件从硬盘读取到内存(jvm)中,将这个过程称为类加载 jvm搜索.class文件位置的顺序...

  • netty实战二之HTTP服务端开发

    Netty http服务端 文件读取服务 HttpFileServerHandler.class HttpFile...

  • ClassLoader解析——Java篇

    我们知道,Java程序的启动,其实是依赖于我们编译之后的.class文件,那么JVM是如何读取.class文件的?...

  • 类加载机制

    类加载也就是JVM"读取"class文件,"class文件"不一定是以文件方式存储在磁盘的,也可能是从网络中接收到...

  • Java类加载器

    1 类加载 Java代码编译成Class文件后,需要放到内存中运行,而把Class文件读取到内存的操作就是类加载器...

网友评论

      本文标题:读取Class文件

      本文链接:https://www.haomeiwen.com/subject/mwithxtx.html