2017-04-17 227 views
0

我正在为android操作系统构建一个电子邮件发送应用程序。我在我的libs文件夹中有三个jar:activation.jar,additional.jar和mail.jar。我也将这三个jar包含在项目的构建路径中,称为“参考库”。当我运行这个应用程序并尝试发送邮件时,我在LogCat上遇到以下错误。致命异常:java.lang.NoClassDefFoundError:javax.activation.DataHandler

FATAL EXCEPTION: java.lang.NoClassDefFoundError: javax.activation.DataHandler

我完全难过,因为许多人都有相同的NoClassDefFoundError错误。因为我有所需的jar包含在我的课堂构建路径库,我认为它可以作为链接描述运行时错误:https://stackoverflow.com/a/34419/3576562 注:我跟着这个机器人电子邮件教程:http://www.chengxuyuans.com/qa/android/85326.html

Activity.java:

Button dialogButton1 = (Button) dialog.findViewById(R.id.SendButton); 
       dialogButton1.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         //Toast.makeText(getApplicationContext(), "Forgot-Password-Request button", Toast.LENGTH_LONG).show(); 
         final EditText email = (EditText) dialog.findViewById(R.id.EditEmail); 
         if(email.getText() != null && email.getText().toString().trim().length() > 0){//check for email written. 
          Runnable run = new Runnable() { 
           public void run() { 
            SendPasswordRequestEmail(email.getText().toString()); 
            dialog.dismiss(); 
           } 
          }; 
          (new Thread(run)).start(); 

         } 
        } 
       }); 

public void SendPasswordRequestEmail(String emailAddress){ 
     try{ 
      GMailSender sender = new GMailSender("[email protected]", "email password"); 
      sender.sendMail("This is Subject", 
        "This is Body", 
        "[email protected]", 
        "[email protected]"); 
     }catch (Exception ex) { 
      Log.e("MailChimp", "SendEmail Method-Exception forgot password email: " + ex.getMessage()); 
      showResult("SendEmail Method-Exception forgot password email: "+ex.getMessage(), "Error"); 
      ex.printStackTrace(); 
     } 
    } 

GMailSender.java:

package com.example.proactiveregistrationapplication; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.mail.Message; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.security.Security; 
import java.util.Properties; 

public class GMailSender extends javax.mail.Authenticator { 
    private String mailhost = "smtp.gmail.com"; 
    private String user; 
    private String password; 
    private Session session; 

    static { 
     Security.addProvider(new com.example.proactiveregistrationapplication.JSSEProvider()); 
    } 

    public GMailSender(String user, String password) { 
     this.user = user; 
     this.password = password; 

     Properties props = new Properties(); 
     props.setProperty("mail.transport.protocol", "smtp"); 
     props.setProperty("mail.host", mailhost); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.quitwait", "false"); 

     session = Session.getDefaultInstance(props, this); 
    } 

    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(user, password); 
    } 

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception { 
     try{ 
     MimeMessage message = new MimeMessage(session); 
     DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); 
     message.setSender(new InternetAddress(sender)); 
     message.setSubject(subject); 
     message.setDataHandler(handler); 
     if (recipients.indexOf(',') > 0) 
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); 
     else 
      message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); 
     Transport.send(message); 
     }catch(Exception e){ 

     } 
    } 

    public class ByteArrayDataSource implements DataSource { 
     private byte[] data; 
     private String type; 

     public ByteArrayDataSource(byte[] data, String type) { 
      super(); 
      this.data = data; 
      this.type = type; 
     } 

     public ByteArrayDataSource(byte[] data) { 
      super(); 
      this.data = data; 
     } 

     public void setType(String type) { 
      this.type = type; 
     } 

     public String getContentType() { 
      if (type == null) 
       return "application/octet-stream"; 
      else 
       return type; 
     } 

     public InputStream getInputStream() throws IOException { 
      return new ByteArrayInputStream(data); 
     } 

     public String getName() { 
      return "ByteArrayDataSource"; 
     } 

     public OutputStream getOutputStream() throws IOException { 
      throw new IOException("Not Supported"); 
     } 
    } 
} 

JSSEProvider.java:

package com.example.proactiveregistrationapplication; 

import java.security.AccessController; 
import java.security.Provider; 

public final class JSSEProvider extends Provider { 

