2

I try to run my class (LoginFrame.java) by command line in here:

enter image description here

(My Java and Javac commands works in command line)

First i use javac command to compile LoginFrame Class: (this class has main method)

enter image description here

And use Java to run the class:

enter image description here

I set bin folder of jdk 1.7 in evrironment variable path

(I use windows 7)

What is wrong?

LoginFrame class:

package Project;

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();
    }
}
}
}
  • 1
    Well for one thing your program is not compiled since it contains errors. – Esben Skov Pedersen Jul 20 '14 at 11:06
  • 4
    You have compilation error during the compilation stage, so no `.class` file will be generated. Try fixing the errors first, then compile again using `javac`, and then run the compiled class with `java`. – MD Sayem Ahmed Jul 20 '14 at 11:07
  • @SayemAhmed why `LoginFrame` class runs when i try with an `IDE`? –  Jul 20 '14 at 11:09
  • @SayemAhmed My `NewBook_User` and `MainFrame` and `SetMyImage` classes are exists in directory, But why has error? –  Jul 20 '14 at 11:10
  • @user3808021: Please post the respective code too – nIcE cOw Jul 20 '14 at 11:12
  • @user3808021: @user3808021: Please post the respective code too. Moreover, since current class uses external classes, you need to first reach the root of the project. There with `dir`, output will be, `bin`, `src` and blahblah. Here issue the command: `javac -d bin src\Project\*.java`. Since you using `package`, hence now go inside `bin` folder and type `java Project.ClassNameHavingMainMethod` – nIcE cOw Jul 20 '14 at 11:17
  • Your class has errors and won't compile to a .class file. Since you have no .class file, you can't run the .class file. Fix the imports, provide the 3 missing classes, and try again. – Don Branson Jul 20 '14 at 11:26
  • @DonBranson Now, i close command line and opened again and try again, now the result is this: `Exception in thread main: java.lang.NoClassDefFoundError: LoginFrame (wrong name: Project\LoginFrame)` –  Jul 20 '14 at 11:28
  • @user3808021: Please See my updated comment: Use `java Project.LoginFrame`, to run the program :-) – nIcE cOw Jul 20 '14 at 11:29
  • @nIcEcOw Ok, Now my class runs correctly, But because it use(My Whole Project ) a `jar` file for `JDBC connector`, It cannot connect to database! –  Jul 20 '14 at 11:32
  • @user3808021: Post a new question with relevant details :-) – nIcE cOw Jul 20 '14 at 11:33
  • @nIcEcOw Ok, I asked on here before, But no one can help me! http://stackoverflow.com/questions/24740803/could-not-find-or-load-main-class-on-jar-executing –  Jul 20 '14 at 11:36
  • 1
    Well, I suggested fixing the imports and providing the three missing classes. In response you closed the command line and re-opened it. That just won't help. Try adding the missing imports. You're also missing three classes. You can try to run this all day long, but it's not going to run until you fix the compile errors. – Don Branson Jul 20 '14 at 11:38
  • @DonBranson My problem solved , Please help me in here: http://stackoverflow.com/questions/24740803/could-not-find-or-load-main-class-on-jar-executing Thanks –  Jul 20 '14 at 11:39
  • Why are taking tension for running it using command line.And,Don Branson ic correct about it @user3808021!What additional problems are you facing! – Am_I_Helpful Jul 20 '14 at 11:41
  • @shekharsuman I can't run my `jar` file that export from project yet! it says: `cannot find or load main class` –  Jul 20 '14 at 11:44

1 Answers1

-1

You have to change the directory to your src directory and than call `javac -cp ./ Project.LoginFrame.java

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Sorry, How can i change my directory? –  Jul 20 '14 at 11:37
  • 1
    Downvoted because your answer is misleading. This won't help until he fixes the compile errors. – Don Branson Jul 20 '14 at 11:42
  • @DonBranson Is it possible my code hasn't any compilation Error in my `IDE` but exist some compilation Errors in command line compilation? –  Jul 20 '14 at 11:47
  • I'm not sure why your IDE wouldn't catch the compile errors. You're clearly missing imports for Swing classes. – Don Branson Jul 20 '14 at 11:51
  • @DonBranson I remove import statements here to be short. –  Jul 20 '14 at 11:52
  • Ah, well, that's probably not helpful. The question is still very long. You need to distill the class down to a few lines that still reproduce the problem. You could probably do that with a "Hello world." class. – Don Branson Jul 20 '14 at 11:53
  • 2
    Did this answer really helped you OR you accepted it just for the sake of it @user3808021? – Am_I_Helpful Jul 20 '14 at 12:05