I have a huge program that I have spent 100+ hours on. It would not work when I exported it even though it worked fine in Ecipse. I made a smaller program with just that code to test it out. I have searched and changed my code and I still can’t get my program to work when I export and run my jar file. It works great in Eclipse. I have tried file reader, stream, buffer, standing on my head, and changed to anything I have seen online about reading a file that is included in the jar file. I am new to coding so some things still confuse me. I changed the .java to .zip and looked at the pieces and the file is in there.
My read code is
private void readFile(){
try {
System.out.println("starting array");
String[] myarray;
myarray = new String[5];
System.out.println("myarray"+myarray);
url = getClass().getResource("classifyinginfo.txt");
System.out.println("url is "+url);
readbuffer = new BufferedReader(new FileReader(url.getPath()));
line1 = readbuffer.readLine();
line1 = readbuffer.readLine();
line1 = readbuffer.readLine();
line1 = readbuffer.readLine();
{ String splitarray[] = line1.split(",");
firstentry = splitarray[0];
myarray[0]=splitarray[3];
myarray[1]=splitarray[4];
myarray[2]=splitarray[5];
myarray[3]=splitarray[6];
myarray[4]=splitarray[7];
//line2 and 3 readbuffer etc
Arrays.sort(myarray);
for(int i=0; i < myarray.length; i++){
System.out.println(myarray[i]);
textArea.append(myarray[i]+"\n");}
}
System.out.println(Arrays.toString(myarray));
// textArea.setText(Arrays.toString(myarray));
readbuffer.close(); } catch (IOException e) {
System.out.println("we have an error");}
System.out.println("Line: " + line1 );
}
please please tell me how to make this work in and out of Eclipse.
You just do like this:
The
FileReaderclass can read individual files on disk, and that’s all. But you don’t need to use it once you have a URL. Modified in this way, the program will work whether the file is in a jar or not.You can actually eliminate the URL altogether and just do this: