2017-02-14 1 views
0

Ich möchte innerhalb von NetBeans + Java 8 ein Programm von innerhalb eines anderen mit Hilfe von Runtime.getRuntime ausführen() .exec (...). Es führt javac und javah, aber nicht java korrekt aus. Ihre Hilfe wird sehr geschätzt.Runtime.getRuntime(). Exec (...) läuft javac, javah aber nicht java unter Java 8 + NetBeans:

Dies ist das Programm, das ich

package Programs; 


public class AddTwoIntegers { 

    private static Integer addTwoIntegers(Integer a, Integer b) 
    { 
     return a + b; 
    } 


    public static void main(String[] args) 
    { 
     Integer a = 5; 
     Integer b = 8; 
     Integer result = addTwoIntegers(a, b); 
     System.out.println(a.toString() 
       .concat( " + ") 
       .concat( b.toString()) 
       .concat(" = ") 
       .concat( result.toString())); 
    } 

} 

Dies ist das Programm ausgeführt werden soll, die die vorherige Klasse laufen vorgibt:

/* 

This program is intended to run a java program from another java program 
It runs javac and javah but not java. 

*/ 
package Programs; 

import java.io.*; 

import java.util.Arrays; 

//import java.util.concurrent.TimeUnit; 
// java.util.concurrent.TimeUnit; 

public class JavacTest { 

    private static void printLines(String name, InputStream ins) throws Exception { 
     String line; 
     BufferedReader in = new BufferedReader(new InputStreamReader(ins)); 
     while ((line = in.readLine()) != null) { 
      System.out.println(name + " " + line); 
     } 
    } 

    private static void runProcess(String command) throws Exception { 
     Process pro = Runtime.getRuntime().exec(command); 
     printLines(command + " stdin:", pro.getInputStream()); 
     printLines(command + " stdout:", pro.getInputStream()); 
     printLines(command + " stderr:", pro.getErrorStream()); 
     //Take your time 
     pro.waitFor(); 
     System.out.println(command + " exitValue() " + pro.exitValue()); 
    } 

    private static void runProcess2(String [] command) throws Exception { 
     Process pro = Runtime.getRuntime().exec(command); 
     printLines(Arrays.toString(command) + " stdin:", pro.getInputStream()); 
     printLines(Arrays.toString(command) + " stdout:", pro.getInputStream()); 
     printLines(Arrays.toString(command) + " stderr:", pro.getErrorStream()); 
     //Take your time 
     pro.waitFor(); 
     System.out.println(Arrays.toString(command) + " exitValue() " + pro.exitValue()); 
    } 




