classloader

类加载器ClassLoader-1

老子叫甜甜 提交于 2019-11-30 01:11:45
一, 类加载器深入剖析 1,Java虚拟机与程序的生命周期 在如下几种情况下,Java虚拟机将结束生命周期: –执行了System.exit()方法 –程序正常执行结束 –程序在执行过程中遇到了异常或错误而异常终止 –由于操作系统出现错误而导致Java虚拟机进程终止 2,类的加载,链接,初始化 概念: •加载:查找并加载类的二进制数据(java编译后的.class文件) •连接 –验证:确保被加载的类的正确性 –准备:为类的静态变量分配内存,并将其初始化为默认值 –解析:把类中的符号引用转换为直接引用 •初始化:为类的静态变量赋予正确的初始值(=号后面的值) 图示: 初始化的条件: •Java程序对类的使用方式可分为两种 –主动使用 –被动使用 •所有的Java虚拟机实现必须在每个类或接口被Java程序“首次主动使用”时才初始化他们 •主动使用(六种) –创建类的实例 –访问某个类或接口的静态变量,或者对该静态变量赋值 –调用类的静态方法 –反射(如Class.forName(“com.shengsiyuan.Test”)) –初始化一个类的子类 –Java虚拟机启动时被标明为启动类的类(Java Test),就是用java命令执行的那个带有main方法入口的类。 被动使用 除了以上六种情况,其他使用Java类的方式都被看作是对类的被动使用,都不会导致类的初始化。 类的加载: 1

How is the Java Bootstrap Classloader loaded? [duplicate]

做~自己de王妃 提交于 2019-11-29 23:51:09
This question already has an answer here: What loads the java system classloader? 4 answers In java, it is said that all the classes are being loaded by classloaders. So first of all, bootstrap classloader loads all the rt.jar classes. So I am still confused as Classloader is also a class, so who will load this BootStrapClassloader. Kindly explain. Answer : When a JVM starts up, a special chunk of machine code runs that loads the system classloader. This machine code is known as the Bootstrap / Primordial (or sometimes - Null ) classloader. It is not a Java class at all, as are all other

Loading classes not present in the classpath

五迷三道 提交于 2019-11-29 22:58:39
问题 Let's say I've compiled a Groovy script using Groovyc, which has generated one or more .class files in the file system. From a Java application, how do I add those classes to the classpath dynamically in order to load them and call their methods? The goal is to pre-compile Groovy scripts and store them into the database, so evaluation can be performed from compiled versions of the scripts. 回答1: You can create an instance of URLClassLoader to load new classes from a directory: URL dirUrl = new

JVM-ClassLoader

别等时光非礼了梦想. 提交于 2019-11-29 22:52:19
<谭锋>整理 为了支持跨平台的特性,java语言采用源代码编译成中间字节码,然后又各平台的jvm解释执行的方式。字节码采用了完全与平台无关的方式进行描述,java只给出了字节码格式的规范,并没有规定字节码最终来源是什么,它可以是除了java语言外的其他语言产生,只要是满足字节码规范的,都可以在jvm中很好的运行。正因为这个特性,极大的促进了各类语言的发展,在jvm平台上出现了很多语言,如scala,groovy等 由于字节码来源并没有做限制,因此jvm必须在字节码正式使用之前,即在加载过程中,对字节码进行检查验证,以保证字节码的可用性和安全性。 1. jvm运行时内存结构划分 在正式介绍之前,先看看jvm内存结构划分: 结合垃圾回收机制,将堆细化: 在加载阶段主要用到的是方法区: 方法区是可供各条线程共享的运行时内存区域。存储了每一个类的结构信息,例如运行时常量池( Runtime Constant Pool )、字段和方法数据、构造函数和普通方法的字节码内容、还包括一些在类、实例、接口初始化时用到的特殊方法 。 如果把方法的代码看作它的“静态”部分,而把一次方法调用需要记录的临时数据看做它的“动态”部分,那么每个方法的代码是只有一份的,存储于JVM的方法区中;每次某方法被调用,则在该调用所在的线程的的Java栈上新分配一个栈帧,用于存放临时数据,在方法返回时栈帧自动撤销。 2.

Can i deny access to a jvm class by configuring java.policy file?

ぃ、小莉子 提交于 2019-11-29 22:44:47
问题 I wanted to add to my jdk6\jre\lib\security\java.policy file an interdiction to create some classes that are blacklisted by appengine. For example I want my local jvm to throw an exception when the application tries to instantiate javax.naming.NamingException . It is possible? I will try to explain my specific problem here. Google offers an service (GAE-google app engine) that has some limitations on what classes can be used. For example doesn't instantiate JNDI classes that are in javax

Is it possible to have the System ClassLoader load .class files specified at run time?

雨燕双飞 提交于 2019-11-29 21:16:39
问题 I am writing a static analysis tool for an assignment, it analyses Java bytecode using the ASM library. One of the parts of ASM that we use requires (or at least, appears to require) that the class be loaded from the ClassLoader. We were hoping the tool would be able to analyse .class files without requiring them on the classpath. We already load the .classes from a specified directory at run time and read them in using an InputStream. This is acceptable for ASM in most cases. There are some

What is the use of Custom Class Loader

冷暖自知 提交于 2019-11-29 21:06:08
Recently I came accross the java custom class loader api. I found one use over here, kamranzafar's blog I am a bit new to the class loader concept. Can any one explain in detail, what are the different scenarios where we may need it or we should use it? Custom class loaders are useful in larger architectures consisting of several module/applications. Here are the advantages of the custom class loader: Provides Modular architecture Allows to define multiple class loader allowing modular architecture. Avoiding conflicts Clearly defines the scope of the class to within the class loader. Support

In what order are the different parts of a class initialized when a class is loaded in the JVM?

痴心易碎 提交于 2019-11-29 20:30:32
问题 Imagine a Java class which has most features that you can find in a class. For example: it inherits from another class, implements a couple of interfaces, includes some 'static final' constants, some final constants, some static variables, instance variables, a static block, an unnamed code block (just code in {}), constructors, methods etc. When the class in question is loaded into the JVM for the first time, in what order are the various portions of the class initialized or loaded into the

How do I create a ClassLoader that will not search the parent for loading classes

六月ゝ 毕业季﹏ 提交于 2019-11-29 19:54:57
问题 I think I understand how class-loading hierarchies work. (the JVM looks into the parent hierarchy first) So I would like to create a ClassLoader, or use an existing library, that is a completely separate scope, and doesn't look at the parent ClassLoading hierarchy. Actually I'm looking for the same effect of launching a separate JVM, but without literally doing so. I'm confident this is possible, but surprised it was so hard to find a simple example of how to do that. 回答1: Simply use the

Java: Difference between Class.forName and ClassLoader.loadClass

﹥>﹥吖頭↗ 提交于 2019-11-29 19:32:43
Recently came across some code that got me thinking. What's the difference between: Class theClass = Class.forName("SomeImpl"); SomeImpl impl = (SomeImpl)theClass.newInstance(); and: Class theClass = ClassLoader.loadClass("SomeImpl"); SomeImpl impl = (SomeImpl)theClass.newInstance(); Are they synonymous? Is one preferable to the other in certain circumstances? What are the do's and dont's to using these two methods? Thanks in advance. Class.forName() will always use the ClassLoader of the caller, whereas ClassLoader.loadClass() can specify a different ClassLoader. I believe that Class.forName