简体   繁体   English

将参数发送到 Java 程序,运行它并从 PHP 脚本获取输出

[英]Send parameters to a Java program, run it and get output from a PHP script

I have the following project structure:我有以下项目结构:

project
  |_____run_java.php
  |_____java_program.java

The java program has some input variable a , b , c . java 程序有一些输入变量a , b , c I need to send these variables from the PHP script and then run the java program.我需要从 PHP 脚本发送这些变量,然后运行 ​​java 程序。

The output in the java program is in the form of System.out.println(output); java程序中的System.out.println(output);System.out.println(output);的形式System.out.println(output); (Should I change it to return output; )? (我应该将其更改为return output; )?

I tried the following simple test script, even without any inputs but it doesn't work:我尝试了以下简单的测试脚本,即使没有任何输入,但它不起作用:

run_java.php : run_java.php :

<?php
echo shell_exec("javac java_program.java && java XYZ");

java_program.java : java_program.java :

class XYZ {
    public static void main(String args[]){
        System.out.println("Test");
    }
}

But when I open run_java.php the page is blank.但是当我打开run_java.php ,页面是空白的。

see this: https://www.php.net/manual/en/function.shell-exec.php看到这个: https : //www.php.net/manual/en/function.shell-exec.php

And, Try to keep the java file name and class name consistent.并且,尽量保持java文件名和类名一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM