You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: FAQ.html
+59-6Lines changed: 59 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -34,10 +34,26 @@ <H4>What about portability?</H4>
34
34
</p>
35
35
36
36
<H4>Do I need a Java Application Server or Servlet Engine?</H4>
37
-
<p>Not necessarily. But a servlet engine or J2EE service is easier to
38
-
install and to handle than a standalone Java service.
37
+
<p>Java needs an execution environment. Although there's a "java.so"
38
+
and a "php_java.dll", which can start Java as a sub process of a HTTP
39
+
server, a servlet engine or J2EE service is <ahref="http://tomcat.apache.org">easier to install</a> and to
40
+
handle than a standalone Java process started from some HTTP server.
41
+
</p>
42
+
<p>
43
+
You can also add PHP support to your Java application by adding the
44
+
following line to its main class:
45
+
<blockquote>
46
+
<code>
47
+
static final php.java.bridge.JavaBridgeRunner runner = php.java.bridge.JavaBridgeRunner.getRequiredInstance(8087);
48
+
</code>
49
+
</blockquote>
50
+
The above code opens the port 8087, so that local PHP scripts can call
51
+
methods/procedures from your Java application, as long as your Java
52
+
application is running.
53
+
</p>
54
+
<p>The third option is the standard JSR 223 script interface, which
55
+
allows one to execute PHP code/scripts from Java applications.
39
56
</p>
40
-
41
57
<H4>Ho do I enable logging?</H4>
42
58
<p>
43
59
Copy <code>log4j.jar</code> into <code>java.ext.dirs</code>. Example for JDK 6:
@@ -97,16 +113,42 @@ <H4>What's the difference between SERVLET_LOCAL:8080 and INET_LOCAL:8080?</H4>
97
113
<p>INET_LOCAL always uses local TCP socket communication.</p>
98
114
99
115
<H4>Whenever I reboot my computer I have to start the bridge back end again. How can I automate this?</H4>
100
-
<p>Download and install a servlet engine or J2EE server, for example tomcat.</p>
116
+
<p>Download and install a servlet engine or J2EE server as a Windows
117
+
or Unix service.</p>
101
118
102
119
<H4>Do I have to require Java.inc in each of my scripts? Isn't that very slow?</H4>
103
-
<p>The PHP/Java Bridge library <code>Java.inc</code> must be included before it can be used. Therefore the scripts should contain the statement
120
+
<p>In order to communicate with Java, a PHP "Java" class definition is needed. Here's a simple PHP "Java" class definition which fits into one line:
121
+
<blockquote>
122
+
<code>
123
+
<?php<br>
124
+
// The following is the "Java" class definition, stripped down to fit into one line<br>
125
+
// To use this sample start Java with: java -jar JavaBridge.jar INET:9267<br>
126
+
// Or enable java.so or php_java.dll, which automatically start the above<br>
127
+
// process Then type: php sample.php<br>
128
+
//<br>
129
+
class P {const Pc="<C v=\"%s\" p=\"I\">", PC="</C>"; const Pi="<I v=\"%d\" m=\"%s\" p=\"I\">", PI="</I>"; const Ps="<S v=\"%s\"/>", Pl="<L v=\"%d\" p=\"%s\"/>", Po="<O v=\"%d\"/>"; private $c; function str($s){fwrite($this->c, sprintf(self::Ps, $s));} function obj($s){fwrite($this->c, sprintf(self::Po, $s->java));} function __construct(){$this->c=fsockopen("127.0.0.1",9267);} function cBeg($s){fwrite($this->c, sprintf(self::Pc, $s));} function cEnd(){fwrite($this->c, self::PC);} function iBeg($o, $m){fwrite($this->c, sprintf(self::Pi, $o, $m));} function iEnd(){fwrite($this->c, self::PI);} function val($s){if(is_object($s))$this->obj($s);else $this->str((string)$s);} function res(){$r=sscanf(fread($this->c, 8192),"%s v=\"%[^\"]\"");return $r[1];}} class Java {var $java, $p; function __construct() {if(!func_num_args()) return; $this->p=new P(); $ar=func_get_args(); $this->p->cBeg(array_shift($ar)); foreach($ar as $arg) $this->p->val($arg); $this->p->cEnd(); $ar = sscanf($this->p->res(), "%d"); $this->java=$ar[0];} function __call($meth, $args) {$this->p->iBeg($this->java, $meth); foreach($args as $arg) $this->p->val($arg); $this->p->iEnd(); $proxy = new Java(); $ar = sscanf($this->p->res(), "%d"); $proxy->java=$ar[0]; $proxy->p=$this->p; return $proxy;} function toString() {$this->p->iBeg("", "castToString"); $this->p->val($this); $this->p->iEnd(); return base64_decode($this->p->res());}}<br>
130
+
<br>
131
+
// Test<br>
132
+
$i1 = new Java("java.math.BigInteger", "1");<br>
133
+
$i2 = new Java("java.math.BigInteger", "2");<br>
134
+
$i3 = $i1->add($i2);<br>
135
+
echo $i3->toString() . "\n";<br>
136
+
<br>
137
+
?><br>
138
+
</code>
139
+
</blockquote>
140
+
</p>
141
+
<p>The above simple "Java" class assumes that some Java VM has been
142
+
started on host "127.0.0.1", port "9267". And it cannot handle values
143
+
larger than 8192 bytes. Therefore the PHP/Java Bridge
144
+
library <code>Java.inc</code> should be used. Scripts should contain
145
+
the statement
104
146
<blockquote>
105
147
<code>
106
148
require_once("...java/Java.inc");
107
149
</code>
108
150
</blockquote>
109
-
at the beginning of the script. PHP compiles and caches PHP scripts, the <code>Java.inc</code> library is loaded only once.
151
+
at the beginning of each script. PHP compiles and caches PHP scripts, the <code>Java.inc</code> library is loaded only once.
110
152
</p>
111
153
112
154
<H4>Can I use Java libraries without installing java?</H4><p>Yes. On a GNU operating system (e.g.: GNU/Linux, GNU/windows (aka "cygwin"), ...) you can use the GCC compiler to compile Java classes to native code.
@@ -115,6 +157,12 @@ <H4>Can I use Java libraries without installing java?</H4> <p>Yes. On a GNU oper
115
157
<code>libgcj</code> library, which is part of the GNU gcc compiler. This library also uses much less system
116
158
resources (memory, files) than a "real" Java VM.</p>
117
159
160
+
<H4>I can't load the resource or file from the current working directory!?!</H4>
161
+
<p>
162
+
The "current working directory" is not useful, as the Java back end may be running
163
+
on a different server or from a different working directory. Use a full path or a URL resource instead.
164
+
</p>
165
+
118
166
<H4>I get a blank page or some other error!?!</H4>
119
167
<p>
120
168
Check the PHP error log, see your <code>php.ini</code> file for details. If the command:
@@ -1124,6 +1172,11 @@ <H4>What about Java 5 varargs?</H4>
1124
1172
</code>
1125
1173
</blockquote>
1126
1174
</p>
1175
+
<H4>NULL tests</H4>
1176
+
<p>
1177
+
Use <code>java_is_null($value)</code> or <code>is_null (java_values ($value))</code> to test for a (Java-) NULL value.
0 commit comments