我创建了一个应用程序(在 Java 中)并使其受密码保护:当我在 Eclipse 中运行/编译我的代码时它工作正常,但是当我创建一个 jar 文件并运行该 jar 时它不起作用:它向我显示了初始帧但在那之后没有任何效果:
最初,当我创建第一个 jar 时,它可以正常工作,但是当我添加密码重置功能时,它就会停止工作:
在回答之前,请记住代码在编译器(即 Eclipse)中可以正常工作,但在转换为 jar 时无法正常工作。
转换程序:(File>Export>Runnable Jar
选择的选项 - package required libraries into jar
)
在 Eclipse 中工作的输出 -此处为屏幕截图
一个不工作的(从 jar 执行)屏幕截图在这里
一段代码相关(考虑声明和初始化的每一件事)
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();
}
}