簡體   English   中英

通過命令行運行Java類

[英]Run java class by command line

我嘗試在此處通過命令行運行我的類( LoginFrame.java ):

在此處輸入圖片說明

(我的JavaJavac命令在命令行中有效)

首先,我使用javac命令來編譯LoginFrame類:(此類具有main方法)

在此處輸入圖片說明

並使用Java運行該類:

在此處輸入圖片說明

我在evrironment variable path設置了jdk 1.7的bin文件夾

(我使用Windows 7)

怎么了?

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

您必須將目錄更改為src目錄,然后調用`javac -cp ./ Project.LoginFrame.java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM