Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include text files with Executable Jar

I have a Java project and I want to include a text file with the executable jar. Right now the text file is in the default package.

InputFlatFile currentFile = new InputFlatFile("src/theFile.txt");

I grab the file with that line as you can see using src. However this doesn't work with the executable jar.

How to keep this file with the executable jar, so someone using the program can just click a single icon and run the program?

like image 506
Jake Avatar asked Apr 20 '10 00:04

Jake


People also ask

Can JAR file be deployed?

Tomcat JAR deployment options There are three recommended options to make this happen: Package the JAR file in the WEB-INF\lib folder of the Java web application; Place the JAR file in the \lib subfolder of the Apache Tomcat installation; Configure a folder for shared JAR files by editing Tomcat's common.


2 Answers

you want it IN the executable jar? then to read the file you should use

getClass().getResourceAsStream()

to read the file.

Keep the text file in the package you want to access it from. The classloader will find it.

Remember also that filenames in JARs are case sensitive.

like image 89
Sanjay Manohar Avatar answered Sep 25 '22 10:09

Sanjay Manohar


The base directory in the jar file is in the classpath.

Try InputFlatFile currentFile = new InputFlatFile("theFile.txt");

You are probably using an IDE and it has a src folder in it that the IDE uses for the base of the packages. When you create the jar file from the IDE it then removes the src folder and the root folder has the packages in it.

i.e. in eclipse src/com.blah.blah once jar file is created the structure becomes com.blah.blah

Of course I assume that InputFlatFile is properly reading the value.

http://www.devdaily.com/blog/post/java/read-text-file-from-jar-file

like image 43
Romain Hippeau Avatar answered Sep 24 '22 10:09

Romain Hippeau