    public JSSEProvider() { 
     super("HarmonyJSSE", 1.0, "Harmony JSSE Provider"); 
     AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() { 
      public Void run() { 
       put("SSLContext.TLS", 
         "org.apache.harmony.xnet.provider.jsse.SSLContextImpl"); 
       put("Alg.Alias.SSLContext.TLSv1", "TLS"); 
       put("KeyManagerFactory.X509", 
         "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl"); 
       put("TrustManagerFactory.X509", 
         "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl"); 
       return null; 
      } 
     }); 
    } 
} 

logcat的错误:

04-17 16:15:04.395: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.395: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.415: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory 
04-17 16:15:04.415: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error 
04-17 16:15:04.435: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory 
04-17 16:15:04.435: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error 
04-17 16:15:04.435: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory 
04-17 16:15:04.435: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error 
04-17 16:15:04.445: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory 
04-17 16:15:04.445: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error 
04-17 16:15:04.455: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory 
04-17 16:15:04.455: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error 
04-17 16:15:04.455: W/(26895): Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory 
04-17 16:15:04.455: W/art(26895): Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.465: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.475: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.485: I/art(26895): Rejecting re-init on previously-failed class java.lang.Class<javax.activation.DataHandler> 
04-17 16:15:04.485: E/AndroidRuntime(26895): FATAL EXCEPTION: Thread-6719 
04-17 16:15:04.485: E/AndroidRuntime(26895): Process: com.example.proactiveregistrationapplication, PID: 26895 
04-17 16:15:04.485: E/AndroidRuntime(26895): java.lang.NoClassDefFoundError: javax.activation.DataHandler 
04-17 16:15:04.485: E/AndroidRuntime(26895): at com.example.proactiveregistrationapplication.GMailSender.sendMail(GMailSender.java:52) 
04-17 16:15:04.485: E/AndroidRuntime(26895): at com.example.proactiveregistrationapplication.UnsubscribeActivity.SendPasswordRequestEmail(UnsubscribeActivity.java:216) 
04-17 16:15:04.485: E/AndroidRuntime(26895): at com.example.proactiveregistrationapplication.UnsubscribeActivity$3$1$1.run(UnsubscribeActivity.java:83) 
04-17 16:15:04.485: E/AndroidRuntime(26895): at java.lang.Thread.run(Thread.java:818) 
04-17 16:15:04.635: D/ViewRootImpl(26895): MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 759) or=1 
04-17 16:15:04.655: I/Timeline(26895): Timeline: Activity_idle id: [email protected] time:49039028 
04-17 16:15:04.945: D/ViewRootImpl(26895): #3 mView = null 
04-17 16:15:04.965: E/WindowManager(26895): android.view.WindowLeaked: Activity com.example.proactiveregistrationapplication.UnsubscribeActivity has leaked window com.android.internal.policy.PhoneWindow$DecorView{4f688cf V.E...... R......D 0,0-1002,565} that was originally added here 
04-17 16:15:04.965: E/WindowManager(26895):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:565) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.app.Dialog.show(Dialog.java:350) 
04-17 16:15:04.965: E/WindowManager(26895):  at com.example.proactiveregistrationapplication.UnsubscribeActivity$3.onClick(UnsubscribeActivity.java:99) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.view.View.performClick(View.java:5697) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.widget.TextView.performClick(TextView.java:10826) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.view.View$PerformClick.run(View.java:22526) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.os.Handler.handleCallback(Handler.java:739) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.os.Handler.dispatchMessage(Handler.java:95) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.os.Looper.loop(Looper.java:158) 
04-17 16:15:04.965: E/WindowManager(26895):  at android.app.ActivityThread.main(ActivityThread.java:7224) 
04-17 16:15:04.965: E/WindowManager(26895):  at java.lang.reflect.Method.invoke(Native Method) 
04-17 16:15:04.965: E/WindowManager(26895):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
04-17 16:15:04.965: E/WindowManager(26895):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

回答

0

原因:确切病因不明给我。我认为它可能是运行时类路径中的配置错误,在项目 - >(右键单击) - >运行 - >运行配置中找到,也在https://stackoverflow.com/a/34419/3576562中注明。

额外努力,我试过:在“运行配置”窗口中,我确定名字我的项目的“Android应用程序”选项卡,并Android->项目下的项目的名称相匹配:选项卡。注意:这并没有解决我的NoClassDefFoundError错误,最初在我的项目中,“Run Configurations”窗口的名称不匹配。

替代解决这个“java.lang.NoClassDefFoundError:的javax.activation.DataHandler”运行时错误是创建一个新的Android项目,并从原来的项目复制所有内容,包括原始的源代码和Android清单。

我也使用了Alternative Email解决方案;