简体   繁体   English

将项目保存到JAR中并发生错误…通过NetBeans完美运行它

[英]Saved project into JAR and Errors occur… perfect running it through NetBeans

I've got an application with database connection etc, which is working correctly in NetBeans, although I want to test the application on different PC etc... so I've created a Jar File, and somehow when opening it it gives an error, straight after trying to connect to the database I'm guessing as GUI is functioning although I can't retrieve or save any data to the database. 我有一个具有数据库连接等功能的应用程序,尽管我想在其他PC上测试该应用程序,但是在NetBeans中可以正常运行...所以我创建了一个Jar文件,以某种方式打开它时出现错误,尝试连接到数据库后,尽管我无法检索任何数据或将任何数据保存到数据库,但我猜想由于GUI正在起作用。 What could be the problem? 可能是什么问题呢?

public View_HomePage() {
        initComponents();
        String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
               lblDate.setText(date);

        String[][] rowData = new String[0][4];
        String[] columns = {"ID", "Name", "Surname", "Registration", "Make", "Model", "Engine", "Year", "Mileage", "Type", "Date", "Time", "Postcode", "Number"};
        resultModel = new DefaultTableModel(rowData,columns);
        jTable1.setAutoCreateRowSorter(true); 

         try{
            Model_Customer[] appointment = Controller_ManageCustomer.FindCustomers2(lblDate.getText());
            resultModel.setRowCount(0);
            for(int i = 0; i < appointment.length; i++)
                resultModel.insertRow(i,new Object[]{appointment[i].GetID(),appointment[i].GetFName(), appointment[i].GetLName(), appointment[i].GetRegistration(), appointment[i].GetMake(), appointment[i].GetModel(), appointment[i].GetEngine(), appointment[i].GetYear(), appointment[i].GetMileage(), appointment[i].GetType(), appointment[i].GetDate(), appointment[i].GetTime(), appointment[i].GetPostcode(), appointment[i].GetNumber()});

         if (jTable1 == null) {
        jTable1 = new JTable(resultModel);
        add(new JScrollPane(jTable1));
    } else {
        jTable1.setModel(resultModel);
    }

}
         catch(Exception ex){
            JOptionPane.showMessageDialog(this,ex.getMessage(),"Error Problem",JOptionPane.ERROR_MESSAGE);
        }

    }

Error in CMD shows as = CMD中的错误显示为=

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\IMPREZA>cd Documents

C:\Users\IMPREZA\Documents>cd attachments

C:\Users\IMPREZA\Documents\attachments>java -jar LPGExerts.jar
ClassNotFoundException: org.postgresql.Driver
Fatalnull
null


Import java.sql.*;
import java.util.concurrent.*;

public class Database 
{
    private static Connection con;
    private static String url = "jdbc:postgresql://lpgexperts.heliohost.org:5432/*****";
    private static String user = "****";
    private static String pass = "***"
    static{
        try{
            Class.forName("org.postgresql.Driver"); 
            Database.con = DriverManager.getConnection(Database.url, Database.user, Database.pass);
        }
        catch (ClassNotFoundException ex) {System.err.println("ClassNotFoundException: " + ex.getMessage());}
        catch (SQLException ex)           {System.err.println("SQLException: " + ex.getMessage());}

        //Ping every minute, keep alive

        // Create a service with 3 threads.
        ScheduledExecutorService execService = Executors.newScheduledThreadPool(3);

        // Schedule a task to run every 5 seconds with no initial delay.
        execService.scheduleAtFixedRate(new Runnable() {
            public void run() {
                try{
                    Connection c = Database.GetConnection();
                    Statement stmt = c.createStatement();
                    ResultSet rs = stmt.executeQuery("SELECT 1");
                }catch(Exception e){
                    System.out.println("Fatal" + e.getMessage());
                    System.out.println(e.getCause());
                }
            }
        }, 0L, 60L, TimeUnit.SECONDS);
    }

    /**
     * Get connection interface
     * 
     * @return Connection instance
     */
    public static Connection GetConnection(){
        return Database.con;
    }

Update: Made it Work!!! 更新:使它工作!!! Opened project properties in NetBeans... and found that in Libraries there was nothing about PGDriver... added the driver to the library saved and compiled the jar file and woop woop works :) 在NetBeans中打开了项目属性...,发现在库中没有关于PGDriver ...将驱动程序添加到已保存的库中,并编译了jar文件,并产生了蠕动效果:)

You need to have the PostGresql driver on your classpath when you run it as a jar on a different machine. 当您在其他机器上以jar形式运行PostGresql驱动程序时,需要在其类路径上安装PostGresql驱动程序。

Say your driver jar is named pg.jar. 假设您的驱动程序jar名为pg.jar。 It is kept at - 它保持在-

c:\Users\IMPREZA\Documents\jars

then 然后

C:\Users\IMPREZA\Documents\attachments>java -classpath c:\Users\IMPREZA\Documents\jars\pg.jar;. -jar LPGExerts.jar

Update: Even though you are running it on same machine, do specify classpath. 更新:即使您在同一台机器上运行它,也请指定classpath。 It should work. 它应该工作。 Try it and let me know. 试试看,让我知道。

Update 2: >> but how can i run the jar then not from cmd but by double click? 更新2:>>但是,我怎么不能然后通过cmd而是双击运行jar?

NetBeans adds the jar to your classpath before running it. NetBeans在运行之前将jar添加到您的类路径中。 You have added the driver jar to the project in NetBeans. 您已将驱动程序jar添加到NetBeans中的项目中。 Let me know the result! 让我知道结果!

Update 3: 更新3:

Yes. 是。 do check the spellings. 检查拼写。 Make sure you use double quotes for paths that contain spaces. 确保对包含空格的路径使用双引号。

Try this - 尝试这个 -

java -classpath C:\Users\IMPREZA\Documents\attachments\lib\postgresql-9.2-1002.jdbc4.jar;.\LPGExerts.jar -jar LPGExerts.jar

with quotes (just a shot, no need since I dont see spaces in paths) 带引号(只是一个镜头,不需要,因为我没有在路径中看到空格)

java -classpath "C:\Users\IMPREZA\Documents\attachments\lib\postgresql-9.2-1002.jdbc4.jar;.\LPGExerts.jar" -jar LPGExerts.jar

Update 4: 更新4:

Now, i cant say anything. 现在,我什么也不能说。 has to be a spelling mistake. 必须是拼写错误。 Check all spellings, paths, etc. 检查所有拼写,路径等。

  1. Use the exact same jar as in Netbeans, and remember to update your -classpath argument to match the file name of the jar you added to your project in Netbeans. 使用与Netbeans中完全相同的jar,并记住更新-classpath参数以匹配您添加到Netbeans中的项目中的jar的文件名。 I am just making sure that we have a working jar in classpath. 我只是确保我们在classpath中有一个可工作的jar。 The jar you added in Netbeans is working. 您在Netbeans中添加的jar正在运行。

  2. Make sure your LPGExerts.jar is being rebuilt after every code change you made. 每次更改代码后,请确保重新构建LPGExerts.jar。

Do let me know if you find a solution. 如果您找到解决办法,请告诉我。

Update 5: You are welcome 更新5:不客气

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

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