php调用java的完整安装配置说明文档

WBOY
Release: 2016-06-23 13:33:52
Original
864 people have browsed it


1. Windows安装配置过程

1.1. Apache和php的安装配置

Apache和php可以使用解压版、安装版、套件版均可。

以下以解压版为例:

1、解压版无需安装,解压后放到任意目录下即可。

2、配置apache对php的支持,配置过程,自行参见网络。

3、demo测试使用的软件版本为:apache2.2.6、php5.3.5、javabridge5.4.4;软件版本可自行选择,但要保证能相互兼容支持。

1.2. Javabridge的配置

1、确认配置好php 与 apache 环境后,配置javabridge,使php支持java调用。

即将JavaBridge的jar包和对应的java目录放入任意目录下。

2、在php的项目中引入java目录的Java.inc(包含Java.inc与JavaProxy.php),如: require_once ( "../java/Java.inc" );

注意引入的路径问题,即../java/Java.inc部分;参考demo的secUtil.php页面引入方式。

3、将php调用的jar包放入到系统所使用的jre的lib下的ext下,如C:\Java\jdk1.6.0_20\jre\lib\ext

4、启动JavaBridge即可。

Javabridge启动有三种方法:

(1)、直接双击启动jar包。

(2)、可以cmd指令运行start java -jar JavaBridge.jar SERVLET_LOCAL:8080(在ext目录下)

(3)、可以使用批处理运行第二种方法(批处理与jar包同一目录)

1.3. 运行phpDemo

1、验证javabridge是否正常,可通过调用java api测试,如:test.php

 

require_once("java/Java.inc");

header("content-type:text/html; charset=utf-8");

// get instance of Java class java.lang.System in PHP

$system = new Java('java.lang.System');

