【发布时间】: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