[Java] obtains the current. class file compilation location, java. class

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.