Crux
Crux

Reputation: 33

Execute Java File and Its Input in PHP

So i want to make a program that can execute java file and the input (input file is from me with .in extension) in php. After the execution i want to get the result as output file (so i can check wheter its correct or not). Basicly i want to make some kind like hackerrank or clash of code in codegames.

Here is my code

 <?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 />";
?>

Here are my directories

enter image description here

This is the answer.java code

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

This is the input.in

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

The problem is i still having an error in output.out. it says that

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

Is there anything wrong with my code or can you guys give me a solution? I appreciate any help that you give 🙏

Upvotes: 0

Views: 247

Answers (1)

Yasser CHENIK
Yasser CHENIK

Reputation: 407

You can use this :

$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 ;

To compile and run the file , and the results will be stored in output. Source

In your main function in your java code use :

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

If you want to run multiple commands one after another IN the Same function call use :

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

I just tried it and it worked for me .

Upvotes: 1

Related Questions