Open xdewx opened 2 years ago
it seems jshell not load any annotation processor (such as lombok and mapstruct, etc).
We can compile source code in shell, then import those bytecode class files:
edit java source code, eg TestVO:
package vo;
@lombok.Data
public class TestVO {
private String name;
private Integer age;
}
download lombok.jar(https://projectlombok.org/downloads/lombok.jar) to libs;
compile: cd vo; javac -cp libs/lombok.jar TestVO.java
;
add to classpath, eg: add vo parent folder to env's IJAVA_CLASSPATH(restart jupyter):
export IJAVA_CLASSPATH="/home/jupyter/notebooks/0-Java:$IJAVA_CLASSPATH"
;
import in jupyter cell, eg: import vo.*;
Very useful. But it seems a little troublesome. It will be great if ijava-kernel could compile and setup by itself. Thank u for your reply.
We could compile at runtime by ourselves.
eg:
// 1. compiler output, use System.err if null
StringWriter out = new StringWriter();
// 2. a diagnostic listener; if null use the compiler's default method for reporting diagnostics
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
// 3. a file manager; if null use the compiler's standard file manager
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
// 4. compiler options, null means no options
List<String> options = buildCompileOptions(compileOptions);
// 5. the compilation units to compile, null means no compilation units
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(sourceFile));
CompilationTask task = compiler.getTask(out, fileManager, diagnostics, options, null, compilationUnit);
Boolean isCompileSuccess = task.call();
fileManager.close();
I built a cell magic for compile, here is source code:
the getter and setter do not work! What shoud i do?