title: Java : Subprocess which using Scanner throws NoSuchElementException
body: <p>Sorry for awkward english; It not my native language.</p>
  
  <p>I'm developing Online-Judge to use in next semester.</p>
  
  <p>That will receive one .java file from a student and compile it,</p>
  
  <p>and run .class and test it with predefined testcases by professor.</p>
  
  <p>But when I submit an java file below, Enter.java, execution error has arose and .java file is judged as ExecutionError.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Enter {
  
      public static void main(String[] args) {
      // TODO Auto-generated method stub
  
          int i=0;
          int max=0;
          int min=10000000;
  
          while(true)
          {
              System.out.print("Enter integer :");
  
              Scanner input = new Scanner(System.in);
              i = input.nextInt();
  
              if(i==-1)
                  break;
  
              else if(i&gt;max)
              {
                  max=i;
              }
              else if(i&lt;min)
              {
                  min=i;
              }
  
          }
          System.out.printf("Smallest Integer is : %d",min);
          System.out.println();
          System.out.printf("Largest Integer is : %d", max);
      }
  
  }
  </code></pre>
  
  <p>It is not a correct source code of course, but it is wield to me that system judged it as ExecutionError(It means an exception occurs while system runs .class to test it with testcases). 
  And NoSuchElementException also occurs in input.nextInt(); 
  But It supposed to be judged as Fail(runs and ends well, but produces wrong output).</p>
  
  <p>Here is source of JudgeModule.java</p>
  
  <pre><code>private Judgement judge() {
  
      // some works before compile &amp; test //
  
      // phase 1. compile //
      CompileResult compileResult = tester.doCompile(compiler, fd);
  
      if( !compileResult.isSuccess() )    // compile fail
      {
          // some works
          Judgement judgement = new Judgement(compileResult.getResult());
          return judgement;
      }
  
      // pahse 2. execute
      ExecuteResult executeResult = tester.doTest(compiler, fd, testcases);
  
      if( executeResult.getException() != null )
      {
          // some works
          Judgement judgement = new Judgement(executeResult.getResult());
          return judgement;
      }
  
      // some works after compile &amp; test//
  
      Judgement judgement = new Judgement(executeResult.getResult());
  
      return judgement;
  }
  </code></pre>
  
  <p>tester is just an instance of SourcecodeTester, have no member variables and just holds doCompile() and doTest().</p>
  
  <p>Below is the source of SourcecodeTester. I omit doCompile() things.</p>
  
  <pre><code>public class SourcecodeTester {
  
      static long executeTimeout = 1000;
      public class ExecuteResult {
          JudgeResult result;
          Exception exception;
          ArrayList&lt;TestResult&gt; testResults;
      }
      public class TestResult {
          JudgeResult result;
          Exception exception;
          String output;
      }
  
  
      public ExecuteResult doTest(Compiler compiler, FileDescription fd, Testcase[] testcases )
      {
          // pahse 2. execute
          JudgeResult result = JudgeResult.Pass;
          Exception exception = null;
          ArrayList&lt;TestResult&gt; testResults = new ArrayList&lt;&gt;();
  
          try {
              for( Testcase testcase : testcases )
              {
                  testResults.add( execute(compiler, testcase, fd ) );
              }
  
          } catch( Exception e ) {
              e.printStackTrace();
              result = JudgeResult.SystemError;
              exception = e;
          }
          // do some works
  
          return new ExecuteResult(result, exception, testResults );
      }
  
  
      private TestResult execute(Compiler compiler, Testcase testcase, FileDescription fd ) 
      {
          Runtime runTime = Runtime.getRuntime();
  
          String executeCommand = assembleExecuteCommand(compiler, fd);
  
          JudgeResult executeResult = JudgeResult.Fail;
          Exception executeException = null;
          String executeOutput = "";
          try {
              boolean timeout = false;
              long startTime = System.currentTimeMillis();
  
              Process executeProcess = runTime.exec(executeCommand);
              writeToProcessInput(executeProcess, testcase.getInput());       // write to process input
              while( executeProcess.isAlive() &amp;&amp; !timeout )
              {
                  Thread.sleep(10);
                  if((System.currentTimeMillis()-startTime) &gt; executeTimeout )
                      timeout = true;
              }
  
              if( timeout )
              {
                  executeProcess.destroy();
                  throw new JudgeException("execution timeout!" + (System.currentTimeMillis()-startTime) + "ms", JudgeResult.ExecutionTimeout);
              }
  
              // exam exitValue
              int exitValue = executeProcess.exitValue();
  
              // read from process output
              char[] processOutput = readFromProcessOutput(executeProcess);
  
              // read from process error
              char[] processError = readFromProcessError(executeProcess);
  
              if( exitValue != 0 ) {
                  throw new JudgeException("execution fail with exitvalue : " + exitValue + " &gt;\n" + String.valueOf(processError), JudgeResult.ExecutionError);
              }
              else if( processError.length != 0 ) {
                  throw new JudgeException("execution fail with error : " + String.valueOf(processError), JudgeResult.ExecutionError );
              }
  
              String output = String.valueOf(testcase.getOutput()).replaceAll("\r\n", "\n");
              executeOutput = String.valueOf(processOutput);
  
              if( Arrays.equals(processOutput, output.toCharArray()) ) {
                  executeResult = JudgeResult.Pass;
              }
              else {
                  executeResult = JudgeResult.Fail;
              }
          } 
          catch( JudgeException e ) {
              e.printStackTrace();
              executeResult = JudgeResult.ExecutionError;
              executeException = e;
          }
          catch( Exception e ) {
              e.printStackTrace();
              executeResult = JudgeResult.SystemError;
              executeException = e;
          }
          return new TestResult( testcase.getId(), executeResult, executeException, executeOutput );
      }
  
  
      private void writeToProcessInput(Process _process, char[] _dataToWrite ) throws IOException
      {
          OutputStreamWriter osw = new OutputStreamWriter(_process.getOutputStream() );
          BufferedWriter writer = new BufferedWriter( osw );
          writer.write( _dataToWrite );
  //      osw.write(_dataToWrite);
          writer.close();
          osw.close();
      }
  
      private char[] readFromProcessOutput(Process _process) throws IOException
      {
          return readFromStream(  new InputStreamReader( _process.getInputStream() )  );
      }
  
      private char[] readFromProcessError(Process _process) throws IOException
      {
          return readFromStream(  new InputStreamReader( _process.getErrorStream() )  );
      }
  
      private char[] readFromStream(InputStreamReader isr) throws IOException {
          int numOfByteRead;
          char[] readBuffer = new char[1024];
          StringBuffer standardOutput = new StringBuffer();
          while ((numOfByteRead = isr.read(readBuffer)) &gt; 0) {
              standardOutput.append(readBuffer, 0, numOfByteRead);
          }
  
          return standardOutput.toString().toCharArray();
      }
  
  }
  </code></pre>
  
  <p>When I compile and execute Enter.java from command line or Eclipse,
  It works fine and I can put several numbers from console and there is no exception occurs.</p>
  
  <p>But I submit it to OnlineJudge and then NoSuchElementException occurs.
  Input char array for testing is just contains several numbers seperated by space character.</p>
  
  <p>Finally, What I want to ask is,</p>
  
  <ol>
  <li><p>What's the difference and how it works differently between </p>
  
  <ul>
  <li>runs Enter.class and types several inputs from console.</li>
  <li>runs Enter.class using Process.exec() and write char array as input to subprocess's output stream.</li>
  </ul>
  
  <p>Why scanner.nextInt() at Enter.java throws NoSuchElementException or not?</p></li>
  <li><p>How can I modify OnlineJudge to Enter.java works same.</p></li>
  </ol>