compilation error - SendMessage.java  does not exist

mac_g
mac_g used Ask the Experts™
on
i am very new to java. I would like to test the code as standalone as well as servlet, which is attached. while compilation i am getting the following error message. i am not setting any class path  here before compilation.

please advice for the resolution
---------------------------------------

SendMessage.java:7: package javax.mail does not exist
import javax.mail.*;
^
SendMessage.java:8: package javax.mail.internet does not exist
import javax.mail.internet.*;
^
SendMessage.java:9: package javax.activation does not exist
import javax.activation.*;


F1.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you must include the java mail jar in the class path or added it to the build configuration in eclipse.
It is not included by default
mac_gAdmin - Middleware Servers

Author

Commented:
Thanks for your reply.

90% errors have eliminated ..  now i am getting from the attached line of code.

-------------

SendMessage.java:21: cannot find symbol
symbol  : class SendMessageException
location: class SendMessage
        String Attachment) throws SendMessageException
                                  ^
SendMessage.java:132: cannot find symbol
symbol  : class SendMessageException
location: class SendMessage
                        throw new SendMessageException("Email Address error "
operation + " value > " + aex.getMessage());
                                  ^
SendMessage.java:137: cannot find symbol
symbol  : class SendMessageException
location: class SendMessage
                        throw new SendMessageException(nested.getMessage());
                                  ^
I am not sure what SendMessageException is
try using MessagingException instead
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

have you created class named 'SendMessageException' and put that in class path?
mac_gAdmin - Middleware Servers

Author

Commented:
yes, there is jar file named -javamailintegration.jar, there  class file name with -SendMessageException defined in it. this jar file already in classpath


D:\javamailintegration\oracle\forms\demos\javamail
Top Expert 2016
Commented:
Please place the following as the first line of main and tell us what it prints
System.out.println(System.getProperty("java.class.path"));

Open in new window

is this a class you have written or obtained separately from the javamail package.  presuming so i think you need to include it in your imports.
Generally I prefer not to use *s in my import so that it is clearer which classes i have imported and which are missing
mac_gAdmin - Middleware Servers

Author

Commented:
the jar file I have dowloaded from oracle site for mail feature.

D:\>echo %CLASSPATh%
.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\jdevstudio10133\jdbc\lib;C:\j
devstudio10133\jdbc\lib\ocrs12.jar;C:\jdevstudio10133\jdbc\lib\ojdbc14.jar;C:\jd
evstudio10133\jdbc\lib\ojdbc14dms.jar;C:\jdevstudio10133\jdbc\lib\orai18n.jar;C:
\jdevstudio10133\jlib\activation.jar;C:\jdevstudio10133\jlib\javamailintegration
.jar;C:\jdevstudio10133\jlib\mail.jar
i am still unclear on what class you are using
i found this:
http://deegree.sourceforge.net/deegree1.x.x_javadoc/org/deegree_impl/tools/mail/SendMailException.html
is this what you are using?
if so then you need to import this class in you list of imports
if not please can you let us know what it is.
Where did you see it on the oracle site, could you provide a link?
Top Expert 2016

Commented:
OK. Can you now post the result of the following please?
jar tf C:\jdevstudio10133\jlib\javamailintegration.jar

Open in new window

mac_gAdmin - Middleware Servers

Author

Commented:

D:\>jar tf C:\jdevstudio10133\jlib\javamailintegration.jar
META-INF/
META-INF/MANIFEST.MF
oracle/forms/
oracle/forms/demos/
oracle/forms/demos/javamail/
oracle/forms/demos/javamail/SendMessage.class
oracle/forms/demos/javamail/SendMessageException.class
mac_gAdmin - Middleware Servers

Author

Commented:
please find the link download the JAR's what I said.


http://java.sun.com/products/javamail/javamail-1_4_2.html
this package does not include SendMessageException class
so either you have to create it yourself or it is in a different jar which you need to inlcude in you class path and a reference to the class in your imports.
Top Expert 2016

Commented:
Make sure you have the following at the top of your code
import oracle.forms.demos.javamail.SendMessageException;

