【问题标题】:Running xjc from java code从 java 代码运行 xjc
【发布时间】:2013-06-23 03:03:43
【问题描述】:

我正在使用 xjc 从 xsd 生成类。生成必须发生在 java 代码中。现在我已经这样做了:

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() 有没有办法做到这一点?

【问题讨论】:

    标签: java jaxb operating-system xjc


    【解决方案1】:

    您实际上可以使用命令行工具后面的驱动程序类(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();
                }
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

      • 我也试过这个。问题是它也永远不会退出!那是因为这个过程永远不会结束!
      猜你喜欢
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2021-07-16
      相关资源
      最近更新 更多