0

I am beginner with Java and I have tried to make my app better. So I have a method to fill the jcombobox with items from text file. The method is

private void fillComboBox(JComboBox combobox, String filepath) throws FileNotFoundException, IOException {

    BufferedReader input = new BufferedReader(new FileReader(filepath));
    List<String> strings = new ArrayList<String>();
    try {
        String line = null;
        while ((line = input.readLine()) != null) {
            strings.add(line);
        }
    } catch (FileNotFoundException e) {
        System.err.println("Error, file " + filepath + " didn't exist.");
    } finally {
        input.close();
    }

    String[] lineArray = strings.toArray(new String[]{});

    for (int i = 0; i < lineArray.length - 1; i++) {
        combobox.addItem(lineArray[i]);
    }

}

And I am using it correctly

fillComboBox(jCombobox1, "items");

the text file with items is in my root directory of netbeans project. It works perfectly when running the app from netbeans. But when I build the project and create .jar file. It does not run. I tried to run it from comand line. This is what I got.

java.io.FileNotFoundException: items(System cannot find the file.)

How to deal with this? I didnt found anything. I dont know where is problem since it works nice in netbeans. Thank you very much for any help.

jv95
  • 681
  • 4
  • 18

1 Answers1

0

Is the .jar file in the same root directory?

Your exported .jar file might be working from a different directory, and not be able to find the text file.

Try placing the exported Jar in the same directory as the text file.

My example:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class test {

    public static void main(String[] args) throws FileNotFoundException, IOException{
        JComboBox box = new JComboBox();
        fillComboBox(box, "C:\\path\\test.txt");
        JOptionPane.showMessageDialog(null, box);

    }

    private static void fillComboBox(JComboBox combobox, String filepath) throws FileNotFoundException, IOException {

        BufferedReader input = new BufferedReader(new FileReader(filepath));
        List<String> strings = new ArrayList<String>();
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                strings.add(line);
            }
        } catch (FileNotFoundException e) {
            System.err.println("Error, file " + filepath + " didn't exist.");
        } finally {
            input.close();
        }

        String[] lineArray = strings.toArray(new String[] {});

        for (int i = 0; i < lineArray.length - 1; i++) {
            combobox.addItem(lineArray[i]);
        }

    }
}
CA2C7B
  • 368
  • 1
  • 14
  • @jv95 Try printing the current directory of the program using System.getProperty("user.dir"). Post the results. – CA2C7B Oct 10 '16 at 23:00
  • It printed C:\Users\Jan\Documents\NetBeansProjects\Project – jv95 Oct 11 '16 at 17:16
  • And right in this directory is the text file with jcombobox items. – jv95 Oct 11 '16 at 17:17
  • @jv95 Is that exact path printed from the netbeans and exported jar? – CA2C7B Oct 11 '16 at 17:29
  • Is your program short enough that you could post it? Or could you post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)? I'd like to help, but I'm not sure how to proceed without more information. – CA2C7B Oct 11 '16 at 17:40
  • When running from cmd line it does not write anything. When running from netbeans it writes to path i have posted. – jv95 Oct 11 '16 at 18:12
  • Thank you. I posted the sample already in my question. This is the part of code that does not work. Can you try it by yourself? if you have the same problem?¨ – jv95 Oct 11 '16 at 18:12
  • I've posted my MCV versions of the code you provided. I suspect that the problem actually lies outside of that function. Let me know if my code works, and if it does, please provide the code for where you provide the path. – CA2C7B Oct 11 '16 at 19:08
  • Thank you very much. Your code works. I found the error. In my original code there is path "fillComboBox(jCombobox1, "items");" and in your code there is full path to directory. Well thats it. But how to make it to use the text file from root directory? If I will try to run this app on another computer with different path to file. It will not work. Right? How to say that the text file with jcombobox items is always in root directory? – jv95 Oct 11 '16 at 19:41
  • `new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());` will get you the path to the jar, but as [the commenters](http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file) say, it's not a bulletproof answer. You would then need to append the file name to that path. – CA2C7B Oct 11 '16 at 20:28