Open in new window

thanks cehj, i have been trying to figure that out.
mac_gAdmin - Middleware Servers

Author

Commented:
after importing i am getting the different error message ..

D:\>javac SendMessage.java
SendMessage.java:1: 'class' or 'interface' expected
---
^
SendMessage.java:3: 'class' or 'interface' expected
import java.net.InetAddress;
^
SendMessage.java:4: 'class' or 'interface' expected

--------------------   please refer to complete code --


import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import oracle.forms.demos.javamail.SendMessageException;



public class SendMessage
{

      public static void send(String SMTPhost,
                        String From,
                        String To,
                        String Subject,
                        String Body,
                        String Attachment) throws SendMessageException
      {
            

            int StartPos = 0;
            int EndPos;
            char LastAttachChar;
            String Filename;
            String operation = null;

            //Check if attachment is null
            if (Attachment == null||Attachment.equals(""))
            {
                  EndPos=0;
            }
            else
            {
                  EndPos=Attachment.length();
                  LastAttachChar = Attachment.charAt(EndPos-1);

                  //If Attachment is terminated by comma, delete the last character from attachment
                  if (LastAttachChar==',')
                  {
                        Attachment = Attachment.substring(StartPos,EndPos-1);
                        EndPos=Attachment.length();
                  }
            }


            //Get properties and default session for the SMTP server
            Properties props = System.getProperties();
            props.put("mail.smtp.host", SMTPhost);
            // props.put ("mail.smtp.port", "993");
            Session session = Session.getDefaultInstance(props, null);

            //Now try and create the mail message
            try
            {
                  //Create a message
                  Message msg = new MimeMessage(session);

                  //Add sender to message
                  operation = "FROM";
                  msg.setFrom(new InternetAddress(From));

                  //Add reciepient list to message
                  operation = "TO";
                  InternetAddress[] address = InternetAddress.parse(To,false);
                  msg.setRecipients(Message.RecipientType.TO, address);

                  //Add subject to message
                  msg.setSubject(Subject);

                  //Add date to message
                  msg.setSentDate(new Date());

                  //Add a new part to the message
                  // -------------------------------

                  Multipart mp = new MimeMultipart();
                  {
                        MimeBodyPart mbp = new MimeBodyPart();
                        //Add the body to the new part
                        mbp.setText(Body);
                        mp.addBodyPart(mbp);
                  }
                  //Parse the attachment list and add files to the new part
                  // ------------------------------------------------------

                  if (EndPos != 0)
                  {
                        //Get the first and last positions of the file separator
                        int FilesepPos = Attachment.indexOf(",");
                        int FilesepLastPos = Attachment.lastIndexOf(",");

                        //The attachment contains only one file
                        if (FilesepPos==-1)
                        {
                              FilesepPos=EndPos;
                              EndPos=0;
                        }

                        while (true)
                        {
                              MimeBodyPart mbp = new MimeBodyPart();
                              Filename = Attachment.substring(StartPos,FilesepPos);
                              FileDataSource fds = new FileDataSource(Filename);
                              mbp.setDataHandler(new DataHandler(fds));
                              mbp.setFileName(fds.getName());
                              mp.addBodyPart(mbp);
                              if (EndPos==0)
                              {
                                    break;
                              }
                              Attachment = Attachment.substring(FilesepPos+1,EndPos);
                              EndPos = Attachment.length();
                              if (FilesepPos==FilesepLastPos)
                              {
                                    FilesepPos=EndPos;
                                    EndPos=0;
                              }
                              else
                              {
                                    FilesepPos = Attachment.indexOf(",");
                                    FilesepLastPos = Attachment.lastIndexOf(",");
                              }
                        }
                  }

            //Add the new part to the message
            // ---------------------------------

            msg.setContent(mp);

            //Send the message
            Transport.send(msg);
            }
                  catch (AddressException aex)
            {
                  throw new SendMessageException("Email Address error " + operation + " value > " + aex.getMessage());
            }
                  catch (MessagingException mex)
            {
            //Ignore the top level exception as this just tells us the main was not sent
            //The next exception will give more detail
            Exception nested = mex.getNextException();
            throw new SendMessageException(nested.getMessage());
            }
            catch (Exception ex)
            {
                  throw new SendMessageException(ex.toString());
            }
      }







