0

I am writing a simple class to send email, so there are external JARs required. What I did was I added the External JARS in eclipse project. It is working.

But when I call the class from command line it gives NoClassDefFoundError:javax.mail.Address. Two things here, I had copied all the JARs in Java Lib folder and second is that javax.mail.Address is not directly referenced in code. Any idea what is woring or how to fix it.

The class has a main function which is working fine without the email code.


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("openpages@fvb.com","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();
        }
    }

}
Moon
  • 19,518
  • 56
  • 138
  • 200
  • Could you post some codes? – A23149577 Nov 25 '14 at 08:54
  • Do you include these jar files when you try to compile your code with command line? – A23149577 Nov 25 '14 at 08:56
  • i dont compile with command line, eclipse does the compilation, i just run\call the class through command line – Moon Nov 25 '14 at 08:57
  • this could help : http://stackoverflow.com/questions/6472202/java-error-using-jars-java-lang-noclassdeffounderror-javax-mail-store http://stackoverflow.com/questions/13407983/javac-cannot-find-symbol-error-with-command-line – Vishal_Kotecha Nov 25 '14 at 09:10

3 Answers3

2

Make sure all the JARs that you need (as third-party libraries) are on your classpath (when you run the program). Look at the -cp option when calling the JVM. Something like: java -cp ... should solve your problem.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Use javac/java command with class path to compile the java file with a jar file.

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

Or

Simply, extract your javax.mail.Address jar file and put all folder into classes folder e.g. javax folder. And compile your java file as normally

javac package/javafile.java 
Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24
0

You need to add the lib folder in the classpath so that Java would know the location of the jars. try adding the following part to your java command:

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

The colon : above is used to specify multiple paths. If it gives the error that your main class is not found, then add the path to your main class too in the above classpath.

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

Assumption: You don't need to compile the code, only you want to run it.

rudedude
  • 723
  • 4
  • 18