Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a file inside a .jar file [duplicate]

Possible Duplicate:
How do I read a resource file from a Java jar file?

Starting to go completely bonkers over this after googling for hours. I've also seen variations of the question on the site but can't seem to get it working. A JFrame needs to read data from a ini file and I've created a method to open said file. Said file is stored in a folder called resources inside a jar file.

private BufferedReader openIniFile(String filename){
    BufferedReader brReader = null;                 
    try{
        File fileObj = new File(this.getClass().getResource("/resources/" + filename).toURI()); // The fileobject of the file to read
        if(fileObj.exists())                                            
            brReader = new BufferedReader(new FileReader(fileObj));                     

    } catch(Exception e){
        System.err.println("Exception while opening file: " + e.getMessage()); 

    }

    return null;
}

This of course works perfectly when I'm running the code after compilation, but throws an exception after being exported to a .jar file. I've looked into using InputStream, FileInputStream but can't seem to find a solution that would work.

How can I make the jar file recognize the resource?

like image 679
Chrizmo Avatar asked Nov 24 '11 13:11

Chrizmo


People also ask

How do I access files inside a jar?

You can use getResource() to obtain a URL for a file on the classpath, or getResourceAsStream() to get an InputStream instead. Show activity on this post. You could read the contents of a JAR file using the JarFile class.

How do I extract the contents of a JAR file?

Right-click on the JAR file and select Extract All. View the contents of the open JAR file on the file system.

Can a JAR file contain other JAR files?

To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar. jar contains another JAR file called MyUtils. jar, you cannot use the Class-Path header in MyJar.

Can you access source code of JAR file?

You can always extract the source files (Java files) of a jar file into a zip. location on your system. Drag and drop the jar for which you want the sources on the JAD. 3 JAD UI will open with all the package structure in a tree format.


2 Answers

When your resourse is in JAR file, it's not a File anymore. A File is only a physical file on the filesystem. Solution: use getResourceAsStream. Something like this:

new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/" + filename)))
like image 119
unbeli Avatar answered Oct 23 '22 06:10

unbeli


You're reading the file wrong. If the file is located in a JAR, you cannot use the File class. Instead, you must get an InputStream to the file using getResourceAsStream():

InputStream in = this.getClass().getResourceAsStream("/resources/" + filename);
like image 24
Michael Avatar answered Oct 23 '22 06:10

Michael