简体   繁体   English

Java .jar无法启动,因为从文件加载了jcombobox数据

[英]Java .jar fails to start because of loading jcombobox data from file

I am beginner with Java and I have tried to make my app better. 我是Java的初学者,并且尝试过改进我的应用程序。 So I have a method to fill the jcombobox with items from text file. 因此,我有一种方法可以用文本文件中的项目填充jcombobox。 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. 带有项目的文本文件在我的netbeans项目的根目录中。 It works perfectly when running the app from netbeans. 从netbeans运行应用程序时,它可以完美运行。 But when I build the project and create .jar file. 但是当我构建项目并创建.jar文件时。 It does not run. 它不会运行。 I tried to run it from comand line. 我试图从comand行运行它。 This is what I got. 这就是我得到的。

java.io.FileNotFoundException: items(System cannot find the file.) java.io.FileNotFoundException:项目(系统找不到文件。)

How to deal with this? 该如何处理? I didnt found anything. 我什么都没找到。 I dont know where is problem since it works nice in netbeans. 我不知道问题出在哪里,因为它在netbeans中很好用。 Thank you very much for any help. 非常感谢您的帮助。

Is the .jar file in the same root directory? .jar文件是否在同一根目录中?

Your exported .jar file might be working from a different directory, and not be able to find the text file. 您导出的.jar文件可能正在其他目录中工作,而无法找到该文本文件。

Try placing the exported Jar in the same directory as the text file. 尝试将导出的Jar放置在与文本文件相同的目录中。

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]);
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM