如何使用jci编译java类?
有人我使用这个:
org.apache.commons.jci.compilers.JavacJavaCompiler
我有一个 jci 类:
public void compile() {
sourceDir = new File("java");
sources = new String[] { "test/HelloWorld.java" };
target = new File("result");
JavacJavaCompilerSettings settings = new JavacJavaCompilerSettings();
JavacJavaCompiler javacJavaCompiler = new JavacJavaCompiler(settings);
System.out.println("compile -> " + sources);
System.out.println("compile -> " + sourceDir);
System.out.println("compile -> " + target);
CompilationResult result = javacJavaCompiler.compile(sources,
new FileResourceReader(sourceDir),
new FileResourceStore(target));
for (CompilationProblem cp : result.getErrors()) {
System.out.println("compile -> " + cp.getStartLine());
System.out.println("compile -> " + cp.getMessage());
}
}
我的输出是:
compile -> 0
compile -> Failure executing javac, but could not parse the error: javac: file not found: test/HelloWorld.java
Usage: javac <options> <source files>
use -help for a list of possible options
一些想法,ty。
somebody I use this:
org.apache.commons.jci.compilers.JavacJavaCompiler
I have a jci class:
public void compile() {
sourceDir = new File("java");
sources = new String[] { "test/HelloWorld.java" };
target = new File("result");
JavacJavaCompilerSettings settings = new JavacJavaCompilerSettings();
JavacJavaCompiler javacJavaCompiler = new JavacJavaCompiler(settings);
System.out.println("compile -> " + sources);
System.out.println("compile -> " + sourceDir);
System.out.println("compile -> " + target);
CompilationResult result = javacJavaCompiler.compile(sources,
new FileResourceReader(sourceDir),
new FileResourceStore(target));
for (CompilationProblem cp : result.getErrors()) {
System.out.println("compile -> " + cp.getStartLine());
System.out.println("compile -> " + cp.getMessage());
}
}
and my output is:
compile -> 0
compile -> Failure executing javac, but could not parse the error: javac: file not found: test/HelloWorld.java
Usage: javac <options> <source files>
use -help for a list of possible options
some idea, ty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出清楚地告诉您编译器无法使用您提供的路径名找到源文件。我想到了两个可能的原因:
要尝试的事情:
System.getProperty("user.dir")
的值...该值应与 JVM 的当前目录匹配。在此基础上,找出源文件的相对路径名应该是什么。The output is telling you clearly that the compiler cannot find your source file with the pathname that you have supplied. Two possible causes spring to mind:
Things to try:
System.getProperty("user.dir")
... which should match the JVM's current directory. Based on that, figure out what the relative pathname of the source file should be.