Execute Java Class in PHP

Shell-exec executes the command 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 command 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');
//For jar files
echo shell_exec("java -cp Shell_For_php.jar shell_for_php.Shell_For_php");

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);
}

Leave a Reply

Your email address will not be published. Required fields are marked *