【问题标题】:Execute Java File and Its Input in PHP在 PHP 中执行 Java 文件及其输入
【发布时间】:2022-01-24 05:53:04
【问题描述】:

所以我想制作一个可以在 php 中执行 java 文件和输入(输入文件来自我,扩展名为 .in)的程序。执行后我想将结果作为输出文件(所以我可以检查它是否正确)。基本上我想在代码游戏中制作一些类似hackerrank或代码冲突的东西。

这是我的代码

 <?php
    $lang= "java";
    $command1 = "javac answer.java"; // for compile
    $command2 = "time java -cp Main <input.in> output.out"; 
    
    echo "Com1 : " . $command1 . "<br>";
    echo "Com2 : " . $command2 . "<br>";

    $result = "Accepted";
    $runtime = 0;

    $descriptorspec = array(
        0 => array("pipe", "r"), // stdin is a pipe that the child will read from
        1 => array("pipe", "w"), // stdout is a pipe that the child will write to
        2 => array("pipe", "w") //stderr is a pipe that the child will write to
        );

    $cwd = 'C:\xampp\htdocs\shell_exec\asset'; //The initial working dir for the command
    $process = proc_open($command1, $descriptorspec, $pipes, $cwd);
    echo "Command:" . $command1 . "<br />";
    echo "Descriptor:"; print_r( $descriptorspec);  echo "<br />";
    if (is_resource($process)) {

        $out = stream_get_contents($pipes[1]);
        echo "1." . $out . "<br />";
        fclose($pipes[1]);

        $out = stream_get_contents($pipes[2]);
        echo "2." . $out . "<br />";
        fclose($pipes[2]);

        $return_value = proc_close($process);
        if ($return_value != 0)
            $result = 'Compile Error';
    }
    

    //Check time limit
    if ($result == "Accepted")
    {
        $memory_limit = 64 * 1024; //64MB
        $time_limit = 15; //15second

        $process = proc_open($command2, $descriptorspec, $pipes, $cwd);
        echo 'bash -c "' . $command2 . '"';
        echo "<br />";
        if (is_resource($process))
        {
            $stream = stream_get_contents($pipes[2]);
            echo "Stream : " . $stream;
            fclose($pipes[2]);
            $return_value = proc_close($process);

            $timelimitstring = "CPU time limit exceeded";
            $memorylimitstring = "Memory size limit exceeded";

            if (strstr($stream, $timelimitstring) != null) {
                $result = 'Time Limit Exceeded';
            }

            if (strstr($stream, $memorylimitstring) != null) {
                $result = "Memory Limit Exceeded";
            }

            if ($result == "Accepted" && substr($stream, 1, 4) != "real") {
                $result = "Run Time Error";
            }

            $str = strstr($stream, "real"); 
            $im = strpos($str, "m"); 
            $is = strpos($str, "s"); 
            $m = substr($str, 5, $im - 5);
            $s = substr($str, $im + 1, $is - $im - 1);
            $runtime = number_format($m * 60 + $s, 3);
        }
    }

    echo "result : " . $result . "<br />";
    echo "runtime : " . $runtime . "<br />";
?>

这是我的目录

这是 answer.java 代码

package asset;
import java.util.Scanner;

class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        for(int t = 0;t < total;t++){
            int max = sc.nextInt();
            int[] players = new int[max];
            for(int count = 0;count < max;count++){
                players[count] = sc.nextInt();
            }
            int odd = 0, even = 0;
            for(int i:players){
                if(i%2 == 0)
                    even++;
                else
                    odd++;
            }
            
            if(odd > even)
                System.out.println("READY");
            else
                System.out.println("NOT READY");
        }
        sc.close();
    }
}

这是输入.in

3
3
12 23 4
4
2 33 19 11
1
5

问题是我在 output.out 中仍然有错误。它说

The system cannot accept the time entered.
Enter the new time: 3

我的代码有什么问题吗,或者你们能给我一个解决方案吗?感谢您提供的任何帮助????

【问题讨论】:

    标签: java php output execution proc-open


    【解决方案1】:

    你可以用这个:

    $input = readInputFromYourFile('input.in');
    shell_exec('cd C:\xampp\htdocs\shell_exec\asset'); 
    shell_exec('javac answer.java');
    $output = shell_exec('java answer '. $input);
    echo output ;
    

    编译和运行文件,结果将存储在输出中。 Source

    在您的 java 代码中的 main 函数中使用:

      public static void main(String[] args ){
        String input = args[0];
      }   
    

    如果您想在相同的函数调用中一个接一个地运行多个命令,请使用:

    shell_exec('cd C:\xampp\htdocs\shell_exec\asset & javac answer.java & java answer '. $input);
    

    我刚试过,它对我有用。

    【讨论】:

    • 请注意,您必须将输入格式化为没有空格,因为执行此命令时两个单词之间的空格java answer arg1 arg2 意味着您有不同的参数,因此在您的主 java 代码中您必须单独调用它们,例如这个:args[0] =arg1args[1] =arg2
    • 请问readInputFromYourFile()函数是从哪里得到的?因为它是未定义的。非常感谢?
    • 之后我应该如何编辑我的java文件?对不起,我仍然对你的解释感到困惑。我明白了这个概念,但在执行时我很困惑。
    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多