我试图从XML文件中检索一些Java Bean的描述。
我想在项目@Data
中使用lombok
对它们进行注释,以自动包含构造函数,equals,hashCode,getters,setter和toString。
我想在内存中编译它们,生成一些实例(使用来自相同XML文件的数据)并将它们添加到Drools以最终对该数据做一些推理。
不幸的是,我无法编译这些课程,所以我在寻求你的帮助!
以下代码显示了如何以编程方式在内存中编译Java类:
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
public class Simple {
public static void main(String[] args) throws Exception {
String name = "Person";
String content = //
"public class " + name + " {\n" + //
" @Override\n" + //
" public String toString() {\n" + //
" return \"Hello, world!\";\n" + //
" }\n" + //
"}\n";
System.out.println(content);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileManager manager = new MemoryFileManager(compiler.getStandardFileManager(null, null, null));
List<String> options = new ArrayList<String>();
options.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")));
List<JavaFileObject> files = new ArrayList<JavaFileObject>();
files.add(new MemoryJavaFileObject(name, content));
compiler.getTask(null, manager, null, options, null, files).call();
Object instance = manager.getClassLoader(null).loadClass(name).newInstance();
System.out.println(instance);
}
}
其中MemoryFileManager
是:
package example;
import java.io.IOException;
import java.security.SecureClassLoader;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardJavaFileManager;
public class MemoryFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
private MemoryJavaClassObject object;
public MemoryFileManager(StandardJavaFileManager manager) {
super(manager);
}
@Override
public ClassLoader getClassLoader(Location location) {
return new SecureClassLoader() {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] b = object.getBytes();
return super.defineClass(name, object.getBytes(), 0, b.length);
}
};
}
@Override
public JavaFileObject getJavaFileForOutput(Location location, String name, Kind kind, FileObject sibling) throws IOException {
object = new MemoryJavaClassObject(name, kind);
return object;
}
}
和MemoryJavaClassObject
是:
package example;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import javax.tools.SimpleJavaFileObject;
public class MemoryJavaClassObject extends SimpleJavaFileObject {
protected final ByteArrayOutputStream stream = new ByteArrayOutputStream();
public MemoryJavaClassObject(String name, Kind kind) {
super(URI.create("string:///" + name.replace('.', '/') + kind.extension), kind);
}
public byte[] getBytes() {
return stream.toByteArray();
}
@Override
public OutputStream openOutputStream() throws IOException {
return stream;
}
}
最后MemoryJavaFileObject
是:
package example;
import java.net.URI;
import javax.tools.SimpleJavaFileObject;
public class MemoryJavaFileObject extends SimpleJavaFileObject {
private CharSequence content;
protected MemoryJavaFileObject(String className, CharSequence content) {
super(URI.create("string:///" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return content;
}
}
如果我在第一个代码块中运行该示例,我会得到以下输出,如预期的那样:
public class Person {
@Override
public String toString() {
return "Hello, world!";
}
}
Hello, world!
现在,如果我将lombok.jar
添加到我的项目中,并且包含以下示例:
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
public class Lombok {
public static void main(String[] args) throws Exception {
String name = "Person";
String content = //
"import lombok.Data;\n" + //
"public @Data class " + name + " {\n" + //
" private String name;\n" + //
"}\n";
System.out.println(content);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileManager manager = new MemoryFileManager(compiler.getStandardFileManager(null, null, null));
List<String> options = new ArrayList<String>();
options.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")));
List<JavaFileObject> files = new ArrayList<JavaFileObject>();
files.add(new MemoryJavaFileObject(name, content));
compiler.getTask(null, manager, null, options, null, files).call();
Object instance = manager.getClassLoader(null).loadClass(name).newInstance();
System.out.println(instance);
}
}
不幸的是,我没有得到预期的输出,而是:
import lombok.Data;
public @Data class Person {
private String name;
}
/Person.java:2: warning: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment
public @Data class Person {
^
at lombok.javac.apt.Processor.init(Processor.java:84)
at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)
at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:141)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:500)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1173)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:859)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
at example.Lombok.main(Lombok.java:42)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.processing.JavacProcessingEnvironment
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 15 more
1 warning
Person@39aeed2f
请注意,由于显示了典型输出,因此编译了类并执行了默认的toString()
方法。
另请注意,如果我运行前一个示例,现在我得到以下内容:
public class Person {
@Override
public String toString() {
return "Hello, world!";
}
}
/Person.java:1: warning: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment
public class Person {
^
at lombok.javac.apt.Processor.init(Processor.java:84)
at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)
at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:141)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:500)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1173)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:859)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
at example.Simple.main(Simple.java:44)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.processing.JavacProcessingEnvironment
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 15 more
1 warning
Hello, world!
显然,通过查看异常传递的警告消息,lombok
没有正确挂钩给定的编译器。不幸的是,我无法找到任何有用的信息。我只能认为lombok
无法正确使用Java JDK 8.我是对的吗?
你知道其他解决这个问题的方法吗?
答案 0 :(得分:7)
感谢Holger,我成功地解决了这个问题。
问题是由于类路径中缺少tools.jar
引起的。
这是因为Eclipse默认将Java环境识别为JRE而不是JDK。
最重要的是,Java JDK可能 - 或者可能不是,具体取决于您拥有的版本 - 具有tools.jar
文件。
如果你有Java 7或8,那么你应该在$JAVA_HOME/lib/tools.jar
中拥有这样的库。
如果您使用的是Java 6,则该文件不存在,但$JAVA_HOME/Classes/classes.jar
提供了相同的功能。
编译器是Java 6中添加的功能,因此如果您想使用它并且您拥有旧版本的Java,则应首先更新您的环境。
现在,有几种方法可以将tools.jar
(或classes.jar
)包含到项目的类路径中;因为我使用gradle,所以我决定将它作为依赖项引入,如下面的代码片段所示:
dependencies {
compile files("${System.properties['java.home']}/../lib/tools.jar")
compile 'org.projectlombok:lombok:1.14.4'
testCompile 'junit:junit:4.11'
}
希望这个小小的解释可以帮助其他人面临类似的问题!
干杯!