- Project Admins
- Invoke Java desktop application methods.
- Create and test your Java desktop application.
- Add the PHP/Java Bridge library to your Java application
- Saved searches
- Use saved searches to filter your results more quickly
- License
- php-java/php-java
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Project Admins
Site Powered by PHP and MySQL.
Recommended browsers: Firefox or Safari.
Invoke Java desktop application methods.
This guide shows how to create a Java desktop application and how to call its Java procedures or methods from PHP scripts.
Create and test your Java desktop application.
- Create a simple application, HelloWorld.java , which just displays a «hello world» dialog box:
import javax.swing.JOptionPane;
public class HelloWorld public static void main(String args[]) throws Exception JOptionPane.showMessageDialog(null, «hello world»);
System.exit(0);
>
public void hello(String args[]) throws Exception JOptionPane.showMessageDialog(null, «hello » + args[0]);
>
>
Create a manifest file, MANIFEST.MF , with the following content:
Add the PHP/Java Bridge library to your Java application
- Copy JavaBridge.jar to the current directory.
public class HelloWorld public static final String JAVABRIDGE_PORT=»8087″;
static final php.java.bridge.JavaBridgeRunner runner =
php.java.bridge.JavaBridgeRunner.getInstance(JAVABRIDGE_PORT);
public static void main(String args[]) throws Exception runner.waitFor();
System.exit(0);
>
public void hello(String args[]) throws Exception JOptionPane.showMessageDialog(null, «hello » + args[0]);
>
>
Saved searches
Use saved searches to filter your results more quickly
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.
License
php-java/php-java
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
PHPJava is an experimental library which emulates JVM (a.k.a. Java Virtual Machine) and compiling intermediate code by PHP 🐘 And PHPJava reads binary from pre-compiled Java file(s) ☕ So, PHPJava is NOT bridge to Java. This library can be run 100% in PHP. This project referred to Java Virtual Machine Specification documentation at the time we made it.
We are welcoming any contributions to this project 💪
Contribution guide is here:
The Intermediate Code Compiler
The JVM language of PHP syntax
You can run PHPJava as same as an executable binary.
./vendor/bin/PHPJava HelloWorld
./vendor/bin/PHPJava HelloWorld.class
./vendor/bin/PHPJava -m jar HelloWorld.jar
$ composer require php-java/php-java:dev-master
class HelloWorld < public static void main(String[] args) < System.out.println(args[0]); > >
$ javac -encoding UTF8 /path/to/HelloWorld.java
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; JavaClass::load('HelloWorld') ->getInvoker() ->getStatic() ->getMethods() ->call( 'main', ["Hello World!"] ); // Or, you can specify file path as follows. (new JavaClass(new JavaCompiledClass(new FileReader('/path/to/HelloWorld.class')))) ->getInvoker() ->getStatic() ->getMethods() ->call( 'main', ["Hello World!"] );
$ php /path/to/HelloWorld.php Hello World!
Java Archive (Execute *.jar file)
$ javac -encoding UTF8 -d build src/* $ cd build && jar -cvfe ../Test.jar Test *
use PHPJava\Core\JavaArchive; // You must pass parameters to entrypoint within the `execute` method. // The `execute` method does not have any default parameters. (new JavaArchive('Test.jar'))->execute([]); // or (new JavaArchive('Test.jar')) ->getClassByName('Test') ->getInvoker() ->getStatic() ->getMethods() ->call( 'main', [] );
e.g., Set or Get static fields:
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; $staticFieldAccessor = JavaClass::load('HelloWorld') ->getInvoker() ->getStatic() ->getFields(); // Set $staticFieldAccessor->set('fieldName', 'value'); // Get echo $staticFieldAccessor->get('fieldName');
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; JavaClass::load('HelloWorld') ->getInvoker() ->getStatic() ->getMethods() ->call( 'methodName', $firstArgument, $secondArgument, $thirdArgument, . ); // Or, if the called method has a return value, you can store it to a variable. $result = JavaClass::load('HelloWorld') ->getInvoker() ->getStatic() ->getMethods() ->call( 'methodWithSomethingReturn', $firstArgument, $secondArgument, $thirdArgument, . ); // Output the $result you want echo $result;
If you want to get/set dynamic fields, you need to call the construct method on Java by PHPJava.
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; $javaClass = JavaClass::load('HelloWorld'); $javaClass->getInvoker()->construct(); $dynamicFieldAccessor = $javaClass ->getInvoker() ->getDynamic() ->getFields(); // Set $dynamicFieldAccessor->set('fieldName', 'value'); // Get echo $dynamicFieldAccessor->get('fieldName');
If you want to call a dynamic method (same as a field), you need to call the construct method on Java by PHPJava.
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; $dynamicMethodAccessor = JavaClass::load('HelloWorld') ->getInvoker() ->construct() ->getDynamic() ->getMethods(); $dynamicMethodAccessor ->call( 'methodName', $firstArgument, $secondArgument, $thirdArgument, . ); // Or, if the called method has a return value, you can store it to a variable. $dynamicMethodAccessor ->call( 'methodWithSomethingReturn', $firstArgument, $secondArgument, $thirdArgument, . ); // Output the $result you want echo $result;
Call a method in a built-in package on Java
PHPJava can call a built-in package in the same way as JavaClass::load after the version 0.0.8.5. This feature is emulated by ReflectionClass on PHP and any static methods/fields will dynamically be generated in fact.
e.g.) To Call java.lang.Math is below.
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; echo JavaClass::load('java.lang.Math') ->getInvoker() ->getStatic() ->getMethods() ->call( 'pow', 2, 4 );
The example will return 16 .
Call ambiguous methods in Java from PHP
- In PHP, types are more ambiguous than Java.
- For example, you may want to call a method that accepts a long parameter in Java from PHP. In this case, you can call that method as follows:
e.g., [Recommended] Wrap with \PHPJava\Kernel\Types\Long_ .
class Test < public static void includingLongTypeParameter(long n) < System.out.println(n); > >
$javaClass->getInvoker()->getStatic()->getMethods()->call( 'includingLongTypeParameter', new \PHPJava\Kernel\Types\Long_ (1234) );
The example will return 1234 .
e.g., Set false to strict mode option.
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; $javaClass = JavaClass::load( 'HelloWorld', [ 'strict' => false, ] );
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; $javaClass = JavaClass::load( 'HelloWorld', [ 'max_stack_exceeded' => 12345, 'validation' => [ 'method' => [ 'arguments_count_only' => true, ], ], ] );
use PHPJava\Core\JVM\Parameters\GlobalOptions; use Monolog\Logger; GlobalOptions::set([ 'log' => [ 'level' => Logger::DEBUG, ], 'validation' => [ 'method' => [ 'arguments_count_only' => true, ], ], ]);
Output PHPJava operations
use PHPJava\Core\JavaClass; use PHPJava\Core\Stream\Reader\FileReader; $javaClass = JavaClass::load('HelloWorld'); $javaClass ->getInvoker() ->getStatic() ->getMethods() ->call( 'main', ["Hello", 'World'] ); // Show debug traces. $javaClass->debug();
[method] public static void main(java.lang.String[]) [code] [executed] PC | OPCODE | MNEMONIC | OPERANDS | LOCAL STORAGE ---------+--------+----------------------+------------+----------------- 0 | 0xB2 | getstatic | 0 | 1 3 | 0x2A | aload_0 | 1 | 1 4 | 0x03 | iconst_0 | 2 | 1 5 | 0x32 | aaload | 3 | 1 6 | 0xB6 | invokevirtual | 2 | 1 9 | 0xB2 | getstatic | 0 | 1 12 | 0x2A | aload_0 | 1 | 1 13 | 0x04 | iconst_1 | 2 | 1 14 | 0x32 | aaload | 3 | 1 15 | 0xB6 | invokevirtual | 2 | 1 18 | 0xB2 | getstatic | 0 | 1 21 | 0x2A | aload_0 | 1 | 1 22 | 0x05 | iconst_2 | 2 | 1 23 | 0x32 | aaload | 3 | 1 24 | 0xB6 | invokevirtual | 2 | 1 27 | 0xB1 | return | 0 | 1 ---------+--------+----------------------+------------+-----------------
- [method] shows the called method.
- [code] shows the JVM’s real programs.
- [executed] shows the executed programs.
- PC shows the Program Counter.
- OPCODE shows the Operation Codes.
- MNEMONIC shows the names of the Operation Codes.
- OPERANDS shows the stacked items on memory.
- LOCAL STORAGE shows the stacked items on a method.
About big number calculation
- In normally, PHP cannot calculate big numbers as such as long and double types. But, PHPJava uses external Math library for covering above problems. And, PHPJava to use Java’s type as below comparison table. Therefore, we recommend to cast them to string on PHPJava. And, if it can be calculated with 64-bitPHP, PHPJava uses PHP’s arithmetic operations.
Run Kotlin on the PHPJava
Do you wanna run Kotlin on the PHPJava? Are you serious? Haha, yes, you can, but this feature is currently experimental.
fun main(args: ArrayString>) < println("Hello World!") >
$ kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
use PHPJava\Core\JavaArchive; $jar = new JavaArchive(__DIR__ . '/HelloWorld.jar'); $jar->execute([]);
You’ll get the result: Hello World! .
$ ./vendor/bin/phpunit tests/Cases
$ ./vendor/bin/phpcs --standard=phpcs.xml src