1

What is the best way to load a .java file call the main method and execute the code. So for instance if I call C:/Test.java it should execute the code.

My code right now is the following:

final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
final Iterable<<? extends JavaFileObject>compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList("C:/test.java"));
final JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);

final boolean success = task.call();

try {
    System.out.println("Compiled: " + success);
    fileManager.close();
} catch (final IOException e) {
    e.printStackTrace();
}

But it also generate a .class file in the folder which I don't want. Is this code good for java 14 or there is a better way?

2

2 Answers 2

1

First you can simplify the code a bit. You use the StandardJavaFileManager and it imports the interface Auto/Closeable. So you can use the try-with-resource:

final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();

try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
    final Iterable<<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList("C:/test.java"));
    final JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);

    final boolean success = task.call();
} catch (IOException e) {
    e.printStackTrace();
}

Second you need the .class file to execute your java program. There exists a nice post. If you want to execute your test file and you use the command line, you should use the following commands:

javac Test.java
java Test

// or a little bit shorter
java Test.java

If you want to know more about compilation, I can recommend this post or this post.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.