2016-06-12 9 views
0

Ich versuche zu kompilieren und dann ein Programm über die Befehlszeile in Windows ausführen.Nicht möglich, Ausgabe über Java-Laufzeitprozess zu sehen

Ich habe den folgenden Code, ich habe versucht, die Gewinde-Ansatz, den ich kommentiert und dann den einzelnen Prozess Ansatz. Die Klassendatei wird erzeugt und hoffentlich erzeugt die Ausführung auch Hallo Welt. Wie erfasse ich das in einer Zeichenfolge? Beachten Sie, das ist eine Windows-Maschine, auf der ich bin.

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 

public class CompilerMain { 

    public String doCompile(String compilationLang, String codeString, 
          String challengeName) throws InterruptedException, IOException { 

    String output = null; 
    String commandString = null; 

    if (compilationLang == "java") { 

     String path = "c:\\Test\\"; 

     challengeName = "Solution"; 
     String commandStringCompile; 
     String commandStringExcecute; 
     commandStringCompile = "javac " + path + challengeName + ".java"; 
     commandStringExcecute = "java " + path + challengeName; 
     System.out.println("Command is " + commandStringCompile); 
     System.out.println("Command is " + commandStringExcecute); 
     final Process p = Runtime.getRuntime().exec(commandStringCompile); 
     final Process q = Runtime.getRuntime().exec(commandStringExcecute); 
     Scanner err = new Scanner(p.getInputStream()); 
     Scanner in = new Scanner(q.getInputStream()); 


     while (in.hasNext()) { 

     System.out.println(in.next()); 

     } 

     while (err.hasNext()) { 

     System.out.println(err.next()); 

     } 

     BufferedReader input = new BufferedReader(
      new InputStreamReader(p.getInputStream())); 
     String line = null; 

     try { 
     while ((line = input.readLine()) != null) { 
      System.out.println(line); 
     } 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 


     /*   
     new Thread(new Runnable() { 
      public void run() { 

       BufferedReader input = new BufferedReader(
         new InputStreamReader(p.getInputStream())); 
       String line = null; 

       try { 
        while ((line = input.readLine()) != null) { 
         System.out.println(line); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 

     p.waitFor(); 
     if(p.exitValue()==0){ 
      System.out.println("Error! Code " + p.exitValue()); 
      BufferedReader input = new BufferedReader(
        new InputStreamReader(p.getInputStream())); 
      String line = null; 

      try { 
       while ((line = input.readLine()) != null) { 
        System.out.println(line); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if(p.exitValue()!=0){ 
      BufferedReader input = new BufferedReader(
        new InputStreamReader(p.getInputStream())); 
      String line = null; 

      try { 
       while ((line = input.readLine()) != null) { 
        System.out.println(line); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     final Process q = Runtime.getRuntime().exec(commandString); 
     commandString = "java "+ challengeName + " >>out.txt"; 
     new Thread(new Runnable() { 
      public void run() { 

       BufferedReader input = new BufferedReader(
         new InputStreamReader(q.getInputStream())); 
       String line = null; 

       try { 
        while ((line = input.readLine()) != null) { 
      //   System.out.println(" 1"); 

         System.out.println(line); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 

     q.waitFor(); 
     System.out.println("Exit Value is " + q.exitValue()); 
    */ 
    } 
    return output; 
    } 
} 

Antwort

1

Ihr Ansatz funktioniert gut. Es ist wahrscheinlich Ihre Befehlszeichenfolge ist nicht korrekt. Bitte versuchen Sie zu lesen von p.getErrorStream() und q.getErrorStream(), Sie werden sehen, was vor sich geht.

Ich ändere Ihren Code ein wenig, um auf meinem Linux-Rechner zu arbeiten.

public String doCompile(String compilationLang, String codeString, 
         String challengeName) throws InterruptedException, IOException { 

    String output = null; 
    String commandString = null; 

    if (compilationLang == "java") { 

    String path = "/home/john/IdeaProjects/Test1/src/"; 

    String commandStringCompile; 
    String commandStringExcecute; 
    commandStringCompile = "javac " + path + "Main.java"; 
    commandStringExcecute = "java -cp " + path +" Main"; 
    System.out.println("Command is " + commandStringCompile); 
    System.out.println("Command is " + commandStringExcecute); 
    final Process p = Runtime.getRuntime().exec(commandStringCompile); 
    final Process q = Runtime.getRuntime().exec(commandStringExcecute); 
    Scanner err = new Scanner(p.getErrorStream()); 
    Scanner in = new Scanner(p.getInputStream()); 
    while (in.hasNext()) { 
     System.out.println(in.next()); 
    } 
    while (err.hasNext()) { 
     System.out.println(err.next()); 
    } 

    err = new Scanner(q.getErrorStream()); 
    in = new Scanner(q.getInputStream()); 
    while (in.hasNext()) { 
     System.out.println(in.next()); 
    } 
    while (err.hasNext()) { 
     System.out.println(err.next()); 
    } 
    } 
    return output; 
} 

Der Ausgang:

Command is javac /home/john/IdeaProjects/Test1/src/Main.java 
Command is java -cp /home/john/IdeaProjects/Test1/src/ Main 
hello 
world! 
+0

Ich habe nicht in der Lage gewesen, um zu überprüfen, ich den Fehlerstrom zu sehen in Ordnung, aber nie den Ausgangsstrom, vielleicht ist dies ein Fenster Sache. Lass mich das auf einer Linux-Plattform ausprobieren. – Sameer

Verwandte Themen