Skip to main content

Java ClassLoader 学习反馈

Classloader是Java运行时核心机制之一,它解决了我们的.class文件如何被jvm加载的问题。也就是我们的写的源码.java文件被编译成.class字节码后,如何被jvm加载解释。
Java在广义上有三种classloader
  1. BootstrapClassLoader 是JVM级别的classloader,使用c/c++来写的。详细解释如何来加载一个bootstrap级别的class文件,哪些class文件是bootstrapclassloader来加载的。
  2. ExtClassLoader  是ExtensionClassLoader,他是AppClassLoader的 ‘父类’,这个父类不是指AppClassLoader extends ExtClassLoader,而是指ExtClassLoader来创建了AppClassloader,后面源码部分可以解释这个关系。
  3. AppClassLoader 这是一般我们自己写的class的classloader,他加载了我们自己写的class,而且同时如果我们有自定义的classloader,那么他也是custom class loader的parent class loader.
他们的加载顺序也是从上往下,即 1->2 ->3。
  到这里可能会产生疑问,他们加载的class有何不同? 
AppClassLoader是上面说的,他会加载我们自己定义的class,即我们workspace的classpath下的class
ExtClassLoader则是加载我们Jdk lib/ext 下的class
BootstrapClassLoader是加载rt.jar、resources.jar、charsets.jar和class等。 

那么他们是如何找到lib下这些class文件呢? 答案是通过环境变量中的一些变量,而这些变量的定义也是通过我们设置的JAVA HOME等来取得。
源码中有这么两个地方解释了这个地方。
==》 private static String bootClassPath = System.getProperty("sun.boot.class.path");
==》 String s = System.getProperty("java.ext.dirs");

蓝色部分的是定义了BootstrapClassLoader如何找到他需要的那些class所在的目录。
红色部分则是定义了ExtClassLoader如何找到他需要的class

挑选第一个,我们起一个main函数来做一个测试。
System.out.println(System.getProperty("sun.boot.class.path"));
==>>>
C:\Program Files\Java\jdk1.8.0\jre\lib\resources.jar;
C:\Program Files\Java\jdk1.8.0\jre\lib\rt.jar;
C:\Program Files\Java\jdk1.8.0\jre\lib\sunrsasign.jar;
C:\Program Files\Java\jdk1.8.0\jre\lib\jsse.jar;
C:\Program Files\Java\jdk1.8.0\jre\lib\jce.jar;
C:\Program Files\Java\jdk1.8.0\jre\lib\charsets.jar;
C:\Program Files\Java\jdk1.8.0\jre\lib\jfr.jar;
C:\Program Files\Java\jdk1.8.0\jre\classes
  这就是classloader如何加载class的方式。

  • 找到一个指定的路径
  • 加载路径下指定的class文件





Comments

Popular posts from this blog

Scrapy ERROR :ImportError: Error loading object 'scrapy.telnet.TelnetConsole': No module named conch

原文: https://stackoverflow.com/questions/17263509/error-while-using-scrapy-scrapy-telnet-telnetconsole-no-module-named-conch/17264705#17264705 On Ubuntu, you should avoid using  easy_install  wherever you can. Instead, you should be using  apt-get ,  aptitude , "Ubuntu Software Center", or another of the distribution-provided tools. For example, this single command is all you need to install scrapy - along with every one of its dependencies that is not already installed: $ sudo apt - get install python - scrapy easy_install  is not nearly as good at installing things as  apt-get . Chances are the reason you can't get it to work is that it didn't quite install things sensibly, particularly with respect to what was already installed on the system. Sadly, it also leaves no record of what it did, so uninstallation is difficult or impossible. You may now have a big mess on your system that prevents proper installations from working as well (or maybe not...

Elasticsearch error when the field exceed limit 32kb

When we post data that the field exceed the limit, elasticsearch will reject the data which error: {"error":{"root_cause":[{"type":"remote_transport_exception","reason":"[cs19-2][10.200.20.39:9301][indices:data/write/index]"}],"type":"illegal_argument_exception","reason":"Document contains at least one immense term in field=\"field1\" (whose UTF8 encoding is longer than the max length 32766), all of which were skipped.  Please correct the analyzer to not produce such terms.  The prefix of the first immense term is You can handle this: 1. you can udpate the mapping part at any time PUT INDEXNAME/_mapping/TYPENAME { "properties" : { "logInfo" : { "type" : "string" , "analyzer" : "keyword" , "ignore_above" : 32766 } } } ignore_above means that we will only keep 32766 bytes 2...

Python 使用socket实现ftp 客服端

之前先了解ftp协议,然后解释代码 连接到ftp服务器,得到一个socket(这是一个连接到ftp命令端口socket) 发送必要request 第一步 Connect到服务器后,ftp_socket.recv(1024) 到服务器的欢迎消息(1 中的socket),不要问为什么,ftp协议规定的,应该是。 *注意的是,后面ftp_socket每一次的请求后,都要recv一次,不管你是否全部接受到了都要recv一次,不然可能后面接受不到一些消息。个人觉着这可能是ftp协议的规定:每一次request,都会给client一个response。如果 client没有接受这个response,那么下次的request不会被服务器接受,所以client的recv就会卡住! 第二步就像代码中 直到 #LIST 都是用的命令端口。 而使用数据端口时,就是用命令端口   ftp_socket.send("PASV \r\n")     new_port = ftp_socket.recv(1024)      使用命令 PASV 请求一个动态的数据端口。 解释 我理解的动态数据端口: 即你每一次请求到一个数据端口后,你只能使用一次。比如: [ INFO] 2014-11-29 22:25:44,682 [admin] [127.0.0.1] SENT: 227 Entering Passive Mode (127,0,0,1,204,82)    这是ftp服务器端发送的请求(apache ftpserver),明显括号中是你的ip(我的是本机),然后两个数字。通过查询,你要请求的数据端口:(a,b,c,d,x,y)  new_port  = x*250+y 剩下的部分就很简单了。在代码中再有点解释(后面附server端log) 最后还应该用发送一个quit命令,告诉server 我的请求完毕了。这里忘了。 一点补充:看到server端log可以发现,服务器每一次的SEND  我们都应该recv一次 #download a folder's files  import socket class ParseUrl()...