1

I have tried running the demo msgsend from JavaMail. I have downloaded version 1.4.5 of JavaMail and unpacked it into my JDK folder. I have added mail.jar to CLASSPATH. I have compiled msgsend.java without getting any error back. However, when I then try running java msgsend I get an "Could not find the main class: msgsend" error.

I am a total noob in Java world and have spend hours browsing through the Internet trying to find an answer but to no avail in my case.

Here is what I get in the console. What am I doing wrong?

C:\Program Files\Java\jdk1.6.0_34\javamail-1.4.5\demo>echo %CLASSPATH%
C:\Program Files\Java\jdk1.6.0_34\javamail-1.4.5\mail.jar

C:\Program Files\Java\jdk1.6.0_34\javamail-1.4.5\demo>javac -cp "%classpath%" ms
gsend.java

C:\Program Files\Java\jdk1.6.0_34\javamail-1.4.5\demo>java msgsend
Exception in thread "main" java.lang.NoClassDefFoundError: msgsend
Caused by: java.lang.ClassNotFoundException: msgsend
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: msgsend.  Program will exit.

C:\Program Files\Java\jdk1.6.0_34\javamail-1.4.5\demo>java -cp "%classpath%" msg
send
Exception in thread "main" java.lang.NoClassDefFoundError: msgsend
Caused by: java.lang.ClassNotFoundException: msgsend
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: msgsend.  Program will exit.

C:\Program Files\Java\jdk1.6.0_34\javamail-1.4.5\demo>
David Bejar
  • 512
  • 4
  • 20
  • _... unpacked it into my JDK folder_ Why do you unpack stuff into your JDK folder? That's not necessary or even a good idea. You should create some project folder (e.g. in your user dir) and store all your projects there. This way, you can install a new JDK and remove the old one without losing your stuff. – Tim Büthe Sep 24 '12 at 09:14
  • OK, thanks for the hint. I have now unpacked it to a separate folder without any spaces just to avoid any problems there but still to no avail. Same problem as before... :-) – user1693898 Sep 24 '12 at 10:19

2 Answers2

1

Classes are case sensitive. Be sure you have the compiled MsgSend in your class path, and invoke the compiled class with the proper case.

Try java MsgSend

Edited:

This is how it will work:

Compile as you were doing: javac.exe -cp ..\mail.jar msgsend.java And then to run the compiled class do: java -cp ..\mail.jar;. msgsend In other words: you were missing the current folder in your classpath.

David Bejar
  • 512
  • 4
  • 20
  • I opened msgsend.java and inside I can see `public class msgsend {`, I gather that this means that it should be `java msgsend`. I tried MsgSend and got the same error as before. I am starting pulling my hear with this issue (and since I am quite bald already i do not have much left :-)). Any additional hints are welcome :-) – user1693898 Sep 24 '12 at 10:20
  • I downloaded the demo from Oracle. I succeed to run the class in Eclipse IDE, but for some reason could not run it either in command line. I recommend you to move one step forward and start using an IDE. Makes your life much easier :) – David Bejar Sep 24 '12 at 12:40
  • I think it is a good idea to get it to run in console, and understand this stuff, before using an ide. Has the class a package declaration? Where to download this stuff? – Tim Büthe Sep 24 '12 at 14:14
  • It can be downloaded here: http://www.oracle.com/technetwork/java/javamail/javamail143-243221.html. – user1693898 Sep 24 '12 at 17:02
  • No package declaration. I had to add `package javax.mail` to make it compile in Eclipse as that java class uses classes from `javax.mail` without importing them. Honestly, class name in low case, no package, missing imports, I do not think JavaMail demo examples are a good way to learn java... – David Bejar Sep 25 '12 at 06:47
  • OK, thanks for checking that! I am a total Java noob, can you explain how I add the package? Is it only to add it to msgsend.java somewhere? I am asking because I can see that it already says import javax.mail.* in msgsend.java. – user1693898 Sep 25 '12 at 07:14
  • My mistake, seemly Eclipse removed with `javax.mail.*` imports (a rule I have to clean dirty code). This is how it will work: `javac.exe -cp ..\mail.jar msgsend.java` and then to run the compiled class `java -cp ..\mail.jar;. msgsend` In other words, you were missing the current folder in your classpath. – David Bejar Sep 25 '12 at 07:34
  • Maybe you can mark my edited answer on top as the right answer. :) – David Bejar Sep 26 '12 at 02:31
0

You didn't include the directory where msgsend.class exists, usually the current directory ("."). See the examples in README.txt included with JavaMail.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Do you mean CLASSPATH? If yes, then I think it is included, please see the result of ECHO in the beginning of the listing above. Or do you mean another directory? If so, can you write how I should add it? Should it also be CLASSPATH? – user1693898 Sep 24 '12 at 17:05