[Java] obtains the current. class file compilation location, java. class
The running file generated by Java is. class and is automatically thrown to the Java Virtual Machine for running. It is mainly used to obtain the path of this. class for some occasions and enhance the portability of the program.
The method is also simple, but this program is related to the main class name of the program. Just as the Java file name must be the same as the main class name of the program,
The following procedure:
Public class B {public static void main (String [] args) {String rootPath = B. class. getResource (""). getFile (); System. out. println (rootPath );}}
The running result can be used to retrieve the location of the B. java file after compilation, such:
B. class. getResource (""). getFile (); B indicates the current primary class name B.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.