I created a Java program which sends an email from my gmail account. I programmed it in Eclipse and it works fine. But when I attempt to call it from PHP i get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
at sendVerificationEmail.main(sendVerificationEmail.java:3)
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Here’s the command Eclipse uses:
/usr/lib/jvm/java-6-openjdk/bin/java -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:53094 -Dfile.encoding=UTF-8 -classpath /home/****/dir/to/program/java/bin:/home/****/dir/to/program/java/lib/javamail-1.4.4/mail.jar sendEmail
Here’s the command PHP uses:
exec("java -classpath /home/****/dir/to/program/java/bin:/home/****/dir/to/program/java/lib/javamail-1.4.4/mail.jar sendVerificationEmail $name $email $comments");
You need to add the jar file containing the javax.mail… classes to your command:
For example the following will add the current directory (.) and javax.jar to the classpath:
javax.jar is not what it will be called, that’s just to show you where you’d put it. To find out where Eclipse is getting the javax.mail.* items, look at your Project->Properties->Build Path->Libraries tab. One of the jars listed there is what you need (you can see the contents of jars by unzipping). It might be called mail.jar, j2ee.jar, javamail.jar – there are a few different likely candidates. Since your test app is simple it should have few dependencies so you’ll be able to spot it easily.
You will need to put a copy of this jar file near run script (the above example assumes it is in the same directory as you are running the java command).
Hope that helps.