PHP & Java(1)

原创 2001年01月09日 15:27:00

Mark Nold
Joost Soeterbroek

The Java extension is an extremely exciting tool. By learning how to use this module, you can extend PHP by the power of all available Java classes. To show you the basics of the Java extension, this article will cover installation and a few code examples of using PHP and Java together.

Windows Installation

The following configuration has been tested with Apache 1.3.12, PHP 4.0.3 binaries from www.php4win.de plus the 4.0.3 Zend Optimiser and JDK 1.2.2 from java.sun.com. We have also tested this configuration with older versions of the JDK and the various MS webservers (PWS and IIS) on Windows 95, Windows 98 and NT4.

Step 1: Install the JDK. This is fairly simple, as the JDK installs without many questions. It might be useful to check your environment (autoexec.bat in Windows 9x and System under Control Panel in NT) and make sure the jdk1.x.x/bin directory is in your path. This will make compiling your Java Classes easier. On Win9x add

PATH=%PATH%;C:/jdk1.2.2/bin
to your autoexec.bat. On NT add
;C:/jdk1.2.2/bin
to the end of the PATH environment variable. It is also important to note in your autoexec.bat file, the PHP Java extension ignores the JAVA_HOME and CLASSPATH set up in the environment. This is important because these items must be set correctly in your php.ini file for the Java extension to work.

Step 2: Modifying your php.ini. You need to add something similiar to your php.ini file

[java]
extension=php_java.dll
java.library.path=c:/web/php4/extensionsjava.class.path="c:/web/php4/extensions/jdk1.2.2/php_java.jar;c:/myclasses"

Typically, people put the extension=php_java.dll directive with the rest of the extensions, but it can sit happily under [java]. The java.library.path must be set in the location of the php_java.dll, and java.class.path must include the location of php_java.jar. The java.class.path should also include the path to other classes you may wish to use (note the double quotes!). In these examples, we will be talking about c:/myclasses. You should also note that the single period is ignored by PHP and Java. As far as we know, you cannot set PHP to look in the current directory for its Java classes.

Step 3: Testing your Install. Now, you're ready to go. Create a PHP file that looks something like this:


<?php

  $system = new Java("java.lang.System");
  print "Java version=".$system->getProperty("java.version")." <br>/n";
  print "Java vendor=".$system->getProperty("java.vendor")." <p>/n/n";
  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 'at' h:mm:ss a zzzz");
  print $formatter->format(new Java("java.util.Date"))."/n";
  
?>

NB: This is taken directly from Sam Ruby's examples. If you have correctly installed everything, you should see some results like:

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 

A very simple example, but it shows you can access currently available Java classes. Once you have this example working, you have successfully set up the PHP Java extension.

Windows下图文详解PHP三种运行方式(php_mod、cgi、fastcgi)

PHP能不能成功的在Apache服务器上运行,就看我们如何去配置PHP的运行方式。PHP运行目前为止主要有三种方式: a、以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成到Apache...
  • daiyutage
  • daiyutage
  • 2015年09月29日 16:10
  • 2344

PHP进程之信号捕捉中的declare(ticks=1)

在使用PHP的PCNTL拓展时,发现在信号捕捉的例程中出现了declare(ticks=1)这条php语句,于是对其进行研究。一. 语句在php中的意义php中,declare(ticks=n)和re...
  • Gavin_new
  • Gavin_new
  • 2017年03月24日 12:01
  • 815

【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全

PHP取整数函数常用的四种方法,下面收集了四个函数;经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval PHP取整...
  • lz0426001
  • lz0426001
  • 2015年01月14日 11:38
  • 5851

php中pdo的使用

第一步:测试pdo是否启用:运行如下代码,如果提示参数错误,说明PDO已经安装,如果说明对象不存在,则修改PHP配置文件php.ini,取消php_pdo_yourssqlserverhere.ext...
  • zhezhebie
  • zhezhebie
  • 2017年02月08日 11:31
  • 225

分享一下PHP中的While循环函数,以前觉得挺难的

while循环的方法重复任务,只要拥有一定的条件。例如,您可能想要提示用户输入一个值,只要他们继续输入一个无效的。换句话说,while循环是非常有用的,当你不知道有多少次你想要执行的指令集。 如果你...
  • qihailei1118
  • qihailei1118
  • 2015年12月27日 17:52
  • 629

java 解析php序列化数据问题

有很多情况会涉及到php和java进行数据交换。一般情况是通过json数据格式去交换。但是对于那种比如:商城采用php进行开发,管理系统使用java语言开发,这其中就会涉及到数据交互,而正好php进行...
  • TOP__ONE
  • TOP__ONE
  • 2017年03月21日 10:15
  • 1362

Java、Python和PHP三者的区别

Java、PHP、Python的一些基础只是的差别
  • qq_36520153
  • qq_36520153
  • 2017年07月15日 22:09
  • 1459

PHP与JAVA通信

使用php-java-brigde与JAVA通信 环境 实施过程 下载php-java-brigde应用包 解压php-java-brigde 表格 定义列表 代码块 脚注 目录 数学公式 UML 图...
  • u013815844
  • u013815844
  • 2015年08月19日 20:42
  • 657

判断PHP的exec函数是否执行成功

判断PHP的exec函数是否执行成功 做一个代码发布的系统,需要用到PHP的exec函数来执行Linux下的命令和git,svn命令,如何判断PHP的exec函数是否执行成功呢? 写个PHP...
  • zpf_nevergiveup
  • zpf_nevergiveup
  • 2017年07月23日 20:47
  • 411

对比一下PHP和JAVA开发网站的优劣

分享使用JAVA和PHP开发网站一些经验,并附上网站实例,供各位码农们参考
  • lishengbo
  • lishengbo
  • 2016年08月31日 10:50
  • 5711
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PHP & Java(1)
举报原因:
原因补充:

(最多只允许输入30个字)