Link to home
Start Free TrialLog in
Avatar of jjharr
jjharr

asked on

"Could not find the main class" error

My program compiles fine, but when I attempt to launch a java program on Linux using the "java" command, I get a NoClassDefFoundError for a class that is definitely in the classpath and another error that says it could not find the main class. I have ruled out most of the basic problems for this error.

My program is just 40 lines of code and sends a basic email. It links to one jar - the mail.jar available from Oracle/Sun. The command I'm using is :

java MailTest -classpath ./mail.jar

I have unset the $CLASSPATH variable to prevent conflicts. $JAVA_HOME is correctly set and I have tried running the java executable with the full java path with the same result. The directory is readable and writable by the user I'm logged in as. The case in the command matches the case of the MailTest.class file.

The program was compiled with 'javac MailTest.java -classpath ./mail.jar' and successfully generated a MailTest.class file. The NoClassDefFoundError I'm getting is for the javax/mail/Address class which is definitely in the mail.jar file which is explicitly listed in my classpath.

I'm completely stumped. Suggestions?

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

should be
java-classpath ./mail.jar MailTest

Open in new window

Oops - missing space
java -classpath ./mail.jar MailTest

Open in new window

Avatar of jjharr
jjharr

ASKER

I tried that. I get the same error except my NoClassDefFoundError references MailTest instead of the jar class.
Does sending mails not require activiation.jar as well? You might be chasing a red herring
Please post the output of the following
ls -l *.jar

Open in new window

Avatar of jjharr

ASKER


I don't think so. The javamail downloadable has a sample (msgsend) example that doesn't require it and my code follows the same process. I think you mostly need it for MimeType if you're going to send a multipart message. It also seems like that would give me a compile or run time error relating to that jar. But I added it anyway with no change in the error.

I might try compiling their test programs and see if I see similar issues.
The output? http:#35281760
Avatar of jjharr

ASKER



-rw-rw-r-- 1 josh josh  56290 Mar 31 10:09 activation.jar
-rw-rw-r-- 1 josh josh 494975 Mar 31 08:51 mail.jar
Avatar of jjharr

ASKER


For completeness, the entire 35-line source (personal info X'ed out) :

 
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;
 
public class MailTest {
 
	public static void main(String[] args) {
 
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "localhost");
		props.setProperty("mail.user", "josh");
		props.setProperty("mail.from", "XXXXX@XXXXX.com");		
 
		Session session = Session.getInstance(props);
 
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("XXXX@XXXX.com));
			message.setRecipients(Message.RecipientType.TO,	InternetAddress.parse("XXXXXX@gmail.com"));
			message.setSubject("Test Mail");
			message.setText("Test Body");			
			
			Transport mailTransport = session.getTransport();
			mailTransport.connect();
			Transport.send(message);
 
			System.out.println("Done");
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

Open in new window

Well if that's the JavaMail mail.jar , there's no class in it called 'Mail' and there's no other class in that file, so there's no class to run
Avatar of jjharr

ASKER


You kind of lost me. Are you talking about the .setProperty values? If so, I'm just using the same values as in the JavaMail example that uses exactly the same jars.
>>and there's no other class in that file

Sorry - theabove should have said

and there's no other class file in that directory. Where's the class you want to run?
Avatar of jjharr

ASKER


I'm still clueless :( I have very little experience with standalone java. I thought I was just trying to execute main() in the MailTest class?

I have to go out for a while, but I very much appreciate your help and I'll respond when i get back.
But where is this MailTest class? Not in the directory. I don't think there's one in the mail.jar of JavaMail
try this:

java -classpath mail.jar;activation.jar;. MailTest
Or this way:

set CLASSPATH=.\;.\mail.jar;.\activation.jar
java MailTest
Avatar of jjharr

ASKER

@objects and for_yan, I've tried the equivalents of those (in most Linux implementations, paths use colons instead of semi-colons to separate path elements). At this point I don't think it's a classpath issue, or at least not an obvious one.

@CEHJ: If there's a missing MailTest class, that would be consistent with the errors I see, but I'm not sure how there would be. The main() function is wrapped in a statement that defines the MailTest class. Presumably there's an implicit constructor. I don't see how it's substantially different than writing the basic "hello world" Java program, except that one external jar is required.

I'm going to try to run the JavaMail examples tomorrow and see what happens. It's kind of hard (for me) to believe something so basic should be so hard to debug.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What was the problem?