Run Java class file from PHP script on a website
The PHP exec()
function is the way to go, but you should be very careful in what you allow to executed.. in other words don't rely on user input as it could potentially compromise your entire server.
Calling the Java application launcher using exec, you can execute any Java application from PHP, e.g.
<?php exec("java -jar file.jar arguments", $output); ?>
How to run java code (.class) using php and display on the same web page
shell-exec executes the comand that you pass to it. To use this, you have to add a Main method to your class, and pass the properties like arguments in the comand line, so at the end it should look like this:
This is code that you have to execute on php
$output = shell_exec('java SalesTax 10.0 20.0');
Where SalesTax is your java class, 10.0 is the first argument, and 20.0 the second.
Your main method should be something like this
public static void main(String args[]){
double price = Double.valueOf(args[0]);
double salesTax = Double.valueOf(args[1]);
String output = SalesTax(price,salesTax);
System.out.println(output);
}
It's a very simple implementation, you should still add validations and some other stuff, but I think that it's the main idea. Maybe it should be easier to just port it to php.
I hope that you find this helpful. :)
Execute java class in PHP
I would recommend using Java/PHP Bridge found here: http://php-java-bridge.sourceforge.net/pjb/
It's quite easy to install and works very well.
Also, I recommend using the following link to download it. (it's the same one as the link in downloads->documentation)
http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.2.1/php-java-bridge_6.2.1_documentation.zip/download
The file is JavaBridge.war. You'll probably want to use Tomcat for the Java EE container. Once Tomcat is set up, you just put this file in the webapps folder and it's installed.
If you want to regularly use java classes in PHP this is the best method I know of and I have tried a lot of them. Resin also worked, but it didn't play nice with my mail server.
Related Topics
What's the Difference Between JavaScript and Java
Selenium: How to Stop Geckodriver Process Impacting Pc Memory, Without Calling Driver.Quit()
What Is a Nullpointerexception, and How to Fix It
How to Compare Strings in Java
Is Java "Pass-By-Reference" or "Pass-By-Value"
Scanner Is Skipping Nextline() After Using Next() or Nextfoo()
What Causes a Java.Lang.Arrayindexoutofboundsexception and How to Prevent It
The Use of Multiple Jframes: Good or Bad Practice
How to Write a Correct Micro-Benchmark in Java
What Does a "Cannot Find Symbol" or "Cannot Resolve Symbol" Error Mean
How to Add Jtable in Jpanel With Null Layout
How to Avoid Java Code in Jsp Files, Using Jsp 2
Should I Avoid the Use of Set(Preferred|Maximum|Minimum)Size Methods in Java Swing
What's the Simplest Way to Print a Java Array
How to Print My Java Object Without Getting "Sometype@2F92E0F4"