   public static void main(String[] args)
   {
      try
      {




            SendMessage sm = new SendMessage();
            // sm.send("<your smtp server>","<from>","<to>","Test from Main","blaa blaa","");

            sm.send("xxxxx.oracle.com",
                  "Hoan_sender@oracle.com", // FROM
                  "Hoan.xxxxxx@oracle.com", // TO
                  "Test Mail ",
                  "Bonjour les hirondelles ",
            "");


      }
      catch(SendMessageException sme)
      {
            System.out.println(sme.getMessage());
      }
  }


}


Top Expert 2016

Commented:
Please post code in the code snippet window. Make sure the code you post is the code you're trying to compile, since i have no problem with it (below)
import oracle.forms.demos.javamail.SendMessageException;

import java.io.*;

import java.net.InetAddress;

import java.util.Date;
import java.util.Properties;

import javax.activation.*;

import javax.mail.*;
import javax.mail.internet.*;


public class SendMessage {
    public static void send(String SMTPhost, String From, String To,
        String Subject, String Body, String Attachment)
        throws SendMessageException {
        int StartPos = 0;
        int EndPos;
        char LastAttachChar;
        String Filename;
        String operation = null;

        //Check if attachment is null
        if ((Attachment == null) || Attachment.equals("")) {
            EndPos = 0;
        } else {
            EndPos = Attachment.length();
            LastAttachChar = Attachment.charAt(EndPos - 1);

            //If Attachment is terminated by comma, delete the last character from attachment
            if (LastAttachChar == ',') {
                Attachment = Attachment.substring(StartPos, EndPos - 1);
                EndPos = Attachment.length();
            }
        }

        //Get properties and default session for the SMTP server
        Properties props = System.getProperties();
        props.put("mail.smtp.host", SMTPhost);

        // props.put ("mail.smtp.port", "993");
        Session session = Session.getDefaultInstance(props, null);

        //Now try and create the mail message
        try {
            //Create a message
            Message msg = new MimeMessage(session);

            //Add sender to message
            operation = "FROM";
            msg.setFrom(new InternetAddress(From));

            //Add reciepient list to message
            operation = "TO";

            InternetAddress[] address = InternetAddress.parse(To, false);
            msg.setRecipients(Message.RecipientType.TO, address);

            //Add subject to message
            msg.setSubject(Subject);

            //Add date to message
            msg.setSentDate(new Date());

            //Add a new part to the message
            // -------------------------------
            Multipart mp = new MimeMultipart();

            {
                MimeBodyPart mbp = new MimeBodyPart();
                //Add the body to the new part
                mbp.setText(Body);
                mp.addBodyPart(mbp);
            }

            //Parse the attachment list and add files to the new part
            // ------------------------------------------------------
            if (EndPos != 0) {
                //Get the first and last positions of the file separator
                int FilesepPos = Attachment.indexOf(",");
                int FilesepLastPos = Attachment.lastIndexOf(",");

                //The attachment contains only one file
                if (FilesepPos == -1) {
                    FilesepPos = EndPos;
                    EndPos = 0;
                }

                while (true) {
                    MimeBodyPart mbp = new MimeBodyPart();
                    Filename = Attachment.substring(StartPos, FilesepPos);

                    FileDataSource fds = new FileDataSource(Filename);
                    mbp.setDataHandler(new DataHandler(fds));
                    mbp.setFileName(fds.getName());
                    mp.addBodyPart(mbp);

                    if (EndPos == 0) {
                        break;
                    }

                    Attachment = Attachment.substring(FilesepPos + 1, EndPos);
                    EndPos = Attachment.length();

                    if (FilesepPos == FilesepLastPos) {
                        FilesepPos = EndPos;
                        EndPos = 0;
                    } else {
                        FilesepPos = Attachment.indexOf(",");
                        FilesepLastPos = Attachment.lastIndexOf(",");
                    }
                }
            }

            //Add the new part to the message
            // ---------------------------------
            msg.setContent(mp);

            //Send the message
            Transport.send(msg);
        } catch (AddressException aex) {
            throw new SendMessageException("Email Address error " + operation +
                " value > " + aex.getMessage());
        } catch (MessagingException mex) {
            //Ignore the top level exception as this just tells us the main was not sent
            //The next exception will give more detail
            Exception nested = mex.getNextException();
            throw new SendMessageException(nested.getMessage());
        } catch (Exception ex) {
            throw new SendMessageException(ex.toString());
        }
    }

