在执行JAR文件时无法找到或加载主类

4

我完成了我的项目(在NetBeans中),并导出了Jar文件(在导出Jar之前,我正确地在项目属性中设置了我的主类):

enter image description here

现在,这是我的JAR文件:

enter image description here

当我运行Jar时(在命令行页面上),会显示以下错误:

Could not find or load main Class on JAR executing

这是我的MANIFEST.MF信息:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.1
Created-By: 1.7.0_11-b21 (Oracle Corporation)
Class-Path: lib/mysql-connector-java-5.1.18-bin.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: Project.LoginFrame

这里是我所有的课程:

enter image description here

我也尝试在命令行中运行:

enter image description here

我的项目此时已经执行,但是所有图片(在一个文件夹中的)都没有显示,同时还发生了sql 异常

enter image description here

更新:

package Project;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.*;

public class LoginFrame extends javax.swing.JFrame implements ActionListener {

String dbUrl = "jdbc:mysql://localhost/Library";
private char[] Password;
private JButton ClearBtn,ExitBtn,LoginBtn;
private JLabel ErrorLbl;
private JComboBox comboBox;
private JLabel lbl1;
private JLabel lbl2;
private JLabel lbl3;
private String[] enterUserInf = new String[4];
private JPasswordField passwordField;
private JTextField usernameTF;

public LoginFrame() {
    initComponents();
    this.getRootPane().setDefaultButton(LoginBtn);
    comboBox.addActionListener(this);
    setVisible(true);
}

public static void main(String args[]) throws IOException {
    new LoginFrame();
}

private void initComponents() {
    //...
}

private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {

    try {
        if (comboBox.getSelectedIndex() == 0) {
            ErrorLbl.setText("Select A Model...");
            ErrorLbl.setVisible(true);
            return;
        }

        Password = passwordField.getPassword();

        if (!passwordControl()) {
            return;
        }

        if (comboBox.getSelectedIndex() == 1) {
            if (userEnterCondition(Password)) {
                this.setVisible(false);
                new BookPage_User(enterUserInf, enterUserInf[0]);
            } else {
                ErrorLbl.setText("Incorrect Password!");
            }
        }

        if (comboBox.getSelectedIndex() == 2) {
            if (adminEnterCondition(Password)) {
                this.setVisible(false);
                new MainFrame().setVisible(true);
            } else {
                ErrorLbl.setText("Incorrect Password!");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        ErrorLbl.setText("Enter Correct Input");
    }

}

private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
    System.exit(0);
}

private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {
    passwordField.setText("");
}

public boolean passwordControl() {
    Password = passwordField.getPassword();
    if (String.valueOf(Password).trim().isEmpty()) {
        ErrorLbl.setText("Empty Password!");
        ErrorLbl.setVisible(true);
        return false;
    }
    return true;
}

public boolean adminEnterCondition(char[] pass) {
    Connection con;
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    String query = "Select * From adminLogin";
    String password = null;
    try {
        con = DriverManager.getConnection(...);
        preparedStatement = con.prepareStatement(query);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            password = resultSet.getString("ID");  // Get column value by name column name
            if (password.equalsIgnoreCase(String.valueOf(pass))) {
                return true;
            }
        }

    } catch (SQLException sqle) {
        sqle.printStackTrace();
        return false;
    }
    return false;
}

public boolean userEnterCondition(char[] pass) {
    Connection con;
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    String query = "Select * from users";
    String password = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(...);
        preparedStatement = con.prepareStatement(query);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            password = resultSet.getString("User_ID");
        }

    } catch (SQLException sqle) {
        return false;
    } catch (ClassNotFoundException cnfe) {
    }
    return false;
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == comboBox) {
        if (comboBox.getSelectedIndex() == 1) {
            usernameTF.setText("User");
            usernameTF.setEditable(false);
            passwordField.requestFocusInWindow();
            ErrorLbl.setVisible(false);
        } else if (comboBox.getSelectedIndex() == 2) {
            passwordField.requestFocusInWindow();
        }
    }
}
}

你将它导出为“可运行的jar文件”还是普通的jar文件? - ltalhouarne
尝试不使用压缩,我记得以前使用压缩的JAR文件会出现问题。 - user784540
@user3808021:以前从未使用过任何IDE,所以无法多说NetBeans的工作原理,但是很快我会尝试创建一个小项目,你可以在命令提示符上运行。我会尽快告诉你。 - nIcE cOw
@nIcEcOw 终于成功地运行了我的项目的 jar 文件,但是有一个小问题,jar 文件无法连接到我的数据库(MySQL),为什么? - user3808021
@user3808021:你必须将MySql connector.jar加入到JAR文件的类路径中,就像我在我的应用程序中使用derby.jar一样,确保阅读我们创建的manifest.txt - nIcE cOw
@user3808021:为了更简单的解决方法,只需将此连接器JAR文件放置在应用程序的JAR文件旁边。并且在清单中只需编写Class-Path: mysql-connector-java-5.1.18-bin.jar - nIcE cOw
2个回答

4

使用WinRAR或类似的程序打开您的.jar文件,然后进入META-INF文件夹并打开MANIFEST.MF文件。确保"Main-Class: your.class.path"属性正确。


所以,你可能没有安装适用于 MySQL 的 JDBC 驱动程序,或者没有正确配置它! - Am_I_Helpful
@shekharsuman 当我使用IDE运行我的项目时,JDBC驱动程序可以正常工作。 - user3808021
可能是JAR文件出了问题! - Am_I_Helpful
Sajjad-你介意分享LoginFrame的代码吗? - Am_I_Helpful
任何与LoginFrame相关的内容——它可能包含jdbc部分或其他任何内容! - Am_I_Helpful
我的主类没问题,但是我还是得到了同样的错误? - Naveen - நவீன்

2

因此,这里出现了一个问题的错误——为什么你在代码中提到了con = DriverManager.getConnection(...);

因此,你的代码捕获了SQL Exception!这就是错误的来源 :-

public boolean userEnterCondition(char[] pass) {
Connection con;
PreparedStatement preparedStatement;
ResultSet resultSet;
String query = "Select * from users";
String password = null;
try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(...);     //error in this line

................. and so on.

用以下内容替换:
con =DriverManager.getConnection("jdbc:mysql://localhost/Library?" +
                               "user=yourusername&password=yourpassword");

我希望这能有所帮助。如果仍然出现错误,请在下方评论!

2
你绝不能捕获异常而不打印或记录任何信息,否则你会遇到麻烦。所以至少要打印堆栈跟踪! - Lars
当我再次运行Jar时,显示了相同的输出:(无法找到或加载主类C:\ User\ Sajjad\Desktop\UniversityProjects\dist\UniversityProject.jar)。 - user3808021
@Lars,我并没有收到任何异常信息,只是在运行Jar文件后,在命令行中显示了这个错误消息,然后就消失了。 - user3808021
我正在使用 NetBeans,通过拖放方式创建 LoginFrame 类的可视化组件(例如按钮、文本框等),然后手动添加按钮动作代码。 - user3808021
@shekharsuman 感谢您迄今为止解决我的问题的勤奋。 - user3808021
显示剩余10条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接