Analysis and resolution of exceptions found in jar package file files

Source: Internet
Author: User
Tags file url readline

SOURCE Link:

Http://hxraid.iteye.com/blog/483115#comments

We often read some resource files in the code (compared to slices, music, text, etc.). These simple processes are of course not problematic when running alone. However, if we make the code into a jar package, even if the resource files are packaged together, these things will not be found. Take a look at the following code:

Java code
  1. Source Code 1:
  2. Package edu.hxraid;
  3. Import java.io.*;
  4. Public class Resource {
  5. public void GetResource () throws ioexception{
  6. File file=new file ("Bin/resource/res.txt");
  7. BufferedReader br=New BufferedReader (new FileReader (file));
  8. String s="";
  9. While ((S=br.readline ()) =null)
  10. System.out.println (s);
  11. }
  12. }

This code is written in the eclipse-built Java project, where the directory is: (where the resource file Res.txt is placed in the bin directory to make a jar package)
1, src/
Src/edu/hxraid/resource.java
2, bin/
Bin/resource/res.txt
Bin/edu/hxraid/resource.class

Obviously running source code 1 is able to find the resource file res.txt. But when we make the whole project into a jar package (Resourcejar.jar), the directory inside the jar is:
Edu/hxraid/resource.class
Resource/res.txt

in this case, the Resource.class bytecode in the jar package: LDC <string "Bin/resource/res.txt" > [20] will not be able to navigate to the Res.txt location in the jar package. Even if the bin/directory is removed: LDC <string "Resource/res.txt" > [20] still cannot locate the Res.txt in the jar package.

This is mainly because the jar package is a separate file rather than a folder, and it is absolutely impossible to pass "file:/e:/.../resourcejar.jar/resource/res.txt" This form of file URL to locate Res.txt. So even with a relative path, you can't locate the TXT file within the jar file (the reader may have some confusing explanations for this reason, and we'll elaborate on the results of a run of code below).

then the resource into the jar package, no matter what path Resourcejar.jar in the system, the bytecode program in the jar package can find the resources in the package. Is this going to be a fantasy?


Of course not, we can use the class loader (ClassLoader) to do this:

(1) ClassLoader is an abstract class of Class loaders. It can dynamically get the run information of the load class at run time. It can be said that when we call the resource class in Resourcejar.jar, the JVM loads into the resource class and records the resource runtime information (including the path information for the jar package where resource is located). The methods in the ClassLoader class can help us to obtain this information dynamically:
Public URL getresource (String name)
Finds a resource with the given name. A resource is some data (images, sounds, text, and so on) that can be accessed through class code in a code-base-independent manner. and returns the URL object for the resource.
Public InputStream getResourceAsStream (String name);
Returns the input stream that reads the specified resource. This method is important to get the contents of the files in the jar package directly.

(2) ClassLoader is abstract, it is impossible to instantiate an object, it is more impossible to call the above two methods through ClassLoader. So when we actually write the code, it's through the GetResource () and getResourceAsStream () methods in the class classes, and these two methods delegate the GetResource () in ClassLoader and getResourceAsStream () method . Well, now we re-write a resource code to see what the above puzzling words mean:

Java code
  1. Source Code 2:
  2. Package edu.hxraid;
  3. Import java.io.*;
  4. Import Java.net.URL;
  5. Public class Resource {
  6. public void GetResource () throws ioexception{
  7. //Find the URL of the specified resource, where Res.txt still starts in the bin directory
  8. URL fileurl=This.getclass (). GetResource ("/resource/res.txt");
  9. System.out.println (Fileurl.getfile ());
  10. }
  11. public static void Main (string[] args) throws IOException {
  12. Resource res=New Resource ();
  13. Res.getresource ();
  14. }
  15. }

Run this source code result:/e:/code_factory/wanwan/bin/resource/res.txt (.. /code_factory/wanwan/. is the path where Java project resides)

We package this code into Resourcejar.jar and place the Resourcejar.jar under other paths (such as C:\ResourceJar.jar). Then create another Java project and import Resourcejar.jar to write a test code that calls the resource class in the jar package:

Java code
    1. IMPORT&NBSP;JAVA.IO.IOEXCEPTION;&NBSP;&NBSP;
    2. IMPORT&NBSP;EDU.HXRAID.RESOURCE;&NBSP;&NBSP;
    3. public CLASS&NBSP;TEST&NBSP;{&NBSP;&NBSP;
    4.     public static void main (string[] args)  throws ioexception {   
    5.         resource  res=new resource ();   
    6.          res.getresource ();   
    7.     }  
    8. }   

The result of this operation is:file:/c:/resourcejar.jar!/resource/res.txt

we managed to get the res.txt position dynamically at run time. However, the question comes, can you get the Res.txt file using the code below?
File F=new file ("C:/resourcejar.jar!/resource/res.txt");
Of course not, because ".../resourcejar.jar!/resource/..." is not the format of the file resource Locator (the resource in the jar has its special URL form:Jar:<url>!/{entry} ). Therefore, if the class source code in the jar package is in the form of file F=new files (relative path), it is not possible to locate the document resource. This is why when the source code 1 is packaged into a jar file, it is reported that the FileNotFoundException is the crux of the call to the jar package.

(3) We cannot read the resource file Res.txt in Resourcejar.jar by using the normal operation file, but we can get it through the class getResourceAsStream () method. , this method is how to read the resource files in the jar, which is transparent to us. We rewrite the Resource.java into:

Java code
  1. Source Code 3:
  2. Package edu.hxraid;
  3. Import java.io.*;
  4. Public class Resource {
  5. public void GetResource () throws ioexception{
  6. //Returns the input stream that reads the specified resource
  7. InputStream is=This.getclass (). getResourceAsStream ("/resource/res.txt");
  8. BufferedReader br=New BufferedReader (new InputStreamReader (IS));
  9. String s="";
  10. While ((S=br.readline ()) =null)
  11. System.out.println (s);
  12. }
  13. }

we will edu/hxraid/resource.class and resource files in the/bin directory under Java engineering resource/ Res.txt packaged into the Resourcejar.jar, regardless of the jar package in any directory of the system, call the resource class in the jar package can obtain the Res.txt resources in the jar package, no longer cannot find the Res.txt file.

Analysis and resolution of exceptions found in jar package file files

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.