    public static void main(String[] args) {
        try {
            SendMessage sm = new SendMessage();
            // sm.send("<your smtp server>","<from>","<to>","Test from Main","blaa blaa","");
            sm.send("xxxxx.oracle.com", "Hoan_sender@oracle.com", // FROM
                "Hoan.xxxxxx@oracle.com", // TO
                "Test Mail ", "Bonjour les hirondelles ", "");
        } catch (SendMessageException sme) {
            System.out.println(sme.getMessage());
        }
    }
}

Open in new window

mac_gAdmin - Middleware Servers

Author

Commented:
great ... you made it.

Thanks again lot  ..lot ...


can you please also advice how this code be used to test from web??
Top Expert 2016

Commented:
Not sure what you mean by 'from web'?
mac_gAdmin - Middleware Servers

Author

Commented:
at run time i am getting this .. any idea

D:\>java SendMessage
Exception in thread "main" java.lang.UnsupportedClassVersionError: SendMessage
Unsupported major.minor version 49.0)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
Top Expert 2016

Commented:
You're running the code on a lower version of vm than the one you compiled it with. Try compiling with

java -source 1.4 -target 1.4 ................................................................
Top Expert 2016

Commented:
Oops - that should of course have been

javac -source 1.4 -target 1.4 ................................................................
mac_gAdmin - Middleware Servers

Author

Commented:
when I tried the same code in the test server which is having the version  said above .. i getting error message as ...

----------
C:\test>jar tf C:\Oracleas10g\middletier\forms\java\javamailintegration.jar
META-INF/
META-INF/MANIFEST.MF
oracle/forms/
oracle/forms/demos/
oracle/forms/demos/javamail/
oracle/forms/demos/javamail/SendMessage.class
oracle/forms/demos/javamail/SendMessageException.class

C:\test>javac SendMessage.java
SendMessage.java:1: package oracle.forms.demos.javamail does not exist
import oracle.forms.demos.javamail.SendMessageException;
                                   ^
