【问题标题】:Java .jar fails to start because of loading jcombobox data from file由于从文件加载 jcombobox 数据,Java .jar 无法启动
【发布时间】:2016-10-10 16:48:21
【问题描述】:

我是 Java 的初学者,我试图让我的应用程序变得更好。 所以我有一种方法可以用文本文件中的项目填充 jcombobox。 方法是

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

}

而且我使用正确

fillComboBox(jCombobox1, "items");

包含项目的文本文件位于我的 netbeans 项目的根目录中。 从 netbeans 运行应用程序时,它可以完美运行。但是当我构建项目并创建 .jar 文件时。它不运行。我试图从命令行运行它。 这就是我得到的。

java.io.FileNotFoundException: items(系统找不到文件。)

如何处理?我什么也没找到。我不知道问题出在哪里,因为它在 netbeans 中运行良好。非常感谢您的帮助。

【问题讨论】:

    标签: java netbeans jar jcombobox


    【解决方案1】:

    .jar 文件是否在同一个根目录中?

    您导出的 .jar 文件可能在不同的目录中工作,并且无法找到文本文件。

    尝试将导出的 Jar 放在与文本文件相同的目录中。

    我的例子:

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

    【讨论】:

    • @jv95 尝试使用 System.getProperty("user.dir") 打印程序的当前目录。发布结果。
    • 它打印了 C:\Users\Jan\Documents\NetBeansProjects\Project
    • 而就在这个目录中是带有jcombobox项的文本文件。
    • @jv95 是从 netbeans 打印并导出 jar 的确切路径吗?
    • 您的程序是否足够短以至于您可以发布它?或者你能发一个Minimal, Complete, and Verifiable example吗?我想提供帮助,但如果没有更多信息,我不确定如何继续。
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 2011-02-05
    • 2021-07-08
    • 1970-01-01
    • 2014-05-07
    • 2020-05-10
    • 2017-04-11
    相关资源
    最近更新 更多