Reputation: 1055
Today I encountered problems to launch a jar-file for the first time. Now I know (after unzipping the jar) that the textfiles did not come along when I did the export and creation of a jar-package of my program in Eclipse.
Why does the textfiles not come along with the class files? Where should I put those in the project?
I put the textfiles in the root of the project folder
Greaful for help
EDIT: probably I can do it manually in the cmd but I dont know what I should add in the programcode where the textfiles are loaded. Should I for instance impelent a classloader? I know how to do so when loading images such as jpg org gif. But what if it is a textfile?
Here is the method responsible for loading textfiles
private void read(String text_file, int len, int index) {
String[] stringBuffer = new String[len];
File file = new File(text_file);
FileReader fileReader;
BufferedReader bufferedReader ;
try {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
String line;
int i = 0;
while ( (line = bufferedReader.readLine()) != null) {
stringBuffer[i] = line;
i++;
}
bufferedReader.close();
} catch (FileNotFoundException fnde) {
fnde.printStackTrace();
JOptionPane.showMessageDialog(null, "files could not be found", "Help", 0);
} catch (Exception e) {
e.printStackTrace();
}
splitString(stringBuffer, index);
}
Upvotes: 0
Views: 151
Reputation: 2035
I made an example from your code. The file text.txt is in the source folder under META-INF.
package test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
public class ReadFile {
public static void main(String[] args) {
ReadFile o = new ReadFile();
o.read("test.txt", 2, 0);
}
private void read(String text_file, int len, int index) {
BufferedReader bufferedReader ;
String[] stringBuffer = new String[len];
try {
bufferedReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/META-INF/".concat(text_file))));
String line;
int i = 0;
while ( (line = bufferedReader.readLine()) != null) {
stringBuffer[i] = line;
i++;
}
bufferedReader.close();
} catch (FileNotFoundException fnde) {
fnde.printStackTrace();
JOptionPane.showMessageDialog(null, "files could not be found", "Help", 0);
} catch (Exception e) {
e.printStackTrace();
}
splitString(stringBuffer, index);
}
private void splitString(String[] stringBuffer, int index) {
for(String line: stringBuffer) {
System.out.println(line);
}
}
}
Hope this helps.
Upvotes: 1
Reputation: 5039
Only folders/jars listed under -> Properties -> Java Build Path -> Order and Export will be exported into the JAR. Place the files into a folder, which is on your build path by
the base folder of your project is not and should/can not be part of the export.
Upvotes: 0