简体   繁体   中英

how to access resources(Excel file) in jar file

Hi i have exported my java project as executable jar file. inside my project I am accessing a Excel file containing some data. Now I am not able to access the Excel file when I am trying to access the file.

My project structure is:
Java_Project_Folder
- src_Folder
- resources_Folder(Containing excel file)

I am accessing the excel file like

FileInputStream file=new FileInputStream(new File(System.getProperty("user.dir")
+File.separator+"resources"+File.separator+"Excel.xlsx"));

I have tried accessing this file using getResourceAsStream like:

FileInputStream file=(FileInputStream) this.getClass().getResourceAsStream
("/resources/Excel.xlsx");

But i am getting in is null exception. whats wrong can anyone help?

The first step is to include the excel file itself in your project. You can create a resources folder like you show, but to make sure this gets included in your jar, you add the resources folder in along with your source code files so that it gets built into the jar.

Then

InputStream excelContent = this.getClass().getResourceAsStream("/resources/Excel.xlsx");

should work. From one post at least, the leading forward slash may also mess things up if you use the ClassLoader.

getClass().getResourceAsStream("/a/b/c.xml")  ==> a/b/c.xml
getClass().getResourceAsStream("a/b/c.xml")  ==> com/example/a/b/c.xml
getClass().getClassLoader().getResourceAsStream("a/b/c.xml")  ==> a/b/c.xml
getClass().getClassLoader().getResourceAsStream("/a/b/c.xml")  ==> Incorrect

ref: getResourceAsStream fails under new environment?

Also in eclipse you can set the resources folder as a source folder like this:

in the properties of your eclipse project, go to java build path, select sources, and check to see if all needed source fodlers are added (as source folders). If some are missing, just add them manually using add sources... button

ref: Java Resources Folder Error In Eclipse

I tried this and it is working for me.

My Test1 class is in default package, just check where your accessing class is in any package, if it is then go back to exact resource folder from classpath like this "../"

public class Test1 {

    public static void main(String[] args) {
        new Test1();  
    }
    Test1(){
        BufferedInputStream file= (BufferedInputStream) this.getClass().getResourceAsStream("resources/a.txt");
        try {
            System.out.println((char)file.read());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

I bet you have no package called resources in your project.

Trying to use Class.#getResourceAsStream is the way to go. But this method does not return a FileInputStream . It returns an InputStream wich is an interface.

You should be passing the absolute name of the resource

InputStream is = getClass().getResourceAsStream("my/pack/age/Excel.xlsx");

where the excel file is located in the directory

resources/my/pack/age

FileInputStream file= (FileInputStream) this.getClass().getResourceAsStream("/resources/Excel.xlsx");

Why do you need FileInputStream? Use

InputStream is = getClass().getResourceAsStream..

Secondly use "resources/Excel.xlsx" Thirdly when constructing file like this

new File(System.getProperty("user.dir")+File.separator+"resources"+File.separator+"Excel.xlsx"));

is hard to control slashes. use

new File("parent (userdir property)", "child (resources\Excel.xlsx)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM