java 如何使用php运行java代码(.class)并在同一个网页上显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17375308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:49:15  来源:igfitidea点击:

How to run java code (.class) using php and display on the same web page

javaphphtml

提问by nils

I am trying to run a java program using php script.

我正在尝试使用 php 脚本运行一个 java 程序。

First, php displays a form where users input two values: Price and Sales Tax Rate. Next, it extracts the values and passes it to a java program (precompiled as .class file).

首先,php 显示一个表单,用户可以在其中输入两个值:价格和销售税率。接下来,它提取值并将其传递给 java 程序(预编译为 .class 文件)。

I am uncertain as to where the output is printed if at all the java code is working. My ultimate goal is to display the result to the user in an html page.

如果 java 代码正常工作,我不确定输出的打印位置。我的最终目标是在 html 页面中向用户显示结果。

I upload my file contents to my web server and try to run it from there.

我将我的文件内容上传到我的 Web 服务器并尝试从那里运行它。

update:

更新:

How can I use shell_exec or exec to run java code? I need to pass the parameters (price, salesTax) to shell_exec. Where is the returned output stored?

如何使用 shell_exec 或 exec 来运行 java 代码?我需要将参数(价格、销售税)传递给 shell_exec。返回的输出存储在哪里?

PHP code:

PHP代码:

> <?php
> 
> $salesTaxForm = <<<SalesTaxForm
> 
> <form action="SalesTaxInterface.php" method="post">
> 
>    Price (ex. 42.56):<br>
> 
>    <input type="text" name="price" size="15" maxlength="15"
> value=""><br>
> 
>    Sales Tax rate (ex. 0.06):<br>
> 
>    <input type="text" name="tax" size="15" maxlength="15"
> value=""><br>
> 
>    <input type="submit" name="submit" value="Calculate!">
> 
>    </form>
> 
> SalesTaxForm;
> 
> if (! isset($submit)) :
> 
>    echo $salesTaxForm;
> 
> else :    $salesTax = new Java("SalesTax");
> 
>    $price = (double) $price;    $tax = (double) $tax;
> 
>    print $salesTax->SalesTax($price, $tax);
> 
> endif;
> 
> ?>

Java Code:

Java代码:

import java.util.*;
import java.text.*;

public class SalesTax {
public String SalesTax(double price, double salesTax) 
{

    double tax = price * salesTax;

    NumberFormat numberFormatter;

    numberFormatter = NumberFormat.getCurrencyInstance();

    String priceOut = numberFormatter.format(price);

    String taxOut = numberFormatter.format(tax);

    numberFormatter = NumberFormat.getPercentInstance();

    String salesTaxOut = numberFormatter.format(salesTax);

    String str = "A sales Tax of " + salesTaxOut +

                 " on " + priceOut + " equals " + taxOut + ".";

    return str;

    }

}
import java.util.*;
import java.text.*;

public class SalesTax {
public String SalesTax(double price, double salesTax) 
{

    double tax = price * salesTax;

    NumberFormat numberFormatter;

    numberFormatter = NumberFormat.getCurrencyInstance();

    String priceOut = numberFormatter.format(price);

    String taxOut = numberFormatter.format(tax);

    numberFormatter = NumberFormat.getPercentInstance();

    String salesTaxOut = numberFormatter.format(salesTax);

    String str = "A sales Tax of " + salesTaxOut +

                 " on " + priceOut + " equals " + taxOut + ".";

    return str;

    }

}

回答by Josué Padilla

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:

shell-exec 执行您传递给它的命令。要使用它,你必须向你的类添加一个 Main 方法,并在命令行中传递参数等属性,所以最后它应该是这样的:

This is code that you have to execute on php

这是您必须在 php 上执行的代码

  $output = shell_exec('java SalesTax 10.0 20.0');

Where SalesTaxis your java class, 10.0is the first argument, and 20.0the second.

其中SalesTax是您的 Java 类,10.0是第一个参数,20.0是第二个参数。

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.

这是一个非常简单的实现,您仍然应该添加验证和其他一些东西,但我认为这是主要思想。也许将它移植到 php 应该更容易。

I hope that you find this helpful. :)

我希望你觉得这有帮助。:)

回答by Andbdrew

I'm no PHP expert, but there are a few ways you could do this:

我不是 PHP 专家,但有几种方法可以做到这一点:

So once you choose a path for your server side stuff, if you want to display the results without a page reload, you will want to use some javascript and most likely some jQuery.ajaxor maybe some jQuery('.target-area').load.

因此,一旦您为服务器端内容选择了路径,如果您想在不重新加载页面的情况下显示结果,您将需要使用一些 javascript 并且很可能使用一些jQuery.ajax或一些jQuery('.target-area').load.