我完成了我的项目(在NetBeans
中),并导出了Jar
文件(在导出Jar
之前,我正确地在项目属性中设置了我的主类):
现在,这是我的JAR
文件:
当我运行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
这里是我所有的课程:
我也尝试在命令行中运行:
我的项目此时已经执行,但是所有图片(在一个文件夹中的)都没有显示,同时还发生了sql 异常
:
更新:
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
文件无法连接到我的数据库(MySQL),为什么? - user3808021derby.jar
一样,确保阅读我们创建的manifest.txt
。 - nIcE cOwClass-Path: mysql-connector-java-5.1.18-bin.jar
。 - nIcE cOw