4

I finished my project (in NetBeans) and i export Jar file (i set my main class in project properties correctly before exporting Jar):

enter image description here

Now, This is my JAR:

enter image description here

This Error shown when i run the Jar (in command line page):

Could not find or load main Class on JAR executing

This is my MANIFEST.MF information:

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:

enter image description here

I try in command line too:

enter image description here

My project executed at this time, But all pictures (that are in a folder) not displayed, and also an sql Exception happens:

enter image description here

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();
        }
    }
}
}
  • Did you export it as "a runnable jar file" or just a jar file? – ltalhouarne Jul 14 '14 at 16:06
  • try without compression, I recall there were problems with compressed jars before –  Jul 14 '14 at 16:06
  • @user3808021: Never did used any IDE before, so cannot tell much about how NetBeans work, but in some time, will try to create one small project, that you might can run on command prompt. Will let you know soon. – nIcE cOw Jul 20 '14 at 13:05
  • @nIcEcOw Finally i can run `jar` file of my project successfully, But there is one little problem, jar file cannot connect to my Database (MySQL), Why? –  Jul 21 '14 at 12:38
  • @user3808021: You have to keep the MySql connector.jar on the classpath of your JAR file, just like what I did, with `derby.jar` with my application, do read that `manifest.txt` we created – nIcE cOw Jul 21 '14 at 12:40
  • @user3808021: for simpler workaround, just place this connector JAR file alongside the application's JAR file. And in manifest just write `Class-Path: mysql-connector-java-5.1.18-bin.jar` – nIcE cOw Jul 21 '14 at 12:52

2 Answers2

4

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.

Lars
  • 1,750
  • 2
  • 17
  • 26
2

So,here comes an error to the problem---why have you mentioned con = DriverManager.getConnection(...); in your code?

And hence your code is catching SQL Exception!!! This is the source of error :-

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!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 2
    And you should never catch an exception without printing or logging anything this will get you in trouble as you see. So at least print the stacktrace! – Lars Jul 17 '14 at 10:07
  • When i run `Jar` again, the same output showed: (`Could not find o load main class C:\ User\ Sajjad\Desktop\UniversityProjects\dist\UniversityProject.jar`) –  Jul 17 '14 at 22:37
  • @Lars I didn't get any exception, Just this error message shown at command line after i run the `Jar` file and then disappear. –  Jul 17 '14 at 22:40
  • Are you using IntelliJ IDEA for it?Also,how did you created the code for LoginFrame class,using drag and drop or by manually adding the code? – Am_I_Helpful Jul 18 '14 at 04:16
  • I am using `NetBeans` and i create `LoginFrame` class visually components (such as buttons, textfields and etc) by drag and drop and then add buttons action codes manually. –  Jul 18 '14 at 07:58
  • Seriously,I am done with it.I couldn't help anymore as I can't solve anymore.If you can search something else,or if can transfer me files,I would try one last time for you! – Am_I_Helpful Jul 18 '14 at 08:00
  • @shekharsuman Thanks for diligence to solve my problem so far. –  Jul 18 '14 at 10:25
  • if you can transfer me files,I would try one last time for you! – Am_I_Helpful Jul 18 '14 at 10:27
  • No,the whole project folder---the jar file! – Am_I_Helpful Jul 18 '14 at 11:52
  • My email-id is `shekhar.suman396@gmail.com`---but please send from your own id so that I can make you my friend on Google+ and sometimes have enjoyable chat! – Am_I_Helpful Jul 18 '14 at 11:54
  • @shekharsuman unfortunately i can't send whole project for security reasons, Because this is not a personal project and other part of project has some security codes. Sorry, I think you need just this class. –  Jul 18 '14 at 12:06
  • No,then leave it as I have already view that class here itself! – Am_I_Helpful Jul 18 '14 at 12:31
  • @shekharsuman Should i set main class variable or other stuff like this on command line? –  Jul 18 '14 at 12:37
  • My `Java` and `Javac` statements works correctly in command line. –  Jul 18 '14 at 12:38
  • Oh,that's fine because your program this time did not give exception,SO it means they are mentioned in the path variable! One thing---try resolving with your mates involved in the project. Also,a request to upvote,if not accept, for such unsolvable question!BTW,it depends on your wish! – Am_I_Helpful Jul 18 '14 at 12:40