search
HomeBackend DevelopmentPHP Tutorialphp调用java的完整安装配置说明文档


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'));

?>

 

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

 


Statement
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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software