从java代码运行xjc
我使用xjc从xsd生成类。生成必须在java代码内部进行。现在我是这样做的:从java代码运行xjc,java,jaxb,operating-system,xjc,Java,Jaxb,Operating System,Xjc,我使用xjc从xsd生成类。生成必须在java代码内部进行。现在我是这样做的: Process child = Runtime.getRuntime().exec(command); try { System.out.println("waiting..."); child.waitFor(); System.out.println("waiting ended.."); } catch (I
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()
,有没有办法做到这一点?试试这个
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);
}
实际上,您可以在命令行工具后面使用驱动程序类(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();
}
});
}
}
我也试过这个。。问题是,它永远也不会从这个角度退出!!这是因为这个过程永远不会结束!