SendMessage.java:19: cannot resolve symbol
symbol  : class SendMessageException
location: class SendMessage
        throws SendMessageException {
               ^
SendMessage.java:125: cannot resolve symbol
symbol  : class SendMessageException
location: class SendMessage
            throw new SendMessageException("Email Address error " + operation +
                      ^
SendMessage.java:131: cannot resolve symbol
symbol  : class SendMessageException
location: class SendMessage
            throw new SendMessageException(nested.getMessage());
                      ^
SendMessage.java:133: cannot resolve symbol
symbol  : class SendMessageException
location: class SendMessage
            throw new SendMessageException(ex.toString());
                      ^
SendMessage.java:144: cannot resolve symbol
symbol  : class SendMessageException
location: class SendMessage
        } catch (SendMessageException sme) {
                 ^
Top Expert 2016

Commented:
See http:#33133186 and this time please do it and post it
mac_gAdmin - Middleware Servers

Author

Commented:
please find the output as below


C:\test>javac javaprog.java

C:\test>java -classpath . javaprog
.
 hi

----- the code i am using is ---

class javaprog
{  
        public static void main(String args[])
        {
           System.out.println(System.getProperty("java.class.path"));
           System.out.println(" hi ");

        }
}

mac_gAdmin - Middleware Servers

Author

Commented:
and our  original programe giving as

C:\test>javac SendMessage.java

C:\test>java SendMessage
Exception in thread "main" java.lang.NoClassDefFoundError: SendMessage

C:\test>java -classpath . SendMessage
Exception in thread "main" java.lang.NoClassDefFoundError: oracle/forms/demos/ja
vamail/SendMessageException
Top Expert 2016

Commented:
OK. Please just compile and run the following
public class CP {
    public static void main(String[] args) {
	System.out.println(System.getProperty("java.class.path"));
    }
}

Open in new window

this error:
C:\test>javac SendMessage.java
SendMessage.java:1: package oracle.forms.demos.javamail does not exist
import oracle.forms.demos.javamail.SendMessageException;

looks like it comes from the fact that you dont have the correct jar on the second machine that you tested it on.
or you are using a different classpath when you run it on the second machine
Mick BarryJava Developer
Top Expert 2010
Commented:
your classes need to be in package, add the following to top of class

package oracle.forms.demos.javamail;

> import oracle.forms.demos.javamail.SendMessageException;

and you don't need that
mac_gAdmin - Middleware Servers

Author

Commented:
dear CEHJ,

out of the program you suggested is ...

C:\test>javac -classpath . cp.java

C:\test>java -classpath . cp
.

=========
Mick BarryJava Developer
Top Expert 2010

Commented:
your classpath is fine, you're just missing the package statement in your class
mac_gAdmin - Middleware Servers

Author

Commented:
it is already there in JAR file
below is the output ...


D:\>jar tf C:\jdevstudio10133\jlib\javamailintegration.jar
META-INF/
META-INF/MANIFEST.MF
oracle/forms/
oracle/forms/demos/
oracle/forms/demos/javamail/
oracle/forms/demos/javamail/SendMessage.class
oracle/forms/demos/javamail/SendMessageException.class
Mick BarryJava Developer
Top Expert 2010

Commented:
the directory structure is correct, but you also need a package statement in your source code
Mick BarryJava Developer
Top Expert 2010

Commented:
mac_gAdmin - Middleware Servers

Author

Commented:
can you please advice me what the changes are required in my case?

i am newbie to this java..
Mick BarryJava Developer
Top Expert 2010

Commented:
the first line of both your .java files should be:


package oracle.forms.demos.javamail;

you'll then need to recompile
mac_gAdmin - Middleware Servers

Author

Commented:
there was not issue while compile, but while executing the code  here is output.

------------
C:\test>java  SendMessage
Exception in thread "main" java.lang.NoClassDefFoundError: SendMessage

C:\test>java -classpath . SendMessage
Exception in thread "main" java.lang.NoClassDefFoundError: SendMessage (wrong na
me: oracle/forms/demos/javamail/SendMessage)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
Mick BarryJava Developer
Top Expert 2010

Commented:
> java -classpath . SendMessage

to run it use the full class name:

java -classpath . oracle.forms.demos.javamail.SendMessage

and make sure your class file is in the correct directory (c:\test\oracle\forms\demos\javamail\SendMessage.class)
Mick BarryJava Developer
Top Expert 2010

Commented:
and make sure you have deleted any old class files
Top Expert 2016
Commented:
Assuming you're going to place those jars in the same location again, try the following:
java -Djava.ext.dirs=C:\jdevstudio10133\jlib oracle.forms.demos.javamail.SendMessage

Open in new window

mac_gAdmin - Middleware Servers

Author

Commented:
there is progress by your advice above ..


---

C:\test>java -Djava.ext.dirs=C:\Oracleas10g\middletier\forms\java oracle.forms.d
emos.javamail.SendMessage
SendMessage: Unknown SMTP host: <your smtp server>;
  nested exception is:
        java.net.UnknownHostException: <your smtp server>
-----


But i have defined a valid SMTP server IP in below part of JAVA code , the same IP I tested from my PL/SQL code.. working fine for sending mail.

any clue

=========

public static void main(String[] args) {
        try {
            SendMessage sm = new SendMessage();
            // sm.send("<your smtp server>","<from>","<to>","Test from Main","blaa blaa","");
            sm.send("10.1.33.105", "sameer100@dara.gov.sa", // FROM
                "sameer100@dara.gov.sa", // TO
                "Test Mail ", "Bonjour les hirondelles ", "");
        } catch (SendMessageException sme) {
            System.out.println(sme.getMessage());
        }
    }
Mick BarryJava Developer
Top Expert 2010

Commented:
recompile your code, you appear to be running an old version
Mick BarryJava Developer
Top Expert 2010

Commented:
> C:\test>java -Djava.ext.dirs=C:\Oracleas10g\middletier\forms\java oracle.forms.d

you don't actually need all that btw

mac_gAdmin - Middleware Servers

Author

Commented:
after recompiling ... effect is same

version is


C:\test>java -version
java version "1.4.2_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)
Top Expert 2016

Commented:
>>SendMessage: Unknown SMTP host: <your smtp server>;

That indicates that the version before you commented out this line

>>// sm.send("<your smtp server>","<from>","<to>","Test from Main","blaa blaa","");

is running
Top Expert 2016

Commented:
>>after recompiling ... effect is same

You need to rejar as well if you want to run from the jar file
Top Expert 2016

Commented:
The command would be something like
jar uvf C:\jdevstudio10133\jlib\javamailintegration.jar -C <put parent directory of oracle\forms\demos\javamail here without angle brackets> oracle\forms\demos\javamail\SendMessage.class

Open in new window

mac_gAdmin - Middleware Servers

Author

Commented:
seems some syntax error.

--
D:\>jar uvf D:\test\javamailintegration.jar -C D:\test\javamailintegration
Usage: jar {ctxu}[vfm0Mi] [jar-file] [manifest-file] [-C dir] files ...
Options:
    -c  create new archive
    -t  list table of contents for archive
    -x  extract named (or all) files from archive
    -u  update existing archive
    -v  generate verbose output on standard output
Top Expert 2016

Commented:
You missed the final argument (the class file)
mac_gAdmin - Middleware Servers

Author

Commented:
the complete path of the jar file located on the server is -

C:\Oracleas10g\middletier\forms\java\javamailintegration.jar,
Top Expert 2016

Commented:
Isn't that one of Oracle's own jars btw, containing SendMessage?
mac_gAdmin - Middleware Servers

Author

Commented:
it is from Oracle -
Top Expert 2016

Commented:
See what happens when you do this
java -jar C:\Oracleas10g\middletier\forms\java\javamailintegration.jar

Open in new window

mac_gAdmin - Middleware Servers

Author

Commented:

C:\test>java -jar C:\Oracleas10g\middletier\forms\java\javamailintegration.jar
Failed to load Main-Class manifest attribute from
C:\Oracleas10g\middletier\forms\java\javamailintegration.jar
Top Expert 2016

Commented:
OK - was just wondering if it was executable - it isn't. Therefore you need to do what i mentioned before, with Oracle's own jar http:#33135601
mac_gAdmin - Middleware Servers

Author

Commented:
created the new jar file

C:\test>jar cvf test.jar SendMessage.class
added manifest
adding: SendMessage.class(in = 3586) (out= 1927)(deflated 46%)

----

placed the same jar file in class path

and then result is



C:\test>java -classpath .  SendMessage
Exception in thread "main" java.lang.NoClassDefFoundError: SendMessage (wrong na
me: oracle/forms/demos/javamail/SendMessage)
        at java.lang.ClassLoader.defineClass0(Native Method)

Mick BarryJava Developer
Top Expert 2010

Commented:
I already explained how to fix that. you seem to be going around in circles.
My earlier comments show all you need to run your application

you manifest should contain the correct name of the class to run (as explained in my earlier comment)
and the location of the other required jars

eg. if the jars are all in the same directory

Main-Class: oracle.forms.demos.javamail.SendMessage
Class-Path: mail.jar
Mick BarryJava Developer
Top Expert 2010

Commented:
> C:\test>java -classpath .  SendMessage

see my earlier comment :)


java -classpath . oracle.forms.demos.javamail.SendMessage

Top Expert 2016

Commented:
Did you rejar it as i mentioned at http:#33135699 ?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial