Home > Backend Development > PHP Tutorial > Detailed explanation of methods and examples of php calling its own java program

Detailed explanation of methods and examples of php calling its own java program

墨辰丷
Release: 2023-03-29 14:14:02
Original
6081 people have browsed it

This article mainly introduces the method of php calling its own java program. It analyzes in detail the two methods of php calling java program based on third-party jar package and using dll dynamic link library file extension to call java. Friends who need it can For reference,

is as follows:

Needless to say, you need to install jdk at the beginning. I installed java ee 5 jdk

1. Download the php-java-bridge_5.2.2_j2ee.zipExtract it, there is JavaBridge.war Open it directly with winrar, go to WEB-INF/lib/JavaBridge.jar Copy this jar package to the ext/ directory of your php directory.

2. Open the war package, there is a java folder inside, copy it all to your PHP project, such as /demo/java

3. The current version is VMBridge , if you want php to call the java class, you must first start JavaBridge,

call java –jar JavaBridge.jar from the command line or double-click JavaBridge.jar, select listening port 8080 in the pop-up window

In order It will be easier to start in the future. I created a new bat file under ext/ with the following content:

@echo off
start javaw -jar JavaBridge.jar
Copy after login
Copy after login

After saving, double-click to start and there will be a prompt box to select vmbridge port by default. 8080, just click ok

4. Create a new test.php under /demo/ with the following content:

