0

我创建了一个应用程序(在 Java 中)并使其受密码保护:当我在 Eclipse 中运行/编译我的代码时它工作正常,但是当我创建一个 jar 文件并运行该 jar 时它不起作用:它向我显示了初始帧但在那之后没有任何效果:

最初,当我创建第一个 jar 时,它可以正常工作,但是当我添加密码重置功能时,它就会停止工作:

在回答之前,请记住代码在编译器(即 Eclipse)中可以正常工作,但在转换为 jar 时无法正常工作。

转换程序:(File>Export>Runnable Jar选择的选项 - package required libraries into jar

在 Eclipse 中工作的输出 -此处为屏幕截图 http://i.stack.imgur.com/eVigb.jpg

一个不工作的(从 jar 执行)屏幕截图在这里 http://i.stack.imgur.com/9oDOA.jpg

一段代码相关(考虑声明和初始化的每一件事)

public JFrame psFrame=new JFrame("Password Check");
    private JPasswordField psField;
        private static int  tries=5;
    static boolean condition=false;
    private String Check,password;


        //////////// CONSTRUCTOR  ///////////
public  MenusAndButtons(){
    super("Explorer");
    extraString=null;
    try {passwordSetter();}catch (FileNotFoundException e){e.printStackTrace();}
    paswardCheck();

}


public synchronized  void  paswardCheck(){


Check=null;
psFrame.setSize(400, 120);
psFrame.setLocation(480, 300);

psField=new JPasswordField("",30); 
psField.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==psField){ 
            Check=e.getActionCommand();

    if(tries!=1){


                if(Check!=null){


                             if(password.equals(Check)){
                             JOptionPane.showMessageDialog(null, "Welcome to Explorer ", "Welcome", JOptionPane.INFORMATION_MESSAGE);
                             condition=true;
                             psFrame.dispose();
                             Mnb();}
                             else{tries--;
                             JOptionPane.showMessageDialog(null,"You Entered a Wrong Password.\nYou are Left With " +tries+ " more Tries.\nPlease complete The Password Check with Correct Password\n", "Warrnning ",JOptionPane.WARNING_MESSAGE);  
                             psField.setText(null); }


                }else{JOptionPane.showMessageDialog(null,"You Entered Nothing in Password Field.\nPlease Enter a Password.","Null value Password Check",JOptionPane.ERROR_MESSAGE); 
                psField.setText(null);}


    }else{JOptionPane.showMessageDialog(null,"You Entered a Wrong Password 5 Times.\nExplorer is Quiting Now.","Password Check Failed ",JOptionPane.ERROR_MESSAGE); 
    psFrame.dispose();
    System.exit(0);}


    }  

    }});
psFrame.setLayout(new FlowLayout());
psFrame.add(new JLabel("Enter Password"));
psFrame.add(psField);
psFrame.setResizable(false);
psFrame.setVisible(true);


}




public void passwordSetter() throws FileNotFoundException{

    Scanner sc=new Scanner(new File("PasswordContainer.txt"));
    while(sc.hasNext())
        password=sc.nextLine();
    sc.close();

}

public void passwordresetter() throws Exception{


    String old=JOptionPane.showInputDialog(null,"Please Enter current Password","Current Password Check",JOptionPane.CANCEL_OPTION);
    if(old.equals(password)){
    String usererntry=JOptionPane.showInputDialog(null, "Please Enter new Password","Reset Password",JOptionPane.CANCEL_OPTION);

    if(usererntry.length()>=6){
    Formatter fp=new Formatter("PasswordContainer.txt");
    fp.format("%s",usererntry);
    fp.close();
    JOptionPane.showMessageDialog(null,"You've successfully Change Your Password","Password Change Conformation",JOptionPane.PLAIN_MESSAGE);
}else{  
    JOptionPane.showMessageDialog(null,"You Password Must Contain at least 6 Characters\nPlease Retype the New Password ");
    passwordresetter();}
        }


    else{JOptionPane.showMessageDialog(null,"You Entered Wrong Password\nPlease Retype the Current Password ");
    passwordresetter();
  }    
}
4

1 回答 1

0

根据您的描述,听起来您的罐子不像您想象的那样组装。

如果没有显示面板,那么您可能缺少支持库。

我会用 jar 文件打开并浏览内容。无论如何,当您学习 Java 时,它是一个有趣的练习(我注意到您的个人资料中您是一名学生)。浏览 jar 并确保它包含支持您的应用程序所需的所有内容。我的猜测是,您使用的任何框架都没有完全复制到您的 jar 中。

于 2013-08-24T13:12:27.843 回答