- 浏览: 268955 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (70)
- 杂谈 (1)
- 有趣it转载 (1)
- JavaEE (4)
- 备忘 (0)
- linux (6)
- Spring (5)
- hibernate (2)
- IntelliJ IDEA (1)
- mysql (3)
- python (5)
- django (2)
- git (2)
- ActiveMQ (2)
- web前端 (1)
- maven (1)
- javascript (1)
- suds (1)
- svn (1)
- apache (2)
- windows (1)
- eclipse (1)
- JVM (1)
- 设计模式 (1)
- RAID (1)
- jstemplate (3)
- jquery (1)
- JavaEE,poi (0)
- poi (1)
- java (2)
- mongodb (2)
- struts (1)
- 项目管理 (2)
- 算法 (2)
- 算法d (0)
最新评论
-
wzwahl36:
http://www.atool.org/crontab.ph ...
crontab表达式 -
quding0308:
感谢感谢
利用github构建自己的代码库 -
sailinglee:
文不对题
mongoose简单使用样例 -
erickdu888:
真是个好东西
python使用suds调用webservice
class ClassA{ public void method(){ System.out.println("Loading ClassA"); } } public class ClassLoaderTest { public static void main(String args[]){ TestClassA testClassA = new TestClassA(); testClassA.method(); } }
编译,执行
java -verbose:class ClassLoaderTest
[Loaded java.lang.Object from shared objects file] [Loaded java.io.Serializable from shared objects file] [Loaded java.lang.Comparable from shared objects file] [Loaded java.lang.CharSequence from shared objects file] [Loaded java.lang.String from shared objects file] [Loaded java.lang.reflect.GenericDeclaration from shared objects file] [Loaded java.lang.reflect.Type from shared objects file] [Loaded java.lang.reflect.AnnotatedElement from shared objects file] [Loaded java.lang.Class from shared objects file] [Loaded java.lang.Cloneable from shared objects file] [Loaded java.lang.ClassLoader from shared objects file] [Loaded java.lang.System from shared objects file] [Loaded java.lang.Throwable from shared objects file] [Loaded java.lang.Error from shared objects file] [Loaded java.lang.ThreadDeath from shared objects file] [Loaded java.lang.Exception from shared objects file] [Loaded java.lang.RuntimeException from shared objects file] [Loaded java.security.ProtectionDomain from shared objects file] [Loaded java.security.AccessControlContext from shared objects file] [Loaded java.lang.ClassNotFoundException from shared objects file] [Loaded java.lang.LinkageError from shared objects file] [Loaded java.lang.NoClassDefFoundError from shared objects file] [Loaded java.lang.ClassCastException from shared objects file] [Loaded java.lang.ArrayStoreException from shared objects file] [Loaded java.lang.VirtualMachineError from shared objects file] [Loaded java.lang.OutOfMemoryError from shared objects file] [Loaded java.lang.StackOverflowError from shared objects file] [Loaded java.lang.IllegalMonitorStateException from shared objects file] [Loaded java.lang.ref.Reference from shared objects file] [Loaded java.lang.ref.SoftReference from shared objects file] [Loaded java.lang.ref.WeakReference from shared objects file] [Loaded java.lang.ref.FinalReference from shared objects file] [Loaded java.lang.ref.PhantomReference from shared objects file] [Loaded java.lang.ref.Finalizer from shared objects file] [Loaded java.lang.Runnable from shared objects file] [Loaded java.lang.Thread from shared objects file] [Loaded java.lang.Thread$UncaughtExceptionHandler from shared objects file] [Loaded java.lang.ThreadGroup from shared objects file] [Loaded java.util.Dictionary from shared objects file] [Loaded java.util.Map from shared objects file] [Loaded java.util.Hashtable from shared objects file] [Loaded java.util.Properties from shared objects file] [Loaded java.lang.reflect.AccessibleObject from shared objects file] [Loaded java.lang.reflect.Member from shared objects file] [Loaded java.lang.reflect.Field from shared objects file] [Loaded java.lang.reflect.Method from shared objects file] [Loaded java.lang.reflect.Constructor from shared objects file] [Loaded sun.reflect.MagicAccessorImpl from shared objects file] [Loaded sun.reflect.MethodAccessor from shared objects file] [Loaded sun.reflect.MethodAccessorImpl from shared objects file] [Loaded sun.reflect.ConstructorAccessor from shared objects file] [Loaded sun.reflect.ConstructorAccessorImpl from shared objects file] [Loaded sun.reflect.DelegatingClassLoader from shared objects file] [Loaded sun.reflect.ConstantPool from shared objects file] [Loaded sun.reflect.FieldAccessor from shared objects file] [Loaded sun.reflect.FieldAccessorImpl from shared objects file] [Loaded sun.reflect.UnsafeFieldAccessorImpl from shared objects file] [Loaded sun.reflect.UnsafeStaticFieldAccessorImpl from shared objects file] [Loaded java.lang.Appendable from shared objects file] [Loaded java.lang.AbstractStringBuilder from shared objects file] [Loaded java.lang.StringBuffer from shared objects file] [Loaded java.lang.StringBuilder from shared objects file] [Loaded java.lang.StackTraceElement from shared objects file] [Loaded java.nio.Buffer from shared objects file] [Loaded sun.misc.AtomicLong from shared objects file] [Loaded sun.misc.AtomicLongCSImpl from shared objects file] [Loaded java.lang.Boolean from shared objects file] [Loaded java.lang.Character from shared objects file] [Loaded java.lang.Number from shared objects file] [Loaded java.lang.Float from shared objects file] [Loaded java.lang.Double from shared objects file] [Loaded java.lang.Byte from shared objects file] [Loaded java.lang.Short from shared objects file] [Loaded java.lang.Integer from shared objects file] [Loaded java.lang.Long from shared objects file] [Loaded java.io.ObjectStreamField from shared objects file] [Loaded java.util.Comparator from shared objects file] [Loaded java.lang.String$CaseInsensitiveComparator from shared objects file] [Loaded java.security.Guard from shared objects file] [Loaded java.security.Permission from shared objects file] [Loaded java.security.BasicPermission from shared objects file] [Loaded java.lang.RuntimePermission from shared objects file] [Loaded java.util.AbstractMap from shared objects file] [Loaded sun.misc.SoftCache from shared objects file] [Loaded java.lang.ref.ReferenceQueue from shared objects file] [Loaded java.lang.ref.ReferenceQueue$Null from shared objects file] [Loaded java.lang.ref.ReferenceQueue$Lock from shared objects file] [Loaded java.util.HashMap from shared objects file] [Loaded java.util.Map$Entry from shared objects file] [Loaded java.util.HashMap$Entry from shared objects file] [Loaded java.security.AccessController from shared objects file] [Loaded java.lang.reflect.ReflectPermission from shared objects file] [Loaded java.security.PrivilegedAction from shared objects file] [Loaded sun.reflect.ReflectionFactory$GetReflectionFactoryAction from shared objects file] [Loaded java.lang.Iterable from shared objects file] [Loaded java.util.Collection from shared objects file] [Loaded java.util.AbstractCollection from shared objects file] [Loaded java.util.List from shared objects file] [Loaded java.util.AbstractList from shared objects file] [Loaded java.util.RandomAccess from shared objects file] [Loaded java.util.Vector from shared objects file] [Loaded java.util.Stack from shared objects file] [Loaded sun.reflect.ReflectionFactory from shared objects file] [Loaded java.lang.ref.Reference$Lock from shared objects file] [Loaded java.lang.ref.Reference$ReferenceHandler from shared objects file] [Loaded java.lang.ref.Finalizer$FinalizerThread from shared objects file] [Loaded java.util.Enumeration from shared objects file] [Loaded java.util.Hashtable$EmptyEnumerator from shared objects file] [Loaded java.util.Iterator from shared objects file] [Loaded java.util.Hashtable$EmptyIterator from shared objects file] [Loaded java.util.Hashtable$Entry from shared objects file] [Loaded java.nio.charset.Charset from shared objects file] [Loaded java.nio.charset.spi.CharsetProvider from shared objects file] [Loaded sun.nio.cs.FastCharsetProvider from shared objects file] [Loaded sun.nio.cs.StandardCharsets from shared objects file] [Loaded sun.util.PreHashedMap from shared objects file] [Loaded sun.nio.cs.StandardCharsets$Aliases from shared objects file] [Loaded sun.nio.cs.StandardCharsets$Classes from shared objects file] [Loaded sun.nio.cs.StandardCharsets$Cache from shared objects file] [Loaded java.lang.ThreadLocal from shared objects file] [Loaded java.util.concurrent.atomic.AtomicInteger from shared objects file] [Loaded sun.misc.Unsafe from shared objects file] [Loaded java.lang.IncompatibleClassChangeError from shared objects file] [Loaded java.lang.NoSuchMethodError from shared objects file] [Loaded sun.reflect.Reflection from shared objects file] [Loaded java.util.Collections from shared objects file] [Loaded java.util.Set from shared objects file] [Loaded java.util.AbstractSet from shared objects file] [Loaded java.util.Collections$EmptySet from shared objects file] [Loaded java.util.Collections$EmptyList from shared objects file] [Loaded java.util.Collections$EmptyMap from shared objects file] [Loaded java.util.Collections$ReverseComparator from shared objects file] [Loaded java.util.Collections$SynchronizedMap from shared objects file] [Loaded java.lang.Class$3 from shared objects file] [Loaded java.lang.reflect.Modifier from shared objects file] [Loaded sun.reflect.LangReflectAccess from shared objects file] [Loaded java.lang.reflect.ReflectAccess from shared objects file] [Loaded java.lang.StringValue from shared objects file] [Loaded java.util.Arrays from shared objects file] [Loaded java.lang.Math from shared objects file] [Opened C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar] [Loaded java.nio.charset.Charset$3 from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar] [Opened C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar] [Loaded sun.nio.cs.AbstractCharsetProvider from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar] [Loaded sun.nio.cs.ext.ExtendedCharsets from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar] [Loaded java.lang.Class$1 from shared objects file] [Loaded sun.reflect.ReflectionFactory$1 from shared objects file] [Loaded sun.reflect.NativeConstructorAccessorImpl from shared objects file] [Loaded sun.reflect.DelegatingConstructorAccessorImpl from shared objects file] [Loaded java.util.SortedMap from shared objects file] [Loaded java.util.NavigableMap from shared objects file] [Loaded java.util.TreeMap from shared objects file] [Loaded sun.misc.ASCIICaseInsensitiveComparator from shared objects file] [Loaded java.util.TreeMap$Entry from shared objects file] [Loaded sun.misc.VM from shared objects file] [Loaded sun.nio.cs.ext.GBK from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar] [Loaded java.lang.StringCoding from shared objects file] [Loaded java.lang.ThreadLocal$ThreadLocalMap from shared objects file] [Loaded java.lang.ThreadLocal$ThreadLocalMap$Entry from shared objects file] [Loaded java.lang.StringCoding$StringDecoder from shared objects file] [Loaded java.nio.charset.CharsetDecoder from shared objects file] [Loaded sun.nio.cs.ext.DoubleByteDecoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar] [Loaded sun.nio.cs.ext.GBK$Decoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar] [Loaded java.nio.charset.CodingErrorAction from shared objects file] [Loaded java.nio.ByteBuffer from shared objects file] [Loaded java.nio.HeapByteBuffer from shared objects file] [Loaded java.nio.Bits from shared objects file] [Loaded java.nio.ByteOrder from shared objects file] [Loaded java.lang.Readable from shared objects file] [Loaded java.nio.CharBuffer from shared objects file] [Loaded java.nio.HeapCharBuffer from shared objects file] [Loaded java.nio.charset.CoderResult from shared objects file] [Loaded java.nio.charset.CoderResult$Cache from shared objects file] [Loaded java.nio.charset.CoderResult$1 from shared objects file] [Loaded java.nio.charset.CoderResult$2 from shared objects file] [Loaded sun.misc.Version from shared objects file] [Loaded sun.misc.JavaLangAccess from shared objects file] [Loaded java.lang.System$2 from shared objects file] [Loaded sun.misc.SharedSecrets from shared objects file] [Loaded java.lang.Runtime from shared objects file] [Loaded java.io.File from shared objects file] [Loaded java.io.FileSystem from shared objects file] [Loaded java.io.Win32FileSystem from shared objects file] [Loaded java.io.WinNTFileSystem from shared objects file] [Loaded java.io.ExpiringCache from shared objects file] [Loaded java.util.LinkedHashMap from shared objects file] [Loaded java.io.ExpiringCache$1 from shared objects file] [Loaded java.util.LinkedHashMap$Entry from shared objects file] [Loaded sun.security.action.GetPropertyAction from shared objects file] [Loaded sun.jkernel.DownloadManager from shared objects file] [Loaded sun.jkernel.DownloadManager$1 from shared objects file] [Loaded sun.jkernel.DownloadManager$2 from shared objects file] [Loaded java.lang.ClassLoader$3 from shared objects file] [Loaded java.io.ExpiringCache$Entry from shared objects file] [Loaded java.lang.ClassLoader$NativeLibrary from shared objects file] [Loaded java.lang.StringCoding$StringEncoder from shared objects file] [Loaded java.nio.charset.CharsetEncoder from shared objects file] [Loaded sun.nio.cs.ext.DoubleByteEncoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar] [Loaded sun.nio.cs.ext.GBK$Encoder from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\charsets.jar] [Loaded sun.nio.cs.Surrogate$Parser from shared objects file] [Loaded sun.nio.cs.Surrogate from shared objects file] [Loaded java.io.Closeable from shared objects file] [Loaded java.io.InputStream from shared objects file] [Loaded java.io.FileInputStream from shared objects file] [Loaded java.io.FileDescriptor from shared objects file] [Loaded java.io.Flushable from shared objects file] [Loaded java.io.OutputStream from shared objects file] [Loaded java.io.FileOutputStream from shared objects file] [Loaded java.io.FilterInputStream from shared objects file] [Loaded java.io.BufferedInputStream from shared objects file] [Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater from shared objects file] [Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl from shared objects file] [Loaded sun.reflect.misc.ReflectUtil from shared objects file] [Loaded java.io.FilterOutputStream from shared objects file] [Loaded java.io.PrintStream from shared objects file] [Loaded java.io.BufferedOutputStream from shared objects file] [Loaded java.io.Writer from shared objects file] [Loaded java.io.OutputStreamWriter from shared objects file] [Loaded sun.nio.cs.StreamEncoder from shared objects file] [Loaded java.io.BufferedWriter from shared objects file] [Loaded java.lang.Terminator from shared objects file] [Loaded sun.misc.SignalHandler from shared objects file] [Loaded java.lang.Terminator$1 from shared objects file] [Loaded sun.misc.Signal from shared objects file] [Loaded sun.misc.NativeSignalHandler from shared objects file] [Loaded sun.misc.OSEnvironment from shared objects file] [Loaded sun.io.Win32ErrorMode from shared objects file] [Loaded java.lang.NullPointerException from shared objects file] [Loaded java.lang.ArithmeticException from shared objects file] [Loaded java.lang.Compiler from shared objects file] [Loaded java.lang.Compiler$1 from shared objects file] [Loaded sun.misc.Launcher from shared objects file] [Loaded java.net.URLStreamHandlerFactory from shared objects file] [Loaded sun.misc.Launcher$Factory from shared objects file] [Loaded java.security.SecureClassLoader from shared objects file] [Loaded java.net.URLClassLoader from shared objects file] [Loaded sun.misc.Launcher$ExtClassLoader from shared objects file] [Loaded sun.security.util.Debug from shared objects file] [Loaded java.lang.Package from shared objects file] [Loaded java.net.URL from shared objects file] [Loaded java.security.CodeSource from shared objects file] [Loaded java.lang.Void from shared objects file] [Loaded java.security.cert.Certificate from shared objects file] [Loaded java.lang.ClassFormatError from shared objects file] [Loaded java.lang.IllegalArgumentException from shared objects file] [Loaded java.io.IOException from shared objects file] [Loaded sun.misc.URLClassPath from shared objects file] [Loaded java.lang.AssertionStatusDirectives from shared objects file] [Loaded sun.misc.JavaNetAccess from shared objects file] [Loaded java.net.URLClassLoader$7 from shared objects file] [Loaded java.security.PermissionCollection from shared objects file] [Loaded java.util.StringTokenizer from shared objects file] [Loaded java.security.PrivilegedExceptionAction from shared objects file] [Loaded sun.misc.Launcher$ExtClassLoader$1 from shared objects file] [Loaded sun.misc.MetaIndex from shared objects file] [Loaded java.io.Reader from shared objects file] [Loaded java.io.BufferedReader from shared objects file] [Loaded java.io.InputStreamReader from shared objects file] [Loaded java.io.FileReader from shared objects file] [Loaded sun.nio.cs.StreamDecoder from shared objects file] [Loaded java.util.ArrayList from shared objects file] [Loaded java.lang.reflect.Array from shared objects file] [Loaded java.util.Locale from shared objects file] [Loaded java.util.concurrent.ConcurrentMap from shared objects file] [Loaded java.util.concurrent.ConcurrentHashMap from shared objects file] [Loaded java.util.concurrent.locks.Lock from shared objects file] [Loaded java.util.concurrent.locks.ReentrantLock from shared objects file] [Loaded java.util.concurrent.ConcurrentHashMap$Segment from shared objects file] [Loaded java.util.concurrent.locks.AbstractOwnableSynchronizer from shared objects file] [Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer from shared objects file] [Loaded java.util.concurrent.locks.ReentrantLock$Sync from shared objects file] [Loaded java.util.concurrent.locks.ReentrantLock$NonfairSync from shared objects file] [Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer$Node from shared objects file] [Loaded java.util.concurrent.ConcurrentHashMap$HashEntry from shared objects file] [Loaded java.lang.CharacterDataLatin1 from shared objects file] [Loaded java.io.ObjectStreamClass from shared objects file] [Loaded sun.net.www.ParseUtil from shared objects file] [Loaded java.util.BitSet from shared objects file] [Loaded java.net.Parts from shared objects file] [Loaded java.net.URLStreamHandler from shared objects file] [Loaded sun.net.www.protocol.file.Handler from shared objects file] [Loaded java.util.HashSet from shared objects file] [Loaded sun.net.www.protocol.jar.Handler from shared objects file] [Loaded sun.misc.Launcher$AppClassLoader from shared objects file] [Loaded sun.misc.Launcher$AppClassLoader$1 from shared objects file] [Loaded java.lang.SystemClassLoaderAction from shared objects file] [Loaded java.net.URLClassLoader$1 from shared objects file] [Loaded sun.net.util.URLUtil from shared objects file] [Loaded sun.misc.URLClassPath$3 from shared objects file] [Loaded sun.misc.URLClassPath$Loader from shared objects file] [Loaded sun.misc.URLClassPath$JarLoader from shared objects file] [Loaded java.security.PrivilegedActionException from shared objects file] [Loaded sun.misc.URLClassPath$FileLoader from shared objects file] [Loaded sun.misc.Resource from shared objects file] [Loaded sun.misc.URLClassPath$FileLoader$1 from shared objects file] [Loaded sun.nio.ByteBuffered from shared objects file] [Loaded java.security.Permissions from shared objects file] [Loaded java.net.URLConnection from shared objects file] [Loaded sun.net.www.URLConnection from shared objects file] [Loaded sun.net.www.protocol.file.FileURLConnection from shared objects file] [Loaded java.net.ContentHandler from shared objects file] [Loaded java.net.UnknownContentHandler from shared objects file] [Loaded sun.net.www.MessageHeader from shared objects file] [Loaded java.io.FilePermission from shared objects file] [Loaded java.io.FilePermission$1 from shared objects file] [Loaded java.security.Policy from shared objects file] [Loaded sun.security.provider.PolicyFile from shared objects file] [Loaded java.security.Policy$UnsupportedEmptyCollection from shared objects file] [Loaded java.io.FilePermissionCollection from shared objects file] [Loaded java.security.AllPermission from shared objects file] [Loaded java.security.UnresolvedPermission from shared objects file] [Loaded java.security.BasicPermissionCollection from shared objects file] [Loaded sun.misc.JavaSecurityProtectionDomainAccess from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar] [Loaded java.security.ProtectionDomain$2 from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar] [Loaded java.security.ProtectionDomain$Key from C:\Program Files (x86)\Java\jdk1.6.0_21\jre\lib\rt.jar] [Loaded java.security.Principal from shared objects file] [Loaded ClassLoaderTest from file:/F:/Desktop/] [Loaded ClassA from file:/F:/Desktop/] Loading ClassA [Loaded java.lang.Shutdown from shared objects file] [Loaded java.lang.Shutdown$Lock from shared objects file]
由上可以看到,jvm将常用类加载到系统,这样可以加快运行的效率。
这引出了加载类的两种类型:
- 预先加载( pre-loading ) 可以加快运行效率,和io损耗
- 按需加载( load-on-demand ) 可以减少内存消耗
系统常用类采用预加载方式加载,而用户自定义的类就采用按需加载的方式。而加载一个类进入内存的时机便是调用构造函数时。
从加载形式来看,又分为:
- 隐式加载( implicit )程序中用 new 关键字来定义一个实例变量, JRE 在执行到 new 关键字的时候就会把对应的实例类加载进入内存
- 显示加载( explicit )利用java提供的加载方法,通过代码方式显示的加载class至内存
显示加载的例子:
class TestClass{ public void method(){ System.out.println("TestClass-method"); } } public class CLTest { public static void main(String args[]) { try{ Class c = Class.forName("TestClass"); TestClass object = (TestClass)c.newInstance(); object.method(); }catch(Exception e){ e.printStackTrace(); } } }
我们通过 Class 类的 forName (String s) 方法把自定义类 TestClass 加载进来,并通过 newInstance ()方法把实例初始化。
Class 的 forName() 方法还有另外一种形式:
Class forName(String s, boolean flag, ClassLoader classloader), s 表示需要加载类的名称, flag 表示在调用该函数加载类的时候是否初始化静态区, classloader 表示加载该类所需的加载器。
forName (String s) 是默认通过 ClassLoader.getCallerClassLoader() 调用类加载器的,但是该方法是私有方法,我们无法调用,如果我们想使用 Class forName(String s, boolean flag, ClassLoader classloader) 来加载类的话,就必须要指定类加载器,可以通过如下的方式来实现:
Test test = new Test();//Test 类为自定义的一个测试类;
ClassLoader cl = test. getClass().getClassLoader(); // 获取 test 的类装载器;
Class c = Class.forName("TestClass", true, cl);
因为一个类要加载就必需要有加载器,这里我们是通过获取加载 Test 类的加载器 cl 当作加载 TestClass 的类加载器来实现加载的。
这些class必须在classpath下面。
如果不在classpath下,还想要加载class进来,就需要用到class加载的另一种方式。
- 自定义加载机制
try{ URL url = new URL("file:/d:/test/lib/"); URLClassLoader urlCL = new URLClassLoader(new URL[]{url}); Class c = urlCL.loadClass("TestClassA"); TestClassA object = (TestClassA)c.newInstance(); object.method(); }catch(Exception e){ e.printStackTrace(); }
类加载器的体系结构
这里有一个加载的顺序问题,也就是类加载器的代理机制。
类加载器在尝试自己去查找某个类的字节代码并定义它时,会先代理给其父类加载器,由父类加载器先去尝试加载这个类,依次类推。在介绍代理模式背后的动机之前,首先需要说明一下 Java 虚拟机是如何判定两个 Java 类是相同的。Java 虚拟机不仅要看类的全名是否相同,还要看加载此类的类加载器是否一样。只有两者都相同的情况,才认为两个类是相同的。即便是同样的字节代码,被不同的类加载器加载之后所得到的类,也是不同的。试图对这两个类的对象进行相互赋值,会抛出运行时异常
ClassCastException
。当执行 java ***.class 的时候, java.exe 会帮助我们找到 JRE ,接着找到位于 JRE 内部的 jvm.dll ,这才是真正的 Java 虚拟机器 , 最后加载动态库,激活 Java 虚拟机器。虚拟机器激活以后,会先做一些初始化的动作,比如说读取系统参数等。一旦初始化动作完成之后,就会产生第一个类加载器―― Bootstrap Loader , Bootstrap Loader 是由 C++ 所撰写而成,这个 Bootstrap Loader 所做的初始工作中,除了一些基本的初始化动作之外,最重要的就是加载 Launcher.java 之中的 ExtClassLoader ,并设定其 Parent 为 null ,代表其父加载器为 BootstrapLoader 。然后Bootstrap Loader 再要求加载 Launcher.java 之中的 AppClassLoader ,并设定其 Parent 为之前产生的 ExtClassLoader 实体。这两个加载器都是以静态类的形式存在的。这里要请大家注意的是, Launcher$ExtClassLoader.class 与 Launcher$AppClassLoader.class 都是由 Bootstrap Loader 所加载,所以 Parent 和由哪个类加载器加载没有关系。
这三个加载器就构成我们的 Java 类加载体系。他们分别从以下的路径寻找程序所需要的类:
BootstrapLoader : sun.boot.class.path
ExtClassLoader: java.ext.dirs
AppClassLoader: java.class.path
这三个系统参量可以通过 System.getProperty() 函数得到具体对应的路径。
相关推荐
双亲委派模型使得系统类(如java.*开头的类)由Bootstrap ClassLoader加载,而用户自定义类由应用程序类加载器(AppClassLoader)加载。这避免了类的冲突,保持了系统类库的稳定性。但有时我们可能需要打破这种模型...
【图解版】深入分析ClassLoader类加载工作机制,从原理到JVM的装载过程,详情分析了ClassLoader加载类以及自定义类加载器的过程,不可用于商业用途,如有版权问题,请联系删除!
该电子书详细介绍了java虚拟机类加载机制,对于深入理解jvm工作原理有很好的帮助作用,对于初学java,有一定工作经验的小伙伴来说是一本提高自身java素养,夯实自己java基本技能的“葵花宝典”。
比如,如果用户尝试使用Application ClassLoader加载rt.jar中的类,这个请求会被委派到Extension ClassLoader,最终到达Bootstrap ClassLoader,它会首先检查自己是否已经加载了这个类,如果已经加载,那么就不会...
接着,Extension ClassLoader加载扩展类库,然后是App ClassLoader加载应用程序的类路径下的类。每个ClassLoader都有其父ClassLoader,它们形成了一个层次结构,子ClassLoader可以委托父ClassLoader去加载类,避免了...
Bootstrap ClassLoader负责加载JDK的核心库,Extension ClassLoader加载Java的扩展库,而App ClassLoader则加载应用的类路径下的类。用户还可以自定义类加载器,用于加载特定位置或格式的类。 2. **Java类** Java...
这种委托加载机制保证了类的唯一性,防止了同一个类被不同ClassLoader加载两次的问题。 ClassLoader还有自定义的概念,允许开发者创建自己的ClassLoader来实现特定的加载逻辑。例如,从网络、数据库或特定文件系统...
与Common ClassLoader类似,但它的优先级更高,这意味着如果在两个目录下有同名的类,Shared ClassLoader加载的类将被优先考虑。 #### 6. WebApp ClassLoader WebApp ClassLoader是最底层的类加载器,它负责加载每...
默认情况下,Java使用系统ClassLoader(Bootstrap ClassLoader)加载JDK核心库,然后是Extension ClassLoader加载扩展库,最后是App ClassLoader加载应用类路径(ClassPath)下的类。当这些默认ClassLoader无法满足...
在Java编程语言中,类加载器(ClassLoader)是运行时环境的核心组成部分,它负责将类的字节码从各种来源(如JAR文件、网络、内存等)加载到Java虚拟机(JVM)中,使得程序能够运行。本文将深入探讨“使用classloader...
包括commons-logging commons-beanutils commons-lang ezmorph json-lib-2.4-jdk15 commons-collections-3.2.1的jar包,可以解决 org/apache/commons/lang/exception/NestableRuntimeException的问题
最后,App ClassLoader加载的是应用类路径(ClassPath)中的类。 ClassLoader的工作流程主要包含以下步骤: 1. **查找类**:当JVM需要加载一个类时,ClassLoader会根据类名(全限定名,如`java.lang.String`)在...
Bootstrap ClassLoader加载JRE的核心库,Extension ClassLoader加载Java扩展目录下的类,而AppClassLoader则加载应用的主类路径(ClassPath)上的类。 接下来,我们来讨论“加密解密应用程序”。在Java环境中,为了...
Bootstrap ClassLoader加载JDK核心类库,Extension ClassLoader加载扩展类库,而AppClassLoader则加载应用类路径上的类。 3. 双亲委派模型:Java的类加载机制遵循双亲委派模型,即当一个类加载器接收到加载类的请求...
ClassLoader 中的 loadClass 方法是加载类的核心方法,该方法首先检查该 name 指定的 class 是否有被加载,如果没有加载,则委托它的双亲 ClassLoader 进行加载,只有当双亲 ClassLoader 无法加载成功后,才会由自己...
- **System/App ClassLoader**:这个ClassLoader加载应用程序类路径中的类。 #### 三、自定义ClassLoader实践 为了更深入地理解ClassLoader的工作机制,我们将构建一个简单的自定义ClassLoader示例,该ClassLoader...
Extension ClassLoader加载Java扩展目录($JAVA_HOME/jre/lib/ext)下的类库;Application ClassLoader则加载应用类路径(classpath)中的类。 编写自定义ClassLoader的主要原因是扩展加载类的方式,以满足特殊需求...
- Bootstrap ClassLoader加载JDK的核心库,如rt.jar。 - Extension ClassLoader加载Java扩展目录下的jar文件。 - AppClassLoder(也叫System ClassLoader)加载应用类路径(classpath)中的类。 2. **类加载机制...
在Java编程语言中,ClassLoader是一个至关重要的组成部分,它负责加载类到JVM(Java虚拟机)中。了解和掌握ClassLoader的工作原理以及如何自定义ClassLoader对于深入理解Java应用程序的运行机制非常有帮助。以下是对...