<?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(&#39;java.lang.System&#39;);
$s = new Java("java.lang.String", "php-java-bridge config…<br><br>");
echo $s;
// demonstrate property access
print &#39;Java version=&#39;.$system->getProperty(&#39;java.version&#39;).&#39; <br>&#39;;
print &#39;Java vendor=&#39; .$system->getProperty(&#39;java.vendor&#39;).&#39; <br>&#39;;
print &#39;OS=&#39;.$system->getProperty(&#39;os.name&#39;).&#39; &#39;.
$system->getProperty(&#39;os.version&#39;).&#39; on &#39;.
$system->getProperty(&#39;os.arch&#39;).&#39; <br>&#39;;
// java.util.Date example
$formatter = new Java(&#39;java.text.SimpleDateFormat&#39;,
"EEEE, MMMM dd, yyyy &#39;at&#39; h:mm:ss a zzzz");
print $formatter->format(new Java(&#39;java.util.Date&#39;));
?>
Copy after login

5. Start apache , check http://localhost/demo/test.php

in the browser and you will see the following information:

Copy code The code is as follows:

php-java-bridge config…
Java version=1.6.0_10
Java vendor=Sun Microsystems Inc.
OS=Windows Vista 6.0 on x86
Sunday, November 23 , 2008 at 4:31:49 PM China Standard Huai Time

Custom JAR:

package ttt;
public class phptest{
 /**
 * A sample of a class that can work with PHP
 * NB: The whole class must be public to work,
 * and of course the methods you wish to call
 * directly.
 *
 * Also note that from PHP the main method
 * will not be called
 */
 public String foo;
 /**
 * Takes a string and returns the result
 * or a msg saying your string was empty
 */
 public String test(String str) {
  if(str.equals("")) {
   str = "Your string was empty. ";
  }
  return str;
 }
 /**
 * whatisfoo() simply returns the value of the variable foo.
 */
 public String whatisfoo() {
  return "foo is " + foo;
 }
 /**
 * This is called if phptest is run from the command line with
 * something like
 * java phptest
 * or
 * java phptest hello there
 */
 public static void main(String args[]) {
  phptest p = new phptest();
  if(args.length == 0) {
   String arg = "";
   System.out.println(p.test(arg));
  }else{
   for (int i = 0; i < args.length; i++) {
    String arg = args[i];
    System.out.println(p.test(arg));
   }
  }
 }
}
Copy after login

Generate as JAR, copy to D drive Down.

/demo/index2.php

<?
require_once("java/Java.inc");
java_require("D://1.jar");
$myj = new Java("ttt.phptest");
echo "Test Results are <b>" . $myj->test("Hello World") . "</b>";
$myj->foo = "A String Value";
echo "You have set foo to <b>" . $myj->foo . "</b><br>\n";
echo "My java method reports: <b>" . $myj->whatisfoo() . "</b><br>\n";
?>
Copy after login

View http://localhost/demo/index2.php

in the browser

Method 2: php_java.dll needs to configure php.ini, the new version of php-java-bridge does not have dll files

First determine your PHP and Apache servers and JDK ( or JRE can also be installed)

Go online and download php-java-bridge (find it yourself or http://sourceforge.net/project/showfiles.php?group_id=117793)

Decompress the downloaded php-java-bridge. After decompression, there will be a JavaBridge.war in the folder. Then decompress the JavaBridge.war in the same way (win rar can decompress it)
After decompression, you can download it from WEB - Find java-x86-windows.dll in the cgi folder in the INF folder, and find JavaBridge.jar in the lib folder in the WEB-INF folder

Replace java-x86-windows.dll and JavaBridge. jar to the PHP plug-in folder (in my case it is C:/AppServphp/ext), and change java-x86-windows.dll to php_java.dll

Modify the php.ini file

If php.ini does not originally have the following content, please add it yourself. If it originally has the following content, please modify it to the following [I am using JDK]

extension =php_java.dll

[Java]
;java.java = "C:\jdk1.6.0_13\bin\java"
java.class.path = "D:\php\ext\JavaBridge.jar;c:\myclasses" c:\myclasses可自定义,用来存放自己写的JAVA文件 
java.java_home = "C:\jdk1.6.0_13\jre"
java.library = "d:\jdk1.2.2\jre\bin\server\jvm.dll"
java.library.path = "D:\php\ext"
Copy after login

Restart Apache and check phpinfo

java
java support  Enabled
java bridge  3.0.8
java.java_home  C:\jdk1.6.0_13
java.java  C:\jdk1.6.0_13\bin\java
java.log_file  <stderr>
java.log_level  no value (use backend&#39;s default level)
java.ext_java_compatibility  Off
java command  C:\jdk1.6.0_13\bin\java -Djava.library.path=D:\php\ext -Djava.class.path=D:\php\ext/JavaBridge.jar -Djava.awt.headless=true php.java.bridge.JavaBridge INET_LOCAL:0 2
java status  running
java server  9267
Copy after login

Check whether the penultimate java status item is not running (this is because you have not started JavaBridge.jar). If it becomes running <—- it means JavaBridge.jar has been started and php-java-bridge can be officially used

If it is not started, execute:

Because it is impossible to manually start every time To start JavaBridge.jar

So we write a batch process with the following content

@echo off
start javaw -jar JavaBridge.jar
Copy after login
Copy after login

Save it as phpJavaBridge.bat, also in Create a shortcut to the file in the PHP plug-in folder (here is C:AppServ/php/ext)

Create a shortcut to the file and put the created shortcut into the startup (here is C:/Documents and Settings/All Users /"Start"/Menu/Program Startup)

In this way, phpJavaBridge.bat in the C:AppServphpext folder will be automatically launched every time the computer is turned on.

Simple example

<?
$system=new Java(&#39;java.lang.System&#39;);
echo "java版本".$system->getProperty(&#39;java.version&#39;)."<BR>";
echo "发行厂商".$system->getProperty(&#39;java.vendor&#39;)."<BR>";
echo "作业系统版本".$system->getProperty(&#39;os.name&#39;)."<BR>";
echo "java版本".$system->getProperty(&#39;os.version&#39;)."<BR>";
echo "java版本".$system->getProperty(&#39;os.arch&#39;)."<BR>";
?>
Copy after login

Or find test.php in php-java-bridge, http://localhost/test.php to check the effect

<?php
$system=new Java("java.lang.System");
print "Java version=".$system->getProperty("java.version")." <br>";
?>
Copy after login

[java]
extension=PHP_java.dll
java.library.path=c:webPHP4extensions
java.class.path="c:webPHP4extensionsjdk1.2.2PHP_java.jar;c:myclasses"
Copy after login

Add extension=PHP_java.dll to PHP.INI, and set java.class.path in [java] , let it point to PHP_java.jar. If you use a new JAVA class, you should also save it in this path. In this example, we use the c:myclasses directory.

Test the environment and create the following PHP file:

<?php
$system = new Java("java.lang.System");
print "Java version=".$system->getProperty("java.version")." <br>n";
print "Java vendor=".$system->getProperty("java.vendor")." <p>nn";
print "OS=".$system->getProperty("os.name")." ".
$system->getProperty("os.version")." on ".
$system->getProperty("os.arch")." <br>n";
$formatter = new Java("java.text.SimpleDateFormat","EEEE,
MMMM dd, yyyy &#39;at&#39; h:mm:ss a zzzz");
print $formatter->format(new Java("java.util.Date"))."n";
?>
Copy after login

If you installed it correctly, you will see the following information:

Copy code The code is as follows:

Java version=1.2.2
Java vendor=Sun Microsystems Inc.
OS=Windows 95 4.10 on x86
Wednesday, October 18, 2000 at 10:22:45 AM China Standard Time

It is important to understand how to call JAVA. The next step is to create our own JAVA file and let PHP call it. java.class.path is important

Create and use your own JAVA classes[Note the case]

创建你自己的 JAVA 类非常容易。新建一个 phptest.java 文件,将它放置在你的 java.class.path 目录下【 c:\myclasses】,文件内容如下:

public class phptest{
 /**
 * A sample of a class that can work with PHP
 * NB: The whole class must be public to work,
 * and of course the methods you wish to call
 * directly.
 *
 * Also note that from PHP the main method
 * will not be called
 */
 public String foo;
 /**
 * Takes a string and returns the result
 * or a msg saying your string was empty
 */
 public String test(String str) {
  if(str.equals("")) {
   str = "Your string was empty. ";
  }
  return str;
 }
 /**
 * whatisfoo() simply returns the value of the variable foo.
 */
 public String whatisfoo() {
  return "foo is " + foo;
 }
 /**
 * This is called if phptest is run from the command line with
 * something like
 * java phptest
 * or
 * java phptest hello there
 */
 public static void main(String args[]) {
  phptest p = new phptest();
  if(args.length == 0) {
   String arg = "";
   System.out.println(p.test(arg));
  }else{
   for (int i = 0; i < args.length; i++) {
    String arg = args[i];
    System.out.println(p.test(arg));
   }
  }
 }
}
Copy after login

创建这个文件后,我们要编译好这个文件,在 DOS 命令行使用 javac phptest.java 这个命令。

为了使用 PHP 测试这个 JAVA 类,我们在web目录下创建一个 phptest.php 文件,内容如下:

<?php
$myj = new Java("phptest");
echo "Test Results are <b>" . $myj->test("Hello World") . "</b>";
$myj->foo = "A String Value";
echo "You have set foo to <b>" . $myj->foo . "</b><br>\n";
echo "My java method reports: <b>" . $myj->whatisfoo() . "</b><br>\n";
?>
Copy after login

如果你得到这样的警告信息:java.lang.ClassNotFoundException error ,这就意味着你的 phptest.class 文件不在你的 java.class.path 目录下。

注意的是 JAVA 是一种强制类型语言,而 PHP 不是,这样我们在将它们融合时,容易导致错误,于是我们在向JAVA传递变量时,要正确指定好变量的类型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678″;

这只是一个很小的例子,你可以创建你自己的 JAVA 类,并使用 PHP 很好的调用它!关键在于理解java.class.path目录的重要性。

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php实现支付宝在线支付接口开发详解

PHP基于pdo的数据库操作类【可支持mysql、sqlserver及oracle】

php实现压缩合并js的方法

The above is the detailed content of Detailed explanation of methods and examples of php calling its own java program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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