Java .jar 无法启动,因为从文件加载 jcombobox 数据
Java .jar fails to start because of loading jcombobox data from file
我是 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(System cannot find the file.)
如何处理?我什么都没发现。我不知道问题出在哪里,因为它在 netbeans 中运行良好。非常感谢您的帮助。
.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]);
}
}
}
我是 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(System cannot find the file.)
如何处理?我什么都没发现。我不知道问题出在哪里,因为它在 netbeans 中运行良好。非常感谢您的帮助。
.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]);
}
}
}