在CentOS 5.3下配置 php 调用 java 的相关步骤以及疑难解析,主要问题如下:
1)使用phpize建立php扩展时,出现:Cannot find config.m4. ;
2)configure失败:configure: error: /bin/sh './configure.gnu' failed for server;
第一步:下载php-java-bridge源码包
php-java-bridge下载相关地址(
推荐):
http://ftp.heanet.ie/disk1/sourceforge/p/project/ph/php-java-bridge/OldFiles/
#
wget http://ftp.heanet.ie/disk1/sourceforge/p/project/ph/php-java-bridge/OldFiles/php-java-bridge_4.0.7.tar.gz
第二步:Centos下安装 php-java-bridge
1、解压 php-java-bridge 源码包
#
tar xzvf php-java-bridge_4.0.7.tar.gz
2、开始安装 php-java-bridge
#
cd php-java-bridge-4.0.7
#
/usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
3、执行 configure 命令
#
./configure --with-java=/usr/local/java,/usr/local/java/jre
4、执行 make 命令
#
make
5、执行 make install 命令
#
make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
第三步:配置 php 扩展 java
在 php.ini 文件的末尾添加如下内容:
#
vi /usr/local/php/etc/php.ini
[Java]
java.class.path="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613"
java.java_home="/usr/local/java"
java.library=/usr/local/java/lib
java.library.path="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613"
extension_dir=/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613
extension=java.so
第四步:重启apache服务
#
/usr/local/apache/bin/apachectl restart
第五步:测试php调用java 是否成功
1) 新建一个 test.php
#
vi test.php
<?php
$system=new Java("java.lang.System");
print 'Java version='.$system->getProperty('java.version');
echo "程序已经断开....";
?>
2) 测试
#
php test.php
Java version=1.6.0_01程序已经断开....
若出现如上的 “Java version=1.6.0_01程序已经断开....”,说明安装成功!
==================================================================================================================================================
【备注】
(1)在安装php-java-bridge_6.2.1.tar.gz时,出现如下问题,导致不能安装:
下载URL:
http://sourceforge.net/projects/php-java-bridge/
[root@zrf php-java-bridge-6.2.1]#
/usr/local/php/bin/phpize
Cannot find config.m4.
Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module
在此,可能是由于php或php-java-bridge的版本问题,于是更换php-java-bridge较低版本,如:php-java-bridge_3.2.1.tar.gz 等。
[root@zrf php-java-bridge-3.2.1]#
/usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable is set correctly and then rerun this script.
以上说明 autoconf 软件包没有安装,在CentOS 系统中可以执行如下命令:
#
sudo yum install autoconf
注意:安装autoconf软件包时,需要依赖 imake 软件包。但直接执行 sudo yum install autoconf 会自动安装完成。
然后,再执行 phpize 命令:
[root@zrf php-java-bridge-3.2.1]#
/usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
(2)执行 configure 命令时
[root@zrf php-java-bridge-3.2.1]#
./configure --with-java=/usr/local/java,/usr/local/java/jre
......
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
configure: configuring in server
configure: running /bin/sh './configure.gnu' --prefix=/usr/local/php '--with-java=/usr/local/java,/usr/local/java/jre' --cache-file=/dev/null --srcdir=.
./configure.gnu: line 5: aclocal: command not found
autoheader: error: AC_CONFIG_HEADERS not found in configure.in
configure.in:15: error: possibly undefined macro: AM_INIT_AUTOMAKE
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
configure.in:16: error: possibly undefined macro: AM_CONFIG_HEADER
configure.in:20: error: possibly undefined macro: AM_PROG_GCJ
configure.in:21: error: possibly undefined macro: AC_PROG_LIBTOOL
configure.in:66: error: possibly undefined macro: AM_CPPFLAGS
configure.in:195: error: possibly undefined macro: AM_CONDITIONAL
./configure.gnu: line 11: libtool: command not found
./configure.gnu: line 14: automake: command not found
configure: error: cannot find install-sh or install.sh in . ./.
Back-end configuration failed.
Please install the recommended autoconf, libtool and automake versions
or disable back-end configuration and use the pre-compiled J2EE back-end:
./configure --disable-backend --with-java=<JAVA_HOME>
configure: error: /bin/sh './configure.gnu' failed for server
若出现以上错误,请根据推荐安装:
[root@zrf php-java-bridge-3.2.1]#
./configure --disable-backend --with-java=/usr/local/java
即可解决。
经过我的多次尝试,建议不使用这种方法(使用参数 --disable-backend),否则相应的扩展不成功。
(3)JavaBridge.jar的启动问题
一般在Linux下安装完php-java-bridge,重启Apache之后,调用 JavaBridge.jar包的程序会自动启动。
查看是否启动,使用如下命令:
#
ps -ef | grep JavaBridge.jar
若没有自动启动,网上还有另外一种启动方式,如下:
#
java -jar Javabridge.jar SERVLET:8080 3 JavaBridge.log
(不可断开!)
-----------------------------------------------------------------------------------------------------------------------------------------------
Author:Zheng Rongfeng
Date:From Nov 10, 2010 to Nov 28th, 2010