php calls the java method: first download "php-java-bridge_6.2.1_documentation.zip" and run JavaBridge; then create a new test.php in the www directory; finally call the custom JAVA class in PHP. Can.
Recommended: "PHP Video Tutorial" "java Video Tutorial"
1. PHP calls JAVA method
1.1. Software requirements
- Download php-java-bridge_6.2.1_documentation.zip, the download address is: http://php-java-bridge.sourceforge .net/pjb/download.php
- JavaBridge.jar: Unzip the above php-java-bridge_6.2.1_documentation.zip to get a JavaBridge.war, rename JavaBridge.war to JavaBridge.jar, again Use WinRAR to decompress JavaBridge.jar to the JavaBridge directory. JavaBridge.jar and Lucene.jar can be found in JavaBridge\WEB-INF\LIB.
- JDK: JavaBridge is implemented by the Java language, so JDK must be installed to provide support for jar file execution. And configure the environment variables.
1.2. Run JavaBridge
- Double-click to run JavaBridge.jar in JavaBridge\WEB-INF\LIB. A dialog box that can be selected should pop up. If it does not pop up , because the JDK is not installed or there is a file association error. The solution is to install the JDK or run "start javaw -jar JavaBridge.jar" (the content is saved in *.bat, and *.bat is in the same directory as JavaBridge.jar) instead of double-clicking . When the dialog box pops up, there is no need to make a selection, just click "OK". Correct as follows:
1.3. Test JavaBridge
- Create a new test.php in the www directory with the following content:
require_once("java/Java.inc"); $system = new Java('java.lang.System'); $s = new Java("java.lang.String", "php-java-bridge config...<br><br>"); echo $s; print 'Java version='.$system->getProperty('java.version').' <br>'; print 'Java vendor=' .$system->getProperty('java.vendor').' <br>'; print 'OS='.$system->getProperty('os.name').' '. $system->getProperty('os.version').' on '. $system->getProperty('os.arch').' <br>'; $formatter = new Java('java.text.SimpleDateFormat', "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); print $formatter->format(new Java('java.util.Date')).' <br>'.' <br>';
- Find JavaBridge.jar in JavaBridge\WEB-INF\LIB, decompress JavaBridge.jar and you can find the java directory in JavaBridge\META-INF. Completely copy the java directory to the same directory as test.php. Then run: http://localhost/test.php, the following content will be output, indicating that the JavaBridge installation is successful
1.4. Call customization in PHP JAVA class
- First create a test class Test.java:
public class Test { private String name = ""; // setter and getter public void setName(String name) { this.name = name; } public String getName() { return this.name; } //加法 public float add(float num1, float num2) { return num1 + num2; } }
- After writing the class, compile Test.java to generate a class file, and convert Test. Class is copied to the C:\Program Files\Java\jre7\classes directory. Because of different version numbers, the name of the jre7 folder may be different. When installing the JDK, there is no C:\Program Files\Java\jre7\classes by default and needs to be created manually.
- Modify the content of Test.php to:
//自定义类测试 require_once("java/Java.inc"); $test = new Java("Test"); //产生实例 $test->setName("哈哈,PHP调用JAVA的方法!"); //后面的调用就跟在php中调用类方法一样 print "调用类Test的getName方法,返回值为:".$test->getName()."<br>"; print "调用Test的add方法,返回值为:".$test->add(11.2, 15.7);
- Then visit http://localhost/test.php, the page will output the following content:
- It took me a day to work on this thing. I found many methods on the Internet but it didn’t work. It is inconvenient to move the class to the jre directory every time. Add java_require ("Test.jar"); This error is reported when quoting the jar package: Warning: java_require() not supported anymore. Please use
The above is the detailed content of How to call java method in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.