我试图运行我在日食中制作的程序。已将其导出为可运行的jar文件,并将其保存在桌面上,但当我尝试从命令提示符运行它时,它说:"错误:无法访问jarfile Matador.jar"
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Password implements ActionListener {
private String Username = "hudhud";
private String Password = "fitness";
private JTextField txtUsername;
private JTextField txtPassword;
public static void main(String[] args){
Password gui = new Password();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel lblUsername = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
txtUsername = new JTextField(20);
txtPassword = new JTextField(20);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(this);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(this);
panel.add(lblUsername);
panel.add(txtUsername);
panel.add(lblPassword);
panel.add(txtPassword);
panel.add(btnLogin);
panel.add(btnCancel);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String msg;
if (cmd.equals("Login")) {
if (txtUsername.getText().equals(Username) && txtPassword.getText().equals(Password)) {
msg = "Welcome";
} else {
msg = "Login denied. The username or password is incorrect";
}
} else {
msg = "Where are you going, couldn't you guess the password and username??";
}
JOptionPane.showMessageDialog(null, msg);
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
该错误意味着无法找到jar文件。您必须像这样指定jar的路径:
java -jar path/To/Your/Jar/Matador.jar
答案 2 :(得分:0)
项目结构:
c:\project\Matador.jar
cd c:\project
java -jar Matador.jar