By implementing JavaFileObject
yourself and "patching" JavaFileManager
you can keep the compiler in-memory, and then via exposing defineClass()
of ClassLoader
you can load the resulting byte array and launch main()
via reflection.
Here is a condensed variant, it actually does what you asked for, loads source code from file (that's one line with Files.readString()
anyway). However that's something what you can probably leave entirely to the patched JavaFileManager
, just I focused on keeping even the source code in memory, that's why there is the commented String src = "...";
block.
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import javax.lang.model.element.*;
import javax.tools.*;
public class SOCompileTestDense {
public static void main(String[] args) throws Exception {
String name="Test";
// String src="public class Test {\r\n" +
// " public static void main(String[] args) {\r\n" +
// " System.out.println(\"Hello \"+args[0]+\"!\");\r\n" +
// " }\r\n" +
// "}";
String src=Files.readString(Paths.get(name+".java"));
System.out.println(src);
List<JFO> sources=Arrays.asList(new JFO(name,src));
Map<String,JFO> files=new HashMap<>();
JavaCompiler jc=ToolProvider.getSystemJavaCompiler();
JavaCompiler.CompilationTask ct=jc.getTask(null, new JFM(jc.getStandardFileManager(null, null, null),files), null, null, null, sources);
System.out.println(ct.call());
Class<?> clazz=new CL().defineClass(name, files.get(name).baos.toByteArray());
clazz.getDeclaredMethod("main", String[].class).invoke(null, (Object)new String[] {"StackOverflow"});
}
static class JFO implements JavaFileObject {
final String name;
final String src;
JFO(String name,String src){
this.name=name;
this.src=src;
}
public URI toUri() {
try {
return new URI(name);
} catch(URISyntaxException use) {use.printStackTrace();};
return null;
}
public String getName() {
return name;
}
ByteArrayOutputStream baos;
public OutputStream openOutputStream() throws IOException {
baos=new ByteArrayOutputStream();
return baos;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return src;
}
public Kind getKind() {
return Kind.SOURCE;
}
public boolean isNameCompatible(String simpleName, Kind kind) {
return name.equals(simpleName);
}
public InputStream openInputStream() throws IOException {return null;}
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {return null;}
public Writer openWriter() throws IOException {return null;}
public long getLastModified() {return 0;}
public boolean delete() {return false;}
public NestingKind getNestingKind() {return null;}
public Modifier getAccessLevel() {return null;}
}
static class JFM implements JavaFileManager {
final JavaFileManager jfm;
final Map<String, JFO> files;
JFM(JavaFileManager jfm, Map<String, JFO> files){
this.jfm=jfm;
this.files=files;
}
public int isSupportedOption(String option) {
return jfm.isSupportedOption(option);
}
public ClassLoader getClassLoader(Location location) {
return jfm.getClassLoader(location);
}
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
return jfm.list(location, packageName, kinds, recurse);
}
public String inferBinaryName(Location location, JavaFileObject file) {
return jfm.inferBinaryName(location, file);
}
public boolean handleOption(String current, Iterator<String> remaining) {
return jfm.handleOption(current, remaining);
}
public boolean hasLocation(Location location) {
return jfm.hasLocation(location);
}
public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException {
return jfm.getJavaFileForInput(location, className, kind);
}
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
JFO jfo=new JFO(className,null);
files.put(className, jfo);
return jfo;
}
public String inferModuleName(Location location) throws IOException {
return jfm.inferModuleName(location);
}
public Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException {
return jfm.listLocationsForModules(location);
}
public boolean isSameFile(FileObject a, FileObject b) {return false;}
public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {return null;}
public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {return null;}
public void flush() throws IOException {}
public void close() throws IOException {}
}
static class CL extends ClassLoader {
public Class<?> defineClass(String name,byte array[]) {
return defineClass(name, array, 0, array.length);
}
}
}
And this is a tidy, short version, its output looks as follows:
public class Test {
public static void main(String[] args) {
System.out.println("Hello "+args[0]+"!");
}
}
true
Hello StackOverflow!
A much longer variant with verbose output is available on Ideone, https://ideone.com/DAXIlH, and if you download that one and remove the //
in front of various System.out.println();
calls, it will display a lot more stuff, which were simply too much for Ideone.
Practically what I did was implementing all methods as throw new Error();
(see code on Ideone), and gradually filled them out with something meaningful whenever they got actually called. Actually it was pretty straigtforward, the only gotcha I ran into was a couple default
methods of the JavaFileManager
interface. As eclipse did not provide a stub implementation of those, it was a bit of surprise when code died without touching my throw new Error();
lines.
java
command. Is that relevant?