$s = new Java("java.lang.String", "php-java-bridge config...

");

echo $s;

// demonstrate property access

print 'Java version='.$system->getProperty('java.version').' 
';

print 'Java vendor=' .$system->getProperty('java.vendor').' 
';

print 'OS='.$system->getProperty('os.name').' '.

$system->getProperty('os.version').' on '.

$system->getProperty('os.arch').' 
';

// java.util.Date example

$formatter = new Java('java.text.SimpleDateFormat',

"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");

print $formatter->format(new Java('java.util.Date'));

?>

 

注:注意修改里面的引入路径。

 

2、如果javabridge,启动apache和javabridge;访问phpDemo的index.php即可。

 

 

注意:

1、如果访问报错NOTICE修改php.ini配置文件,error_reporting = E_ALL & ~E_NOTICE

2、请保持JavaBridge与Java.inc的版本的一致。

3、调用java的过程如有错误,会通过javabridge窗口和页面显示出来,如调用的方法名不存在。


2. Linux安装配置过程

2.1. Apache安装

2.1.1. 安装版本

httpd-2.2.29.tar.gz

2.1.2. 安装步骤

tar ?zxvf httpd-2.2.29.tar.gz

./configure   

make

make install

2.1.3. 启动apache

安装成功后启动apache:/usr/local/apache2/bin/apachectl start

 

查看是否启动成功:ps -ef  |grep httpd  执行后,出现如下即启动成功。

 

 

2.1.4. Apache FAQ

注意:

1、./configure 时会出现如下提示(未安装gcc编译器时提示):

configure: error: in `/usr/local/tongSoftware/httpd-2.2.29/srclib/apr':

configure: error: no acceptable C compiler found in $PATHSee `config.log' for more detailsconfigure failed for srclib/apr

 

即系统提示未安装gcc编译器,则安装该编译器:yum -y install gcc 即可。

 

2、make和make install 到出现如下即安装成功:

......

mkdir /usr/local/apache2/man

mkdir /usr/local/apache2/man/man1

mkdir /usr/local/apache2/man/man8

mkdir /usr/local/apache2/manual

make[1]: Leaving directory `/root/aphp/httpd-2.2.29'

[root@device httpd-2.2.29]#

 

找到安装路径地方,启动apache即可。

 

3、如果安装的是高版本或部分其他版本的apache,如httpd-2.4.12.tar.gz,编译时会出现如下错误提示:

........

checking for APR... no

configure: error: APR not found . Please read the documentation. 

 

安装解决方案如下:

解决方案如下: 

 

#./configure --prefix……检查编辑环境时出现:

checking for APR... no

configure: error: APR not found . Please read the documentation. 

 

可以用./configure ?help | grep apr 查看帮助。

--with-included-apr Use bundled copies of APR/APR-Util

--with-apr=PATH prefix for installed APR or the full path to apr-config

--with-apr-util=PATH prefix for installed APU or the full path to

安装APR(Apache Portable Runtime )

[root@localhost ~]# cd /tmp/52lamp/ //源码存放位置

[root@localhost 52lamp]# tar -zxvf apr-1.4.2.tar.gz //unzip -o apr-1.4.2.zip

[root@localhost 52lamp]# cd apr-1.4.2

[root@localhost apr-1.4.2]# ./configure

[root@localhost apr-1.4.2]# make

[root@localhost apr-1.4.2]# make install 

 

再次检查编译环境出现

checking for APR-util... no

configure: error: APR-util not found . Please read the documentation. 

 

[root@localhost httpd-2.2.16]# ./configure ?help | grep apr-util

--with-apr-util=PATH prefix for installed APU or the full path to 

 

[root@localhost 52lamp]# tar -zxvf apr-util-1.3.9.tar.gz

[root@localhost 52lamp]# cd apr-util-1.3.9

[root@localhost apr-util-1.3.9]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@localhost apr-util-1.3.9]# make

[root@localhost apr-util-1.3.9]# make install 

 

./configure仍提示APR-util not found,增加--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util后出现

configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/ 

 

[root@localhost httpd-2.2.16]# ./configure ?help | grep pcre

--with-pcre=PATH Use external PCRE library 

 

[root@localhost 52lamp]# unzip -o pcre-8.10.zip

[root@localhost 52lamp]# cd pcre-8.10

[root@localhost cd pcre-8.10]# ./configure --prefix=/usr/local/pcre

[root@localhost cd pcre-8.10]# make

[root@localhost cd pcre-8.10]# make install 

 

继续安装Apache/httpd,./configure 时加上参数 --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre,这个问题就解决了。

注:httpd-2.4.12.tar.gz的安装此过程未亲测。

 

 

2.2. 安装php

2.2.1. 安装版本

php-5.4.40.tar.gz

2.2.2. 安装步骤

tar ?zxvf php-5.4.40.tar.gz

 ./configure --prefix=/usr/local/php/ --with-apxs2=/usr/local/apache2/bin/apxs (注意此处编译,一定要将apache模块编译进去,及--with-apxs2=/usr/local/apache2/bin/apxs)

make

make install

 

2.2.3. Php FAQ

 

注意:

1、如果没有安装libxml2-python和libxml2-devel包,编译时会出现如下错误:

configure: error: xml2-config not found. Please check your libxml2 installation

J解决方式:安装libxml2-python和libxml2-devel包即可:yum -y install libxml2-python libxml2-devel

 

2、make时出现如下错误

1.Perl is not installed

2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs

3. Apache was not built using --enable-so (the apxs usage page is displayed)

The output of /usr/local/apache/bin/apxs follows:./configure: line 6669: /usr/local/apache/bin/apxs: Permission denied

解决方式:1、先安装perl包;2、apxs权限问题,即添加可执行权限;

 

2.2.4. 配置php

创建配置文件到指定配置文件目录中

[root@localhost php-5.4.40]# cp php.ini-dist /usr/local/php/lib/php.ini

 

2.2.5. 添加Apache对PHP支持

[root@localhost php-5.4.40]# vi /usr/local/php/lib/php.ini  

找到AddType application/x-gzip .gz .tgz,在此下面增加一行

AddType application/x-httpd-php .php .php3

重启apache即可。

 

写index.php文件,测试支持是否正常。

 phpinfo();

 ?>

 

2.3. 安装配置javabridge

2.3.1. 免安装方式配置javabridge

1、将javabridge.jar放置到任何位置,一般放在php文件夹内,如/usr/local/php/etc下

2、将与此javabridge.ja r配套的java文件夹放到文件任何位置,一般放在php文件夹内,如/usr/local/php/etc下,然后注意引用的路径问题。

如:require_once ( "/usr/local/php/etc/java/Java.inc" );

3、将所需jar包放到jre下(即/usr/java/jdk1.6.0_20/jre/lib/ext),然后项目引用

//linux

java_require('/usr/java/jdk1.6.0_20/jre/lib/ext/SVSClient.jar');

java_require('/usr/java/jdk1.6.0_20/jre/lib/ext/BJCA_LOG.jar');

 

4、启动javabridge

java -jar JavaBridge.jar SERVLET_LOCAL:8080   此命令启动不能关闭窗口,不会后台运行

java -jar JavaBridge.jar SERVLET_LOCAL:8080 &  此命令启动能关闭窗口,后台运行(有时候需要先ctrl+c中断再关闭窗口)

 

5、查看是否启动成功 ps -ef |grep 8080,启动成功如图所示:

 

 

6、验证javabridge是否正常,可通过调用java api测试,如:test.php

 

require_once("java/Java.inc");

header("content-type:text/html; charset=utf-8");

// get instance of Java class java.lang.System in PHP

$system = new Java('java.lang.System');

$s = new Java("java.lang.String", "php-java-bridge config...

");

echo $s;

// demonstrate property access

print 'Java version='.$system->getProperty('java.version').' 
';

print 'Java vendor=' .$system->getProperty('java.vendor').' 
';

print 'OS='.$system->getProperty('os.name').' '.

$system->getProperty('os.version').' on '.

$system->getProperty('os.arch').' 
';

// java.util.Date example

$formatter = new Java('java.text.SimpleDateFormat',

"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");

print $formatter->format(new Java('java.util.Date'));

?>

 

注:注意修改里面的引入路径。

 


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!