Example #1 Java Example
<?php
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
// demonstrate property access
echo 'Java version=' . $system->getProperty('java.version') . '<br />';
echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
echo 'OS=' . $system->getProperty('os.name') . ' ' .
$system->getProperty('os.version') . ' on ' .
$system->getProperty('os.arch') . ' <br />';
// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
echo $formatter->format(new Java('java.util.Date'));
?>
Example #2 AWT Example
<?php
// This example is only intended to be run using the CLI.
$frame = new Java('java.awt.Frame', 'PHP');
$button = new Java('java.awt.Button', 'Hello Java World!');
$frame->add('North', $button);
$frame->validate();
$frame->pack();
$frame->visible = True;
$thread = new Java('java.lang.Thread');
$thread->sleep(10000);
$frame->dispose();
?>
Exceptions raised result in PHP warnings, and NULL
results. The
warnings may be eliminated by prefixing the method call with an
"@" sign. The following APIs may be used to retrieve and reset
the last error:
anto dot justus at gmail dot com (2009-06-10 10:00:51)
php java
The PHP code here is simple and straight forward. It is not
OOP. But you probably got enough of that while doing the
Java code.
<?php
java_require("F:\\wamp5\\www\\classes\\");
$obj = new Java("hello");
// Call the "sayHello()" method
$output = $obj->SayHello();
echo $output.' this text in PHP
'; // Displays (so this comes from the class!)
// Call the "SayNumber()" method
$output = $obj->SayNumber();
// Displays (so this comes from the class!)
echo $output.' is a lucky number';
//Because the JVM caches everything we want reset while
playing with the code. Otherwise the
// a cached version of the same class file will be used
rather than the new one
echo "
Resetting back-end to initial state\n";
// suppress the warning message from the use of reset.
@java_reset();
?>
That's it. Now browse to the PHP page and you should get
the results shown below.
anto dot justus at gmail dot com (2009-06-10 09:08:19)
Session sharing with php and java
------------------------------------------
<?php require_once("java/Java.inc");
$session = java_session();
?>
<HTML>
<TITLE>PHP and JSP session sharing</title>
<BODY>
<?php
if(is_null(java_values($session->get("counter")))) {
$session->put("counter", 1);
}
$counter = java_values($session->get("counter"));
print "HttpSession variable \"counter\": $counter<br>\n";
$session->put("counter", $counter+1);
?>
<a href="sessionSharing.jsp">JSP page</a>
</BODY>
</HTML>