Maven project, Java code load resources below the resource file path

Source: Internet
Author: User

1 through the class loader loader,

1.  URL resource = TestMain.class.getResource ("/18500228040.txt");

File File = new file (Resource.getfile ());
String Absolutepath = File.getabsolutepath ();
This way the absolute path is obtained, window and Linux will get different results, and these will be in the editor to run the program in the time can be successful, but once the jar package, it will create a problem
The reaction under Window

There's a reaction like this on Linux.

2.   String Path = This.getclass (). GetResource ("/18500228040.txt"). GetPath (); and by this way the load is the same as above, But this will get the absolute path relative to the drive letter below the window, but note that a "/" will be added to the front of him, causing the path to be found when the resource is not available.

By intercepting the right results, it is not a good solution, will continue to do research, then will be written on the blog

Maven project, Java code load resources below the resource file path

Related Article

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.