0

I am trying to run a program on the command line that uses the JavaMail API. This program also reads in a message from a text file. However, I'm getting this error:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
     at java.lang.Class.getDeclaredMethods0(Native Method)
     at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
     at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
     at java.lang.Class.getMethod0(Class.java:3018)
     at java.lang.Class.getMethod(Class.java:1784)
     at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
     ... 7 more

Originally I used java -cp /path/to/mail.jar mailTest < message.txt

I configured the classpath for the jar successfully (as far as I know). Can anyone give me some pointers? :/

blue_skies
  • 1
  • 1
  • 2

2 Answers2

1

Your classpath is definitely wrong. The classpath needs to include both the javax.mail.jar file and the classes for your application. Exactly where is the JavaMail jar file, exactly where are your application classes, and exactly what java command line did you use? If your application classes are in the current directory, you need something like

java -cp /path/to/javax.mail.jar:. mailTest
Bill Shannon
  • 27,854
  • 5
  • 34
  • 37
  • I've placed the jar file into the same directory as my other application classes. The exact java command I used was: java -cp .:/path/to/javax.mail.jar mail.mailTest < mail.message.txt – blue_skies Feb 27 '17 at 14:15
  • Bear in mind that on Windows, the separator after -cp is a semicolon ";", so you should have java -cp .;/path/to/javax.mail.jar mail.mailTest – Adonis Feb 27 '17 at 16:24
  • Sorry I wasn't being clear. I am using mac, so separator is : for me – blue_skies Feb 27 '17 at 16:35
0

Actually if you look at the error message:

Caused by: java.lang.ClassNotFoundException: javax.mail.Address

That's probably because your jar depends on another jar (probably mail-x.y.z.jar) and you did not include it on your classpath when trying to run your jar.

Either include the missing jar(s) on the command line, see this post, or add them to your manifest file, see the doc

Community
  • 1
  • 1
Adonis
  • 3,984
  • 3
  • 31
  • 47
  • I've checked, and there doesn't appear to be another dependency. For now, it seems as if my classpath is wrong, but thank you for your help! I really appreciate it! – blue_skies Feb 27 '17 at 16:09
  • You are on the right path, you are missing this jar from your classpath, as mentioned in my post you have two ways to handle that in a direct fashion: Creating a manifest file, or including the dependency on the command line. – Adonis Feb 27 '17 at 16:23
  • I'm sorry, this may be a stupid question, but I checked my javax.mail.jar and the Address class was there. I tried to search for another jar dependency, but I couldn't find anything, so I thought the jar couldn't be located because my classpath was wrong. How can I find this missing jar then? When I try to look up "javax.mail.Address" my search points me back to the original jar I had on hand. – blue_skies Feb 27 '17 at 16:33
  • You will need to do a research on your filesystem for "mail*jar" or you could download it and place it wherever you wish – Adonis Feb 27 '17 at 16:53
  • Turns out the missing jar was the case! Thank you so much for your help! – blue_skies Feb 27 '17 at 17:11
  • Which jar was missing? Did you not actually have the JavaMail jar file at the path you were specifying? – Bill Shannon Feb 27 '17 at 19:45