Static blocks, which are executed when the project is started, are executed proactively .
Static methods, which are executed passively, need to be called before they are executed.
Static {
Try{
String Path = Config.class. GetResource ("/"). Touri (). GetPath ();
is =NewFileInputStream (path + "config.properties");
Properties =NewProperties ();
Properties.load (IS);
}Catch(Exception e) {
Logger.error ("System configuration File" Config.properties "not found.) ");
E.printstacktrace ();
}
}
PublicStaticvoidMain (string[] args) {
System.out.println (Testclassresource.class. GetResource (""));
System.out.println (Testclassresource.class. GetResource ("/"));
System.out.println (Testclassresource.class. getClassLoader (). GetResource (""));
}
file:/d:/workspace/tests/webroot/web-inf/classes/com/java/base/
file:/d:/workspace/tests/webroot/web-inf/classes/
file:/d:/workspace/tests/webroot/web-inf/classes/
Class.getResource ("") returns the current class, which is the start of the package
Class.getResource ("/") returns the position of the Classpath
getClassLoader (). GetResource ("") returns the location of the Classpath
getClassLoader (). GetResource ("/") Error!!
Java.lang.class.getResource