简体   繁体   English

从PHP调用Java程序

[英]Call Java program from PHP

I created a Java program which sends an email from my gmail account. 我创建了一个Java程序,该程序从我的gmail帐户发送电子邮件。 I programmed it in Eclipse and it works fine. 我在Eclipse中进行了编程,效果很好。 But when I attempt to call it from PHP i get the following error: 但是,当我尝试从PHP调用它时,出现以下错误:

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: 这是Eclipse使用的命令:

/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: 这是PHP使用的命令:

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: 您需要将包含javax.mail ...类的jar文件添加到命令中:

For example the following will add the current directory (.) and javax.jar to the classpath: 例如,以下内容将当前目录(。)和javax.jar添加到类路径:

(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. javax.jar并不是它的名字,它只是向您展示放置它的位置。 To find out where Eclipse is getting the javax.mail.* items, look at your Project->Properties->Build Path->Libraries tab. 要找出Eclipse在哪里获取javax.mail。*项目,请查看您的Project-> Properties-> Build Path-> Libraries选项卡。 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. 它可能被称为mail.jar,j2ee.jar,javamail.jar-有一些不同的候选对象。 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). 您将需要将此jar文件的副本放在运行脚本附近(上面的示例假定它与您运行java命令的目录位于同一目录中)。

Hope that helps. 希望能有所帮助。

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 . 问题在于,您的网络服务器(例如APACHE之前的服务器)无法在类路径中找到Java类文件,您需要做的就是使用CLASSPATH环境变量将类路径更改为Jar /类所在的位置,或将classes / jar文件放在APACHE的lib目录(APACHE / lib)中,然后重新启动服务器。

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM