Java Problem resolution--jar resource invocation in package

Source: Internet
Author: User

When I was packing the project, I encountered a situation in which the IDE could run but the jar package ran incorrectly (FileNotFoundException). Many of the workarounds found on the Internet require the use of the Getresouce method to obtain an absolute path, or the file must be read in before it can continue. The above error is still used with Getrescource, while the file to be read is a file that needs to interact with the front-end and to operate on the file, you must ensure that the original format of the file is unchanged. So reading into the re-operation of the method is not appropriate, because the output of the format control and so will be very troublesome. Here are some of the things you learned about the problem: The I.jar package is a file, and I understand that there is a file system inside the jar package that adds one to the file-specific path! , there will be* !/* such a form, while using a relative or absolute path within the jar package actually calls the filesystem on the machine, the path and the mismatch in the jar package, so it is not feasible to get the file in the jar package with this path. (This gives me another question about how the path passed by the GetResource () method on the Web is resolved): if it's just a path, the jar package is available, and it's an absolute path (/static/ config), which is available inside the jar package (although you don't know why), but if you want to invoke the
New= Resource.getfile ();
It's not possible at this time. I think that because the file is called, it is the filesystem on the machine, and the files output is also the path of the file:/c:/****, and the path inside the jar package is non-conforming, so the jar package cannot find the file through files. II. This time to use the file path, because the call to a Documentbuidler.parse () method, the first is passed a file type parameter, and the new file and the use of the path, the problem is not resolved to view the source code, found that the parse method can be overloaded:
// The following is a 4-overloaded parse function  Public Document Parse (InputStream is) {...}  Public Document Parse (inputstream is,string systemid) {...}  Public Document Parse (String uri) {...}  Public throws Saxexception, ioexception{...}

And then! We found that we could pass uri! directly. The following method is used:
/** * Doc is an instance of document * DB is documentbuilder instance * path is the absolute path to the resource file */  = Db.parse (this. getclass (). getresource (Path). toString ());
And then it became!!

Java Problem resolution--jar resource invocation in package

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.