替php配置java环境
为php配置java环境
JAVA的功能强大是众所周知的,PHP的简单易用也是勿用置于疑的,但偶尔会遇到PHP解决不了的问题--主要是证书的认证、处理等。于是有人便研究出了让PHP调用JAVA类的解决办法。
网上有很多关于如何用PHP调JAVA的文章,但其中不乏需要使用*.dll的文章。用PHP的人都知道,*.dll的文件只能在 windows下用,这和PHP跨平台的原则是相背的。所以研究不需要任何.dll文件的方案是有必要的,经过一段时间的测试,现将个从的经验总结下来。
需要的文件:(可以从其官网上下载http://php-java-bridge.sourceforge.net/pjb/ )
JavaBridge.jar--用于搭建WEB服务器接口的核心JAVA应用程序包。
Java.inc--供PHP语言调用的方法包,主要是Java类。
具体使用办法:
首先运行JavaBridge.jar启动WEB服务器接口,在JavaBridge.jar所在目录运行命令:java -jar JavaBridge.jar SERVLET_LOCAL:8080。其中java如不在环境变量PATH中请使用绝对路径。(windows平台如果关联了jar文件到javaw可 以直接双击JavaBridge.jar,选择SERVLET_LOCAL:8080然后单击确定;Linux平台可以在命令前使用nohup使用程序能 持续运行;8080是常用端口,可以设置为其它端口)。
接下来就可以编写PHP代码了:
//define("JAVA_DEBUG", true); //调试设置?
define("JAVA_HOSTS", "127.0.0.1:8080"); //设置javabridge监听端口,如果开启javabridge.jar设置的端口不是8080,可通过此语句更改
require_once("java/Java.inc"); //php调用java的接口,路径问题需要注意
$here=realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
java_set_library_path
($here.PATH_SEPARATOR .'.'); //设置java开发包(class或jar文件)路径,多个路径就用PATH_SEPARATOR分隔,保证跨平的支持。
java_set_file_encoding
("GBK");????? //设置JAVA编码。没试过其它的编码,也没深入研究如何能用其它的编码。
//前面是配置环境,下面开始真正的调用:
$system = new
Java("java.lang.System");//初始化JAVA下的类,主要操作就是创建Java类的实例,Java类的第一个参数是JAVA开
发的类的名字包含包路径,路径表示按JAVA里导入包的格式。如果JAVA下的类需要使用构造函数,可以在使用第二个参数。
print "Java version=".$system->getProperty("java.version")." \n";
print "Java vendor=".$system->getProperty("java.vendor")." \n\n";
print
"OS=".$system->getProperty("os.name")." ".?
$system->getProperty("os.version")." on
".$system->getProperty("os.arch")." \n";
?>
要调用自己开发的类,类必须是public类型的。要调用的方法也必须是public类型的。
如:ta.java
public class ta
{
?public static void main(String[] args)
?{
??System.out.println("Hello World!");
?}
?public String ts()
?{
??return "from ts";
?}
}
在PHP中
$ta? = new java("ta");
echo java_values($ta->ts());//输出“from ts”
//建议使用java_values函数处理从Java类的实例返回的值。
------------------------------------------------------
不管3721,先运行一次下面的代码再说(java.php):
$java = new java("java.lang.System");
echo $java->getProperty("os.name");
?>
得到的结果是:
Fatal error: Cannot instantiate non-existent class: java in c:\apache\htdocs\java.php on line 3
这是当然的结果了啊,呵呵。因为php要调用java,从逻辑上看需要三样东西:第一是php自己的"接收器",
第二是java的虚拟机,第三当然是默认的一些java类了。
只有上面的准备工作完成了,我们才能在php中使用java!下面是我配置的一些过程。
找到php.ini文件,我的php.ini文件在c:\apache\php下(其他人的可能在c:\winit下或者其他什么地方)。
我用记事本打开php.ini,搜索java这几个字母.
首先把extension=php_java.dll前面的分号去掉。运行java.php,如果没有出现"Cannot instantiate..."的
错误,则说明第一步成功了。
我运行的结果是得到
Fatal error: Unable to load Java Library c:\jdk\jre\bin\hotspot\jvm.dll, error: 找不到指定的模块。 in c:\apache\htdocs\java.php on line 3
我找到 ;java.library = c:\jdk\jre\bin\hotspot\jvm.dll,把java.library前面的分号去掉,把它修改为
java.library = c:\jdk1.3\jre\bin\hotspot\jvm.dll
得到的结果为:
Fatal error: java.lang.NoClassDefFoundError: net/php/reflect in c:\apache\htdocs\java.php on line 3
最后找到java.class.path,完成修改。得到的结果是:
Windows 2000
哈哈,配置成功了!
我的配置如下:
[Java]
extension=php_java.dll
java.library = c:\jdk1.3\jre\bin\hotspot\jvm.dll
java.class.path = .\java\php_java.jar
java.library.path = .\extensions
java.home = c:\jdk1.3
说明:java.library.path必须是php_java.dll所在的目录的路径。
总结起来看,php要调用java,从逻辑上看需要三样东西:第一是php自己的"接收器",这里叫做php_java.dll.
第二是java的虚拟机,这里叫做jvm.dll。第三当然是默认的一些java类了,这里是php_java.jar.
那么具体到配置上面,这三个东西分别对应的就是extension,java.library 和java.class.path.
如果你还要使用自定义的java类,那怎么办呢?答案是可以这样修改java.class.path,
比如:java.class.path=".\java\php_java.jar;c:\myclass"。注意要有引号并且用分号隔开。其中的myclass是你
放"自定义java类"的目录的路径。当你在php中使用java类的时候,php解吸器就在
php_java.jar和你的myclass目录下面寻找。
最后给出我的另外一种"完备并且充分"的配置:
[Java]
extension=php_java.dll
java.library = c:\jdk1.3\jre\bin\hotspot\jvm.dll
java.class.path =".\java\php_java.jar;c:\myclass"
;java.library.path = .\extensions
;java.home = c:\jdk1.3
如果把java.library.path前面的分号去掉,那么它必须和extension_dir有一样的值,这里都是.\extensions。
因为extension_dir是所有php扩展库所在的目录的路径,而java.library.path是php_java.dll所在的目录的路径。所以
才会有这样的要求。所以从逻辑上看,java.library.path是多余的。而java.home把它注释掉照样可以成功的运行,所以
也是多余的。因为根本的原因是还是那句话"逻辑上需要并且只需要三个"!
这样一来,我的这样的配置就是"完备并且充分"的了。
"完备"是指:有了上面的配置,就完全够了;"充分"是指,上面的配置没有任何多余的成分。
"完备并且充分"就是说我的这种配置不多不少,恰到好处,再也找不到比我这个更好的配置方案了。
――哈哈,夏天要来了,大家都来买我的瓜啊。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
