0

I'm trying to run a program I've made in eclipse. Have exported it as a runnable jar file, and saved it on the desktop, but when I try to run it from the command prompt it says : "Error: Unable to access 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);
}

}

Mr.H123
  • 81
  • 1
  • 1
  • 10
  • 2
    You need to tell us what you run from the command prompt, what OS, etc – Robert Moskal Jan 29 '15 at 15:22
  • I've windows 8.1, and the command I write is "java -jar Matador.jar". I have also tried to put "" around Matador.jar – Mr.H123 Jan 29 '15 at 15:24
  • Please tell us the layout of your folders. Where is `Matador.jar` located? Are you inside the folder when you try to run it? –  Jan 29 '15 at 15:25
  • You won't need the quotes around the name of the jar. Duplicate of http://stackoverflow.com/questions/6780678/run-class-in-jar-file – unigeek Jan 29 '15 at 15:31
  • I've told you that I saved it on the desktop :) – Mr.H123 Jan 29 '15 at 15:39

3 Answers3

1

Do this:

java -jar <jar-file-name>.jar

Just make sure you are in the right directory.

Duplicate question: Run jar file in command prompt

Community
  • 1
  • 1
mmaynar1
  • 326
  • 4
  • 14
0

That error means, that the jar file couldn't be found. You have to specify the path to your jar like that:

java -jar path/To/Your/Jar/Matador.jar
Christian
  • 22,585
  • 9
  • 80
  • 106
0

Project structure:

c:\project\Matador.jar

  1. Start command prompt,
  2. cd c:\project
  3. java -jar Matador.jar
Greg
  • 1,671
  • 2
  • 15
  • 30