2013-06-26 215 views
1

我使用xjc從xsd生成類。代必須在java代碼內部發生。現在,我已經做了這樣的:從java代碼運行xjc

Process child = Runtime.getRuntime().exec(command); 
     try { 
      System.out.println("waiting..."); 
      child.waitFor(); 
      System.out.println("waiting ended.."); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
      return false; 
     } 

對上述程序的輸出是:

waiting... 

我生成後,他們使用的類。這裏的問題是子進程永不退出,並且控制永遠不會回到java程序!
有沒有辦法做到這一點沒有getRuntime().exec()

回答

1

試試這個

Process child = Runtime.getRuntime().exec(command); 
BufferedReader in = new BufferedReader( 
           new InputStreamReader(child.getInputStream())); 
      String line = null; 
      while ((line = in.readLine()) != null) { 
       System.out.println(line); 
      } 
+0

我也試過這也..問題是,它永遠不會退出此也!那是因爲這個過程永遠不會結束! – leet

+0

這對我來說效果很好 – Ilya

3

實際上,你可以使用驅動程序類(com.sun.tools.xjc.Driver)命令行工具落後。這對我有效:

import com.sun.tools.xjc.BadCommandLineException; 
import com.sun.tools.xjc.Driver; 
import com.sun.tools.xjc.XJCListener; 
import org.xml.sax.SAXParseException; 

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

public class Generator { 

    public static void main(String[] args) throws BadCommandLineException, IOException { 
     final String targetDir = "jaxb-files"; 
     Path path = Paths.get(targetDir); 
     if(!Files.exists(path)) { 
      Files.createDirectories(path); 
     } 
     Driver.run(new String[]{"-d", targetDir, 
       "D:\\dev\\onepoint\\tui\\java\\xsdjsonschema\\src\\main\\xsd\\test.xsd"}, new XJCListener() { 

      @Override 
      public void error(SAXParseException e) { 
       printError(e, "ERROR"); 
      } 

      @Override 
      public void fatalError(SAXParseException e) { 
       printError(e, "FATAL"); 
      } 

      @Override 
      public void warning(SAXParseException e) { 
       printError(e, "WARN"); 
      } 

      @Override 
      public void info(SAXParseException e) { 
       printError(e, "INFO"); 
      } 

      private void printError(SAXParseException e, String level) { 
       System.err.printf("%s: SAX Parse exception", level); 
       e.printStackTrace(); 
      } 
     }); 
    } 
}