Java 无法在 JAR 执行时找到或加载主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24740803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find or load main Class on JAR executing
提问by
I finished my project (in NetBeans
) and i export Jar
file (i set my main class in project properties correctly before exporting Jar
):
我完成了我的项目(在NetBeans
)并导出Jar
文件(我在导出之前在项目属性中正确设置了我的主类Jar
):
Now, This is my JAR
:
现在,这是我的JAR
:
This Error shown when i run the Jar
(in command line page):
当我运行Jar
(在命令行页面中)时显示此错误:
Could not find or load main Class on JAR executing
Could not find or load main Class on JAR executing
This is my MANIFEST.MF
information:
这是我的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
All of my classes are here:
我所有的课程都在这里:
I try in command line too:
我也在命令行中尝试:
My project executed at this time, But all pictures (that are in a folder) not displayed, and also an sql Exception
happens:
我的项目此时执行,但所有图片(在文件夹中)都没有显示,并且还sql Exception
发生了:
Update:
更新:
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();
}
}
}
}
采纳答案by Lars
Open your jar file with WinRAR or a similar program. Then go to the META-INF folder and open the MANIFEST.MF. Make sure the property "Main-Class: your.class.path" is correct.
使用 WinRAR 或类似程序打开您的 jar 文件。然后转到 META-INF 文件夹并打开 MANIFEST.MF。确保属性“Main-Class: your.class.path”是正确的。
回答by Am_I_Helpful
So,here comes an error to the problem---why have you mentioned
con = DriverManager.getConnection(...);
in your code?
所以,这里有一个错误的问题---你为什么con = DriverManager.getConnection(...);
在你的代码中提到
?
And hence your code is catching SQL Exception
!!! This is the source of error :-
因此,您的代码正在捕获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.
Replace it with
将其替换为
con =DriverManager.getConnection("jdbc:mysql://localhost/Library?" +
"user=yourusername&password=yourpassword");
I hope this helps.If still getting error,please comment below!
我希望这会有所帮助。如果仍然出现错误,请在下面发表评论!