Java – create a runnable jar that contains external files
I want to build a running jar. Jar in Java I need to include some files in the jar so that when I execute the jar, these files will be automatically read from the Java class Therefore, I created a folder in the project and referenced these files from the project I created jar files after some tutorials, but I can't include these external files in my jar files Let me talk about using external files to create runnable jars
My file is
Test | | -------src | | | default package | | | | | test1.java | -------FileFOlder | | | | | abc.txt
I'm at test1 Access ABC. Java class txt. 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 use the eclipse export option to create an executable jar, I cannot see the filefolder directory in the jar Please give me some information about this
Solution
This is what you should do:
Put the file back into your jar file Use class Getresourceasstream() to read it instead of file and FileReader How to really read text file from classpath in Java
