File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Email: java.lang.ClassFormatError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Email: java.lang.ClassFormatError" Watch "Email: java.lang.ClassFormatError" New topic
Author

Email: java.lang.ClassFormatError

Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Hi all,

I am trying to send a simple email from a java application (not a ee application), I am using NetBeans and have imported the:

"Java EE 6 API Library - javaee-api-6.0.jar" - located in the NetBeans sub-folder
"javaee.jar" - located in the glassfish lib folder

Netbeans found no syntax errors and all the imports where excepted once I had added the above files.

But when i run the program I get the following exception:

run:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Address
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: linxaslicencemanagment.MailClient. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Any ideas as to what can be causing this or how I am able to solve this?
All the google-ing i did on the exception told me that adding the library was not enough
as it didn't actually have those classes/methods and that I had to add the .jar as well.
But the exception hasn't changed.

Here is the code:


Any Ideas as to what I am doing incorrectly?
Thanks


Courage is not the absence of fear but rather the judgment that something is more important then fear. ~ Ambrose Redmond
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Update:
Added the javax.mail.jar to my lib but am still getting the same exception.

Any Ideas?
Thanks
Saikat Roy Mahasay
Greenhorn

Joined: Jan 12, 2012
Posts: 10

Did you try it outside NetBeans ? I tried to copy-paste and run your code in eclipse, and it sort of worked. At least I didn't get the ClassFormatError. I used java 1.6 and javamail-1.4.2.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Thanks Saikat,

That exception does disappear when I use it in Eclipse and not NetBeans? - weird.

I added a few changes for the errors I was getting now its getting to a part of the code and just stopping.
I'v waited 30min and it doesn't move on or return any exceptions or errors.

Decided to try get gmail working correctly first.
Any ideas as to what I am doing incorrectly?

Code is the same as above with edited code below. //16 = line changed



Thanks.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Update:

After about 2hours I got the following exception:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1922)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:295)
at linxaslicencemanagment.MailClient.sendMail(MailClient.java:45)
at linxaslicencemanagment.MailClient.main(MailClient.java:83)



Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Final working code for future reference if anyone may need it:

tapakai tk
Greenhorn

Joined: May 02, 2013
Posts: 1
Hi I tried your final code in Eclips and i still get the following error could anyone help me with it:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/BodyPart
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)


Here is the code i tried:

import java.util.Properties;

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

import javax.activation.FileDataSource;
import javax.activation.DataHandler;



public class sendmail
{
private static final String hostName = "smtp.gmail.com";
private static final int hostPort = 465;
private static final String senderAddress = "myuser@gmail.com";
private static final String password = "mypassword";

public void sendEmail(String company, String licence, String remdate, String expdate, String attachments, String supportcontract, String signedcontract, String licenceorigin) throws Exception
{
//Properties
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", hostName);
props.put("mail.smtps.auth", "true");

//Session
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);

//Text
MimeBodyPart text = new MimeBodyPart();
text.setText("Company: " + company + "\n Licence: " + licence + "\n Expiration Date: " + expdate + "\n Support Contract: " + supportcontract +
"\n Signed Contract: " + signedcontract + "\n Licence Origin: " + licenceorigin);

//Attachment
FileDataSource fds = new FileDataSource("C:\\temp.png");
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(fds));
attachment.setFileName(fds.getName());

//Create Multipart & Add Parts
Multipart content = new MimeMultipart();
content.addBodyPart(text);
content.addBodyPart(attachment);

//Compile Message
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing Email");
message.setContent(content);
message.addRecipient(Message.RecipientType.TO, new InternetAddress("myuser@yahoo.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("myuser@gmail.com"));

//Send Message
Transport transport = mailSession.getTransport();
transport.connect(hostName, hostPort, senderAddress, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}

public static void main(String[] args) throws Exception
{
new sendmail().sendEmail("a","b","c","d","e","f","g","h");
}
}

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Email: java.lang.ClassFormatError