打包的可执行jar文件不使用包含的依赖库(sqLite4Java)

时间:2015-10-02 15:16:28

标签: swing netbeans jar jtextarea sqlite4java

我遇到了使用NetBeans IDE和Java Swing构建的打包程序的问题。当项目是从netbeans构建的时,它会复制程序所依赖的所有库,包括我在文件夹dist中名为lib的文件夹中使用的sqlite4java API。 Dist还包含可执行的.jar文件。打开文件时,将打开正常的jFrame,程序运行正常,直到调用SQLiteConnection。没有返回任何错误,但程序总是无法超过可执行文件.jar文件中的那一点。但它在NetBeans IDE中运行良好。

程序显示一个文本区域,并使用swing工作程序连续添加从数据库中获取的新字符串的文本区域。

未正确实例化代码段中未包含的所有变量。

以下是相关代码:

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文件时,才会出现此问题。程序otherwsie在NetBeans IDE中运行良好。

1 个答案:

答案 0 :(得分:0)

您捕获所有异常但不打印它们。改变

} catch (Exception ex) {
}

} catch (Exception ex) {
   ex.printStackTrace();
   Displays.append("exception thrown: " + ex);
}

您可能会看到一条可以解释问题的错误消息。

另外&#34;我的文件路径&#34;应该是一条绝对的道路。当前用户目录可能在Netbeans外部的内部运行不同,并解释相对路径不起作用。