How to add resources to jar file

I need to add an exel file to my jar so that it is portable. I know the answer uses getClass (). GetResource .... but I have no clue on how to use this code.

I have an exel file in the src folder with my class files, it works in netbeans, but not when I open the jar file on another PC. i get filenotfound exception

here is the code i used:

public class WindageLogic { //.... public void GetValues() throws Exception { File exel=new File("src/Calculator/table1.xls"); Workbook w1; //reads from file //reads from file w1 = Workbook.getWorkbook(exel); 

Can someone give me an implementation code that will allow me to access this file from the jar application, I spent the whole weekend browsing the material on the Internet, but I don’t understand how I can use these getresource methods.

thanks alot

+4
source share
2 answers

If your excel file is a resource, you cannot use File to manage it, but rather for resources, and I believe that it is read-only. Are you sure this is what you want to do? Instead, you can load the String command line, which tells the program where to look for the file, or create a properties file that tells where to look first.

Change 1
If you use JExcel, you can call Workbook.getWorkbook(java.io.InputStream is) and get your resource as a stream through the class method getResourceAsStream(...) .

eg.

  public void GetValues() throws Exception { Workbook w1; //reads from file w1 = Workbook.getWorkbook(WindageLogic.class. getResourceAsStream("/Calculator/table1.xls") ); //... 
+2
source

To getClass () method. getResource () worked, the resource should be accessible in the class path of the application (packaged in one of the jar files or just in a folder on the disk, which is included in the class path).

Depending on which IDE or code tool you use, there are several ways to verify this.

The easiest way is to put the src folder in the java -classpath src:%CLASSPATH% <your.class.name> startup class path java -classpath src:%CLASSPATH% <your.class.name>

+1
source

All Articles