通过命令行运行java类

通过命令行运行java类,java,Java,我尝试通过以下命令行运行我的类(LoginFrame.java): (我的Java和Javac命令在命令行中工作) 首先,我使用javac命令编译LoginFrame类:(这个类有main方法) 并使用Java运行该类: 我将JDK1.7的bin文件夹设置在evrironment变量路径中 (我使用Windows7) 怎么了 LoginFrame类: package Project; public class LoginFrame extends javax.swing.JFrame i

我尝试通过以下命令行运行我的类(
LoginFrame.java
):

(我的
Java
Javac
命令在命令行中工作)

首先,我使用
javac
命令编译
LoginFrame
类:(这个类有main方法)

并使用
Java
运行该类:

我将JDK1.7的
bin
文件夹设置在
evrironment变量路径中

(我使用Windows7)

怎么了

LoginFrame类:

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();
    }
}
}
}


您必须将目录更改为
src
目录,然后调用`javac-cp./Project.LoginFrame.java

您必须将目录更改为
src
目录,然后调用`javac-cp./Project.LoginFrame.java

。首先,您的程序没有编译,因为它包含错误编译阶段出错,因此不会生成
.class
文件。首先尝试修复错误,然后使用
javac
再次编译,然后使用
java
@SayemAhmed运行编译后的类。当我尝试使用
IDE
时,为什么
LoginFrame
类会运行?@SayemAhmed My
NewBook\u User
大型机
以及
SetMyImage
类都存在于目录中,但是为什么会出现错误呢?我建议修复导入并提供三个缺少的类。作为响应,您关闭了命令行并重新打开它。那没用。尝试添加缺少的导入。你还缺了三节课。您可以尝试整天运行该程序,但在修复编译错误之前,它不会运行。首先,您的程序没有编译,因为它包含错误。在编译阶段,您有编译错误,因此不会生成
。class
文件。首先尝试修复错误,然后使用
javac
再次编译,然后使用
java
@SayemAhmed运行编译后的类。当我尝试使用
IDE
时,为什么
LoginFrame
类会运行?@SayemAhmed My
NewBook\u User
大型机
以及
SetMyImage
类都存在于目录中,但是为什么会出现错误呢?我建议修复导入并提供三个缺少的类。作为响应,您关闭了命令行并重新打开它。那没用。尝试添加缺少的导入。你还缺了三节课。您可以尝试整天运行此程序,但在修复编译错误之前,它不会运行。对不起,我如何更改我的目录?因为您的答案有误导性,所以被否决。在他修复编译错误之前,这不会有任何帮助。@DonBranson我的代码是否可能在我的
IDE
中没有任何编译错误,但在命令行编译中存在一些编译错误?我不确定您的IDE为什么不会捕获编译错误。您显然缺少Swing类的导入。这个答案真的对您有帮助吗?或者您只是为了@user3808021而接受了它?对不起,我如何更改我的目录?因为您的答案有误导性,所以被否决了。在他修复编译错误之前,这不会有任何帮助。@DonBranson我的代码是否可能在我的
IDE
中没有任何编译错误,但在命令行编译中存在一些编译错误?我不确定您的IDE为什么不会捕获编译错误。很明显,您缺少Swing类的导入。这个答案真的对您有帮助吗?或者您只是为了@user3808021而接受它?