简体   繁体   English

从Java代码运行XJC

[英]Running xjc from java code

I am using xjc to generate classes from xsd. 我正在使用xjc从xsd生成类。 The generation has to happen inside the java code. 生成必须在Java代码内部进行。 Right now I have done it like this: 现在,我已经这样做了:

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

The output for the above program is: 上面程序的输出是:

waiting...

I have to use the classes after they are generated. 它们生成后,我必须使用这些类。 The problem here is that the subprocess never exits and the control is never back to the java program! 这里的问题是,子进程永远不会退出,控件永远不会回到Java程序!
Is there a way to do this without getRuntime().exec() ? 没有getRuntime().exec()有没有办法做到这一点?

You can actually use the driver class ( com.sun.tools.xjc.Driver ) behind the command line tool. 您实际上可以在命令行工具后面使用驱动程序类( com.sun.tools.xjc.Driver )。 This worked for me: 这对我有用:

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

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM