title: Execute Java File and Its Input in PHP
body: <p>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.</p>
  <p>Here is my code</p>
  <pre><code> &lt;?php
      $lang= &quot;java&quot;;
      $command1 = &quot;javac answer.java&quot;; // for compile
      $command2 = &quot;time java -cp Main &lt;input.in&gt; output.out&quot;; 
      
      echo &quot;Com1 : &quot; . $command1 . &quot;&lt;br&gt;&quot;;
      echo &quot;Com2 : &quot; . $command2 . &quot;&lt;br&gt;&quot;;
  
      $result = &quot;Accepted&quot;;
      $runtime = 0;
  
      $descriptorspec = array(
          0 =&gt; array(&quot;pipe&quot;, &quot;r&quot;), // stdin is a pipe that the child will read from
          1 =&gt; array(&quot;pipe&quot;, &quot;w&quot;), // stdout is a pipe that the child will write to
          2 =&gt; array(&quot;pipe&quot;, &quot;w&quot;) //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 &quot;Command:&quot; . $command1 . &quot;&lt;br /&gt;&quot;;
      echo &quot;Descriptor:&quot;; print_r( $descriptorspec);  echo &quot;&lt;br /&gt;&quot;;
      if (is_resource($process)) {
  
          $out = stream_get_contents($pipes[1]);
          echo &quot;1.&quot; . $out . &quot;&lt;br /&gt;&quot;;
          fclose($pipes[1]);
  
          $out = stream_get_contents($pipes[2]);
          echo &quot;2.&quot; . $out . &quot;&lt;br /&gt;&quot;;
          fclose($pipes[2]);
  
          $return_value = proc_close($process);
          if ($return_value != 0)
              $result = 'Compile Error';
      }
      
  
      //Check time limit
      if ($result == &quot;Accepted&quot;)
      {
          $memory_limit = 64 * 1024; //64MB
          $time_limit = 15; //15second
  
          $process = proc_open($command2, $descriptorspec, $pipes, $cwd);
          echo 'bash -c &quot;' . $command2 . '&quot;';
          echo &quot;&lt;br /&gt;&quot;;
          if (is_resource($process))
          {
              $stream = stream_get_contents($pipes[2]);
              echo &quot;Stream : &quot; . $stream;
              fclose($pipes[2]);
              $return_value = proc_close($process);
  
              $timelimitstring = &quot;CPU time limit exceeded&quot;;
              $memorylimitstring = &quot;Memory size limit exceeded&quot;;
  
              if (strstr($stream, $timelimitstring) != null) {
                  $result = 'Time Limit Exceeded';
              }
  
              if (strstr($stream, $memorylimitstring) != null) {
                  $result = &quot;Memory Limit Exceeded&quot;;
              }
  
              if ($result == &quot;Accepted&quot; &amp;&amp; substr($stream, 1, 4) != &quot;real&quot;) {
                  $result = &quot;Run Time Error&quot;;
              }
  
              $str = strstr($stream, &quot;real&quot;); 
              $im = strpos($str, &quot;m&quot;); 
              $is = strpos($str, &quot;s&quot;); 
              $m = substr($str, 5, $im - 5);
              $s = substr($str, $im + 1, $is - $im - 1);
              $runtime = number_format($m * 60 + $s, 3);
          }
      }
  
      echo &quot;result : &quot; . $result . &quot;&lt;br /&gt;&quot;;
      echo &quot;runtime : &quot; . $runtime . &quot;&lt;br /&gt;&quot;;
  ?&gt;
  </code></pre>
  <p>Here are my directories</p>
  <p><a href="https://i.stack.imgur.com/1GHpY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1GHpY.png" alt="enter image description here" /></a></p>
  <p>This is the answer.java code</p>
  <pre><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 &lt; total;t++){
              int max = sc.nextInt();
              int[] players = new int[max];
              for(int count = 0;count &lt; max;count++){
                  players[count] = sc.nextInt();
              }
              int odd = 0, even = 0;
              for(int i:players){
                  if(i%2 == 0)
                      even++;
                  else
                      odd++;
              }
              
              if(odd &gt; even)
                  System.out.println(&quot;READY&quot;);
              else
                  System.out.println(&quot;NOT READY&quot;);
          }
          sc.close();
      }
  }
  </code></pre>
  <p>This is the input.in</p>
  <pre><code>3
  3
  12 23 4
  4
  2 33 19 11
  1
  5
  </code></pre>
  <p>The problem is i still having an error in output.out. it says that</p>
  <pre><code>The system cannot accept the time entered.
  Enter the new time: 3
  </code></pre>
  <p>Is there anything wrong with my code or can you guys give me a solution? I appreciate any help that you give </p>