2017-02-14 84 views
0

我想在Runtime.getRuntime的帮助下在另一个内部运行NetBeans + Java 8程序().exec(...)。它正确运行javac和javah,但不是java。您的帮助将不胜感激。Runtime.getRuntime()。exec(...)在Java 8下运行javac,javah,但不运行java 8 + NetBeans:

这是我要运行

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

} 

这是伪装成运行前面类节目的节目:

/* 

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

“它正确运行javac和javah而不是java。”你怎么知道?也许你应该在你的问题中包含stderr输出,以便我们更多地了解发生了什么问题。 – VGR

+0

你应该运行该程序,如 /home/jose/jdk1.8.0_111/bin/java -cp/home/jose/NetBeansProjects/eJVolu17P/build/classes/Programs.AddTwoIntegers 试试看看它是否有效。 – Ayusman

回答

1

你应该像 /家用运行程序/jose/jdk1.8.0_111/bin/java -cp/home/jose/NetBeansProjects/eJVolu17P/build/classes/Programs.AddTwoIntegers 试试看看它是否有效。

类路径应该指向package.Java类名的父文件夹。检查是否有效。

+0

对!被调用程序的输出显示在stdin中。非常感谢。 – joser

0

我有一个误解:.exec(java ...)的参数有3个标记,但是在考虑了Ayusman提出的解决方案之后,很明显它包含4个标记,例如它在以下最终解决方案:

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) { 
     } 
    } 
} 
相关问题