我试图在我的maven项目中使用jcodemodel生成java代码。我的maven项目有三个模块。我在我的一个模块中编写了一个示例jcodemodel,用于在执行purpose.But时测试它,在行中抛出错误。但是我创建了目录并检查了。我在简单的maven项目中检查了这个示例,它可以工作。但是当我在maven模块中给出它时,它会抛出错误。它在哪里检查构建文件?
codeModel.build(new File("src/main/java/check"));com.sun.codemodel.writer.FileCodeWriter.(FileCodeWriter.java:73)中的
java.io.IOException: src\main\java\check:不存在的目录
public class Consumer {
/**
* @param args
* @throws JClassAlreadyExistsException
* @throws IOException
* @throws JAXBException
*/
public static void main(String[] args) throws JClassAlreadyExistsException, IOException, JAXBException {
writeCodeModel("com.cts");
}
public static JType getTypeDetailsForCodeModel(JCodeModel jCodeModel, String type) {
if (type.equals("Unsigned32")) {
return jCodeModel.LONG;
} else if (type.equals("Unsigned64")) {
return jCodeModel.LONG;
} else if (type.equals("Integer32")) {
return jCodeModel.INT;
} else if (type.equals("Integer64")) {
return jCodeModel.LONG;
} else if (type.equals("Enumerated")) {
return jCodeModel.INT;
} else if (type.equals("Float32")) {
return jCodeModel.FLOAT;
} else if (type.equals("Float64")) {
return jCodeModel.DOUBLE;
} else {
return null;
}
}
// Function to generate CodeModel Class
public static void writeCodeModel(String factroyPackage) throws JAXBException {
try {
JCodeModel codeModel = new JCodeModel();
JDefinedClass foo = codeModel._class( "Foo" ); //Creates a new class
JMethod method = foo.method( JMod.PUBLIC, Void.TYPE, "doFoo" ); //Adds a method to the class
method.body()._return( JExpr.lit( 42 ) ); //the return statement
codeModel.build(new File("src/main/java/check"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}发布于 2012-05-14 11:28:25
异常信息似乎非常清楚:
com.sun.codemodel.writer.FileCodeWriter.(FileCodeWriter.java:73)上的
java.io.IOException: src\main\java\check:不存在的目录
要创建目标目录,您可以像这样修改代码,生成源的首选文件夹位于target/generated-sources/。
File target = new File("target/generated-sources/java");
if (!target.mkdirs()) {
throw new IOException("could not create directory");
}
codeModel.build(target);https://stackoverflow.com/questions/10581280
复制相似问题