2013-08-22 63 views
0

我已經創建了一個應用程序(使用Java),並使其受密碼保護:當我在Eclipse中運行/編譯我的代碼時,它工作正常,但是當我創建一個jar文件並運行該jar時,它不工作:它向我顯示當我創建了一個正常工作的第一罐子,但是當我剛剛加密碼重置功能,它只是停止工作罐子不工作?

起初:

答案之前,請記住,代碼工作正常後,初步框架,但沒有任何作品編譯器(即Eclipse),但在轉換爲jar時不工作。

轉換過程:File>Export>Runnable Jar(選項進行選擇 - package required libraries into jar

輸出在Eclipse工程 - 截屏圖像here http://i.stack.imgur.com/eVigb.jpg

一個無法運作的(從罐子執行) 截屏圖像here 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(); 
    }  
} 
+0

無法看到您的代碼或至少錯誤消息來回答。嘗試從命令行使用java.exe -jar –

+0

啓動JAR沒有顯示錯誤它的行爲與未在actionListener(實際上是actionPerformed方法)中寫入一樣。我會用代碼編輯我的問題 –

回答

0

從你的描述來看,這聽起來像你的jar沒有按照你的想法組裝。

如果面板沒有顯示出來,那麼你可能會缺少支持庫。

我會用jar文件打開並瀏覽內容。無論如何,當你學習Java時,它是一個有趣的練習(我注意到你是你的個人檔案中的學生)。瀏覽jar並確保它包含應該支持您的應用程序的所有內容。我的猜測是,你使用的任何框架都沒有完全複製到你的jar中。