Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Runnable Jar with external files included

I want to build a runnable jar in java. I need to include some of the files in the jar so that when I execute jar the files are automatically read from the java class. Hence I created a folder in the project and referred these files from the project. I created jar file following some tutorial but I could not able to include these external files in my jar file. Please let me about creating runnable jar with external files.

My file struture is

Test
|
|
-------src
|        |
|        default package
|                      |
|                      |
|                      test1.java
|
 -------FileFOlder
|              |
|              | 
|              abc.txt

I am accessing abc.txt in test1.java class. My code is,

public class test1 {


public static void main(String[] args) throws IOException {


    char [] read = new char[20];
    String path = new File(".").getCanonicalPath();
    path = path+"\\Newfolder\\abc.txt";
    System.out.println(path);
    File nF = new File(path);
    FileReader fR = new FileReader(nF);
    fR.read(read);
    for(char c : read){
        System.out.print(c);
    }
    fR.close();
    System.out.println(" Hi..This is test program ");
}

}

When I create executable jar using eclipse export option, I am unable to see FileFolder directory inside the jar. Please give me some information regarding this.

like image 665
Anup Avatar asked Aug 06 '13 21:08

Anup


People also ask

How do I create a jar with an external library?

You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.

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.

What is difference between JAR file and runnable JAR file?

FYI: In simple terms, the difference between a JAR file and a Runnable JAR is that while a JAR file is a Java application which requires a command line to run, a runnable JAR file can be directly executed by double clicking it.


2 Answers

Here's what you should do instead:

Put that file back in your jar file. Use class.getResourceAsStream() to read it instead of File and FileReader. Here is an explanation of how to do that: How to really read text file from classpath in Java

like image 166
Daniel Kaplan Avatar answered Oct 13 '22 04:10

Daniel Kaplan


Problem Solved!

here is how:

1) right click your project folder and create a new folder.

2) move all of your files that you want packed into the jar into that folder.

3) click project -> properties -> Build Path -> Source -> Add Folder and select that folder you just created.

4) create your JAR!

Since you already have created your folder with abc.txt insideit, you can skip steps 1 and 2

EDIT: one way you can make sure your JAR contains these files is to use 7zip.

like image 21
scottysseus Avatar answered Oct 13 '22 05:10

scottysseus