inputstream is = getclass (). getClassLoader (). getResourceAsStream ("helloworld.properties"); What do you mean by GetClass () and getClassLoader ()?
getclass (): Gets the class object to which the current object belongs
getclassloader (): Gets the class loader of the class object
Span style= "FONT-SIZE:18PX;" >getclass:
public final Class getclass ()
returns the runtime class of an object. That Class object is the object, the locked by static synchronizedmethods of The represented class.
returns:
the object of type Class that Represents the runtime class of the object.
getClassLoader
Public ClassLoader getClassLoader ()
Returns the class loader for the class. Some implementations may use NULL to represent the bootstrap class loader. This method would return null in such implementations if this class is loaded by the Bootstrap class loader.
If A security manager is present, and the Caller´s class loader are not null and the Caller´s class loader are not the same As or an ancestor of the class loader for the class whose class loader are requested, then this method calls the security m Anager´s Checkpermission method with a runtimepermission ("getClassLoader") permission-ensure it´s OK to access the Clas s loader for the class.
If This object represents a primitive type or void, and NULL is returned.
Returns:
The class loader that loaded the class or interface represented by this object.
Throws:
securityexception-if A security manager exists and its Checkpermission method denies access to the class loader for The class.
See Also:
ClassLoader, Securitymanager.checkpermission (java.security.Permission), Runtimepermission
a small Trap for Class.getclassloader ():)
yesterday my Code was always in Integer.class.getClassLoader (). GetResource ("*********"); this sentence throws a null pointer exception and is positioned as getClassLoader () to return null. Check out the JDK documentation, and there's a trap here:
description of getClassLoader () in the JDK:
/**
* Returns the class loader for the class. Some implementations
* NULL to represent the bootstrap class loader. This method would return
* Null In such implementations if this class is loaded by the bootstrap
* class loader.
*
* <p> If A security manager is present, and the caller ' s class loader is
* NOT NULL and the caller ' s class loader are not the same as or an ancestor of
* The class loader for the class whose class loader are requested, then
* This method calls the security manager ' s <code>checkPermission</code>
* Method with a <code>runtimepermission ("getClassLoader") </code>
* Permission to ensure it's OK to access the class loader for the class.
*
* <p>if This object
* Represents a primitive type or void, NULL is returned.
.....
The above English can be understood in the following words:
The process of loading a class is very simple: find the location of the class and load the bytecode of the found Java class into memory to generate the corresponding class object. Java's class loader is designed to implement a process where the JVM has more than one class loader, and in fact, you can have countless class loaders for the JVM if you like, but I can't think of any other uses other than testing the JVM. You should have found the problem that the class loader itself is a class, it also needs to be loaded into memory, then the class loader by who load it, there must be a root it? Yes, there really is such a root, it is stays bootstrap ClassLoader. Why is it stays, because you can not grasp in Java code even a little bit of its tail, although you are able to experience it all the way, because Java is required to run the environment of all the class library is loaded by it, and it is written in C + + program, can run independently, Can be said to be the beginning of the JVM run, great bar. After Bootstrap completes its task, it generates a appclassloader (in fact, the system also uses the Extension class loader extclassloader, which is used to load the classes in the Java Runtime Extension package), which is what we often use. Can call Classloader.getsystemclassloader () to obtain, we assume that the program does not use the class loader related operation set or custom new class loader, then all of the Java classes we write will be loaded by it, it is worthy of respect. Appclassloader find the area of the class is familiar with the classpath, is also a beginner must cross the threshold, there is no flash of the feeling, we follow its class lookup scope to it named Classpath class loader. As previously assumed, when a new class appears in Java, Appclassloader first in the class passed to its parent class loader, which is extion ClassLoader, asking if it can load the class, and if so, Appclassloader will not do the job. , the same extion ClassLoader, when loaded, will also ask its parent class loader first. We can see that the class loader is actually a tree-like structure diagram, each class loader has its own father, the class loader when loading the class, always first let its own parent loader load (How to respect the elders), if the parent loader cannot load the class, it will load itself, if it can not load, then sorry, It cries out: Exception,class not found. It is necessary to mention that Noclassdeffou is thrown when a class failure is loaded by using the Classpath loader directlyNdexception exception. If you use a custom class loader LoadClass method or ClassLoader Findsystemclass method to load the class, if you do not deliberately change, then the ClassNotFoundException is thrown.
The JDK here tells us: If a class is loaded by bootstrap, then we get classloader through this class, some JDK implementations will return a null, for example, I use new Object (). GetClass (). getClassLoader (), a null is returned so that the above code will have a Nullpointer exception. So on the safe side, we'd better use our own class to get ClassLoader ("This.getclass (). getClassLoader ()") so there's no problem.
About GetClass (). getClassLoader ()