1

I have to read data from an excel file and show the data in my application. I want to package my excel file (data file) along with the executable jar. I have created a source folder within my main project folder and named it 'res'. Inside 'res', I have 2 sub-folders (normal folders) called "images" and "data". Inside the data folder I have placed the excel file.

My Project Structure

project structure

Build Path

build path of project

Export as JAR

enter image description here

Problem:

The application works flawlessly when I run it from Eclipse but when I export it as a jar, the application doesn't work.It is able to find the images but fails to find the excel file.

In other-words, when I run the application from inside eclipse (Right Click -> Run As -> Java Application) it runs perfectly. But when the launch the exported JAR file ("Tool.jar"), it fails to read the excel data.

Code to Read Excel

URL excelResources = getClass().getResource("/excel/data.xls");
File excel = new File(excelResources.toURI());
FileInputStream fis = new FileInputStream(excel);
Partha Chetry
  • 136
  • 1
  • 9
  • 1
    `getClass().getResource("/res/data/MyAwesome.xls")` will give you a `URL` to your Excel file; `getClass().getResourceAsStream("/res/data/MyAwesome.xls")` will return an `InputStream` to your Excel file... – MadProgrammer Mar 30 '15 at 05:44
  • Can you please add details about the way you're accessing your images and the excel? and error if you got any – sathya_dev Mar 30 '15 at 05:46
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Mar 30 '15 at 05:54
  • http://stackoverflow.com/a/25636097/2587435 – Paul Samsotha Mar 30 '15 at 06:08
  • @peeskillet :- Thanks for pointing me to this link. I tried them but failed to get my issue resolved. The problem is with the exported .jar file. It is able to find the images but fails to load the excel file. Please help. – Partha Chetry Mar 31 '15 at 17:41
  • @MadProgrammer :- Thanks. I am reading my excel in the same way u mentioned. It works fine when I launch the project from with Eclipse. But when I try to execute the exported jar file, it fails to read the excel file included. Please help. – Partha Chetry Mar 31 '15 at 17:43
  • Use `getResourceAsStream` (instead of `getResource`), which returns an `InputStream`. To read it, you can do `BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));` – Paul Samsotha Mar 31 '15 at 17:43
  • Wrapping the result of getResource in a File is NOT what you should be doing. The excel file CAN NOT be accessed like a File on the system, because it's not, it's part of the jar file. Instead, if you need a InputStream, use getAsStream instead – MadProgrammer Mar 31 '15 at 19:53

2 Answers2

1

This...

URL excelResources = getClass().getResource("/excel/data.xls");
File excel = new File(excelResources.toURI());
FileInputStream fis = new FileInputStream(excel);

Is not how embedded resources work. You can no longer access the excel file as if it was a file on the file system, because it's not, it's embedded inside the Jar file.

If you need an InputStream, use Class#getResourceAsStream

try (InputStream is = getClass().getResourceAsStream("/excel/data.xls")) {
    //...
} catch (IOException exp) {
    exp.printStackTrace();
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

Using maven it could be done by plugin:

<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>your.class.Name</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <resources>
                        <resource>
                            <directory>${basedir}/src/main/res/data</directory>
                            <filtering>false</filtering>
                        </resource>
                    </resources>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
szefuf
  • 500
  • 3
  • 14