1

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");
12
  • 3
    Why don't you just use PHP to send it instead? Commented Mar 30, 2012 at 2:16
  • php.net/manual/en/function.mail.php Commented Mar 30, 2012 at 2:17
  • Yes, I agree with minitech. I'm not sure why you can't send mail using php. But here is a link that will show you how to do execute an external program in java -> linglom.com/2007/06/06/… Commented Mar 30, 2012 at 2:22
  • Perhaps we can assume that the Java program is sufficiently complex that it's worth not rewriting in PHP. And even if it's not, the fact that it's possible to mail in PHP is by the by - this error might happen for a more reasonable usage of Java. Commented Mar 30, 2012 at 2:26
  • 1
    @Paul Do you get the same error if you run it from the command line (not in PHP)? Commented Mar 30, 2012 at 2:26

2 Answers 2

1

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:

(linux)   java -classpath .:javax.jar com.me.MyMailClient
(windows) java -classpath .;javax.jar com.me.MyMailClient

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.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that your webserver (for ex APACHE) is not able to locate the java class files in the class path what you have to do is either change your class path using CLASSPATH environment variable to a location where the Jar/classes is there, or put you classes/jar file to the lib directory of the APACHE (APACHE/lib) and restart the server .

hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.