我在使用 NetBeans IDE 和 Java Swing 构建的打包程序时遇到问题。当项目是从 netbeans 构建时,它会将程序所依赖的所有库复制到文件夹 dist 中名为 lib 的文件夹中,包括我使用的 sqlite4java API。Dist 还包含可执行的 .jar 文件。当文件打开时,正常的 jFrame 打开并且程序运行良好,直到调用 SQLiteConnection。不会返回任何错误,但程序总是无法超过可执行 .jar 文件中的那个点。但是,它在 NetBeans IDE 中运行良好。
该程序显示了一个文本区域,并且使用了一个swing worker 来不断地在文本区域中添加从数据库中获取的新字符串。
代码段中未包含的所有变量均已正确实例化。
这是有问题的代码:
int state = evt.getStateChange();
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
//gets the item state i.e. pushed or unpushed
if (evt.getSource() == Record) {
Displays.setText("");
Displays.setEditable(false);
if (worker != null) {
worker.cancel(true);
}
worker = new SwingWorker() {
@Override
protected Integer doInBackground()//Perform the required GUI update here.
{
try {
//x is created to keep track of the amount of
//previously displayed headers so no headers are displayed more than once
int x = 0;
//while the button is pushed down
while (state == evt.SELECTED) {
ArrayList<ArrayList<String>> urlnames = new ArrayList<>();
System.out.println(timeStamp);
Displays.append("reached");
SQLiteConnection db = new SQLiteConnection(new File("my file path"));
Displays.append("reached2");
db.open(true);
//sql statement to find headerurls later than the original starttime
SQLiteStatement st = db.prepare("SELECT url FROM urls Where datetime(last_visit_time/1000000-11644473600,'unixepoch','localtime') >= \"" + timeStamp + "\" ORDER BY last_visit_time ASC");
urlnames.add(new ArrayList<>());
try {
while (st.step()) {
urlnames.get(0).add(st.columnString(0));
}
urlnames.add(new ArrayList<>());
//gathers each header's respective times
st = db.prepare("SELECT datetime(last_visit_time/1000000-11644473600,'unixepoch','localtime') as times FROM urls Where times >= \"" + timeStamp + "\" ORDER BY last_visit_time ASC");
while (st.step()) {
urlnames.get(1).add(st.columnString(0));
}
} finally {
st.dispose();
}
//ends connection
db.dispose();
//prints only the recently added headers
for (int i = 0; i < urlnames.get(0).size() - x; i++) {
System.out.printf(" %s %s %s %s \n", "Time visited: ", urlnames.get(1).get(x + i), " website url:", urlnames.get(0).get(x + i));
Displays.append("Time visited: " + urlnames.get(1).get(x + i) + " \t website url:" + urlnames.get(0).get(x + i) + " \n");
}
System.out.println(x);
//change x to account for the new size of the headers and waits 3 seconds before running through again
x = urlnames.get(0).size();
Thread.sleep(2000);
urlnames.clear();
}
} catch (Exception ex) {
}
return 0;
}
};
worker.execute();//Schedules this SwingWorker for execution on a worker thread.
}
第一个字符串“Reached”总是附加到文本区域,但第二个字符串“Reached2”永远不会添加。仅当我在 NetBeans 中构建程序并将其打包为包含依赖库的可分发 .jar 文件时,才会出现此问题。该程序在 NetBeans IDE 中运行良好。