2014-11-25 56 views
0

我正在寫一個簡單的類來發送電子郵件,所以需要外部JAR。我所做的是在eclipse項目中添加了外部JARS。這是工作。NoClassDefFoundError當我通過commnad線呼叫班級

但當我從命令行調用類它給NoClassDefFoundError的:javax.mail.Address。這裏有兩件事,我已經複製了Java Lib文件夾中的所有JAR,其次是javax.mail.Address沒有在代碼中直接引用。任何想法什麼是woring或如何解決它。

該類有一個主要功能,工作正常,沒有電子郵件代碼。


P.S

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.Enumeration; 
import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class EmailNotifications { 

    protected static String  message_loseEvent ; 
    protected static Properties properties; 
    protected static Connection connection; 
    protected static Session session; 

    public static void main(String [] args)throws Exception 
    { 
     loadPropertiesFromXML("applicatiosnSettings.xml"); 
     setupMailServer(); 
     _openDBConnection(); 

     ResultSet R = _executeQuery(getQuery("TodaysLossEventQuery") ); 
     while(R.next()) { 
        message_loseEvent = "<table border=1 ><tr><td>Name</td><td>Description</td><td>URL</td></tr><tr><td>" + R.getString(R.findColumn("Name")) + "</td><td>" + R.getString(R.findColumn("Description")) + "</td><td><a href='" + R.getString(R.findColumn("DetailURL")) + "'>See on OpenPages</a></td></tr></table>"; 
       } 
     Email("[email protected]","subject","content"); 
     connection.close(); 
    } 

    protected static Properties loadPropertiesFromXML(String filePath) 
    { 
     try 
     { 
      File file = new File(filePath); 
      FileInputStream fileInput = new FileInputStream(file); 
      properties = new Properties(); 
      properties.loadFromXML(fileInput); 
      fileInput.close(); 
      return properties; 
     }catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected static String _getProperty(String key) 
    { 
     Enumeration enuKeys = properties.keys();   

     while (enuKeys.hasMoreElements()) { 
      String _key = (String) enuKeys.nextElement(); 
      if(_key.equals(key)) 
      { 
       // System.out.println(" _getProperty() > " + properties.getProperty(key)); 
       return properties.getProperty(key); 
      }  
     } 

     return null;   
    } 

    protected static void _openDBConnection() 
      throws ClassNotFoundException, SQLException, IOException 
    { 
     //working Drivers 
     Class.forName ("COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver" ); 
     //Class. forName ("com.ibm.db2.jcc.DB2Driver" ); 

     String db_host  = _getProperty("db_host"); 
     String db_port  = _getProperty("db_port"); 
     String db_name  = _getProperty("db_name"); 
     String db_user  = _getProperty("db_username"); 
     String db_password = _getProperty("db_password"); 
     connection = DriverManager.getConnection("jdbc:db2://"+db_host+":"+db_port+"/"+db_name,db_user,db_password); 
     //System.out.println("> DataBase connection obtained "); 


    } 

    protected static String getQuery(String queryTitle) 
    { 
     return _getProperty(queryTitle);   
    } 

    //SELECT OPAGES.RT_LossEvent.LOSSEVENT_ID as LossEventId,OPAGES.RT_LossEvent.NAME00 as Name,OPAGES.RT_LossEvent.DESCRIPTION as Description,OPAGES.ACTORINFO.EMAIL as EmailAddress, OPAGES.RT_LossEvent.DETAIL_PAGE_URL as DetailURL FROM OPAGES.RT_LossEvent JOIN OPAGES.ACTORINFO ON (OPAGES.ACTORINFO.NAME = OPAGES.RT_LossEvent.OWNER AND CONCAT(CONCAT(Year(current date),'-'),CONCAT(Month(current date),CONCAT('-',Day(current date)))) = CONCAT(CONCAT(Year(OPAGES.RT_LossEvent.Creation_Date),'-'),CONCAT(Month(OPAGES.RT_LossEvent.Creation_Date),CONCAT('-',Day(OPAGES.RT_LossEvent.Creation_Date))))) 
    //ResultSet R 
    protected static ResultSet _executeQuery(String _query) 
      throws ClassNotFoundException, SQLException, IOException { 
     //System.out.println(_query); 
     Statement s = connection.createStatement(); 
     return s.executeQuery(_query); 
     //while(R.next()) { 

     // message_loseEvent = "<table border=1 ><tr><td>Name</td><td>Description</td><td>URL</td></tr><tr><td>" + R.getString(R.findColumn("Name")) + "</td><td>" + R.getString(R.findColumn("Description")) + "</td><td><a href='" + R.getString(R.findColumn("DetailURL")) + "'>See on OpenPages</a></td></tr></table>"; 
     //}  
    } 

    protected static void setupMailServer() 
    { 
      // Recipient's email ID needs to be mentioned. 
      String to = _getProperty("from_address"); 
      // Sender's email ID needs to be mentioned 
      String from = _getProperty("from_address"); 
      // Assuming you are sending email from localhost 
      String host = _getProperty("email_host"); 

      // Get system properties 
      Properties properties = System.getProperties(); 
      // Setup mail server 
      properties.setProperty(_getProperty("email_server") , host); 

      // Get the default Session object. 
      session = Session.getDefaultInstance(properties); 


    } 

    protected static void Email(String _toAddress,String _subject,String _content) 
    { 
     try 
     { 
      // Create a default MimeMessage object. 
      MimeMessage message = new MimeMessage(session); 

      // Set From: header field of the header. 
      message.setFrom(new InternetAddress(_getProperty("from_address"))); 
      // Set To: header field of the header. 
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(_toAddress)); 
      // Set Subject: header field 
      message.setSubject(_subject); 
      // Send the actual HTML message, as big as you like 
      message.setContent(message_loseEvent,"text/html"); 

      // Send message 
      Transport.send(message); 
      //System.out.println("> Sent message successfully"); 
     }catch (MessagingException mex) 
     { 
      mex.printStackTrace(); 
     } 
    } 

} 
+0

你能發表一些代碼嗎? – A23149577 2014-11-25 08:54:22

+0

當您嘗試使用命令行編譯代碼時,是否包含這些jar文件? – A23149577 2014-11-25 08:56:20

+0

我不帶命令行編譯,不偏食的編制,我就跑\調用通過命令行 – Moon 2014-11-25 08:57:54

回答

2

確保所有你需要(如第三方庫)都在你的classpath(當你運行程序)的JAR文件。調用JVM時請查看-cp選項。例如:java -cp ...應該可以解決您的問題。

0

使用帶類路徑的javac/java命令來編譯帶有jar文件的java文件。

javac -cp <path to jar file> package/javafile.java 

或者

簡單,提取您javax.mail.Address罐子文件,並把所有的文件夾爲classes文件夾如javax文件夾。正常編譯你的java文件

javac package/javafile.java 
0

你需要在類路徑中添加lib文件夾,以便Java知道jar的位置。嘗試將以下部分添加到您的java命令中:

java -cp "/path/to/lib/*:/other/paths/" ... 

冒號:above用於指定多個路徑。如果它給出你的主類沒有找到的錯誤,那麼在上面的類路徑中也添加路徑到你的主類。

java -cp "/path/to/lib/*:/other/paths/:/path/to/main/class" ... 

假設:您不需要編譯代碼,只需要運行它。