    public static void main(String[] args) { 
     try { 
      System.out.println("\nCompiling with javac: .java -> .class"); 
      runProcess("/home/jose/jdk1.8.0_111/bin/javac  
/home/jose/NetBeansProjects/eJVolu17P/src/Programs/AddTwoIntegers.java"); 
      //take your time 
      //TimeUnit.SECONDS.sleep(1); 
      System.out.println("\n\nShowing the bytecode content of the .class file with javap"); 
      runProcess("/home/jose/jdk1.8.0_111/bin/javap -c 
/home/jose/NetBeansProjects/eJVolu17P/src/Programs/AddTwoIntegers.class"); 
      System.out.println("\n\nWorking Directory = " + 
       System.getProperty("user.dir")); 
      //TimeUnit.SECONDS.sleep(1); 
      System.out.println("\n\nRunning the .class file with java"); 

      //runProcess("/home/jose/jdk1.8.0_111/bin/java -cp  
/home/jose/NetBeansProjects/eJVolu17P/build/classes/Programs/AddTwoIntegers"); 
      //runProcess("/home/jose/jdk1.8.0_111/bin/java -cp  /build/classes/Programs/AddTwoIntegers"); 
      String[] Commands = new String[3]; 
      Commands[0] = "/home/jose/jdk1.8.0_111/bin/java"; 
      Commands[1] = "-classpath"; 
        Commands[2] = "/home/jose/NetBeansProjects/eJVolu17P/build/classes/Programs/AddTwoIntegers"; 
      runProcess2(Commands); 
     } catch (Exception e) { 
     } 
    } 
} 
+0

"Es läuft einwandfrei javac und javah aber nicht java." Wie können Sie sagen? Vielleicht solltest du die stderr-Ausgabe in deine Frage aufnehmen, damit wir mehr darüber wissen, was falsch läuft. – VGR

+0

Sie sollten das Programm von etwas wie /home/jose/jdk1.8.0_111/bin/java -cp/home/jose/NetBeansProjects/eJVolu17P/build/classes/Programs.AddTwoIntegers Versuchen Sie es und sehen, ob dies funktioniert. – Ayusman

Antwort

1

Sie sollten das Programm durch so etwas wie /home laufen /jose/jdk1.8.0_111/bin/java -cp/home/jose/NetBeansProjekte/eJVolu17P/build/classes/Programs.AddTwoIntegers Probieren Sie es aus und sehen, ob das funktioniert.

Der Klassenpfad sollte auf den übergeordneten Ordner des Namens der package.Java-Klasse verweisen. Überprüfen Sie, ob das funktioniert.

+0

Richtig! Die Ausgabe des aufgerufenen Programms erscheint in stdin. Danke vielmals. – joser

0

Ich hatte ein Missverständnis: das Argument .exec (java ...) hatte 3 Tokens, aber nachdem ich über die von Ayusman vorgeschlagene Lösung nachgedacht hatte, wurde klar, dass es aus 4 Token besteht, so wie es in angegeben ist die folgende endgültige Lösung:

import java.io.*; 
import java.util.Arrays; 



//import java.util.concurrent.TimeUnit; 


public class JavacTest { 

    private static void printLines(String name, InputStream ins) 
      throws Exception { 
     String line; 
     BufferedReader in 
       = new BufferedReader(new InputStreamReader(ins)); 
     while ((line = in.readLine()) != null) { 
      System.out.println(name + " " + line); 
     } 
    } 

    private static void runProcess(String command) 
      throws Exception { 
     Process pro = Runtime.getRuntime().exec(command); 
     printLines(command 
       + " stdin:", pro.getInputStream()); 
     printLines(command 
       + " stderr:", pro.getErrorStream()); 
     //Take your time 
     pro.waitFor(); 
     System.out.println(command 
       + " exitValue() " + pro.exitValue()); 
    } 

    private static void runProcess2(String [] command) 
      throws Exception { 
     Process pro = Runtime.getRuntime().exec(command); 
     printLines(Arrays.toString(command) 
       + " stdin:", pro.getInputStream()); 
     printLines(Arrays.toString(command) 
       + " stderr:", pro.getErrorStream()); 
     //Take your time 
     pro.waitFor(); 
     System.out.println(Arrays.toString(command) 
       + " exitValue() " + pro.exitValue()); 
    } 



    /* 
    Note about paths: 


    When NetBeans runs a .java program, the compiled class .class is put in a 
    subfolder of build in the same folder of the project: 
    build/classes/Programs. 
    When a .java program is run with javac from inside another master 
    .java program, the corresponding .class file is put in the 
    same folder as the master .java program. 
    */ 

    public static void main(String[] args) { 
     try { 
      System.out.println("\nCompiling with javac: .java -> .class"); 
      runProcess("/home/jose/jdk1.8.0_101/bin/javac " 
        + " /home/jose/NetBeansProjects/eJVolu17P/src" 
        + "/Programs/AddTwoIntegers.java"); 
      //take your time 
      //TimeUnit.SECONDS.sleep(1); 


      System.out.println("\n\nShowing the bytecode " 
        + "content of the .class file with javap"); 
      runProcess("/home/jose/jdk1.8.0_101/bin/javap " 
        + "-c /home/jose/NetBeansProjects/eJVolu17P/src" 
        + "/Programs/AddTwoIntegers.class"); 
      System.out.println("\n\nWorking Directory = " + 
       System.getProperty("user.dir")); 
      //TimeUnit.SECONDS.sleep(1); 


      System.out.println("\n\nRunning the .class file with java"); 
      String[] Commands = new String[4]; 
      //Path to java 
      Commands[0] = "/home/jose/jdk1.8.0_101/bin/java"; 
      //Command: claspath 
      Commands[1] = "-cp"; 
      //Path to parent folder of package with the .java program = Programs 
      Commands[2] = "/home/jose/NetBeansProjects/eJVolu17P/src/"; 
      //Used package + name of compiled class without suffix .class 
      Commands[3] = "Programs.AddTwoIntegers"; 
      runProcess2(Commands); 
     } catch (Exception e) { 
     } 
    } 
} 
Verwandte Themen