RobotRock
RobotRock

Reputation: 4459

Options with JavaCompiler API

I have a program like this: it compiles files in memory and then executes them from memory. Therefore I needed a custom classloader with a file manager that stores files in memo://. Now I want to pass parameters to the compilers outputted class, since I think it does not inherit them (-Xmx80M, -Djava.library.path etc.). I would think I would need the -J option for this, however the compiler returns an IllegalArgumentException. com.sun.tools.javac.main.RecognizedOptions.getJavacToolOptions(null) doesn't list -J either, so I'm thinking I'm trying to put the argument in the wrong place. Any experience with where I should use -J (or an other option for that matter)?

Edit: com.sun.tools.javac.main.RecognizedOptions.getAll(null) reports -J as an option, howevergetJavacToolOptions(null) does not, and neither getJavacFileManagerOptions(null).

To clarify, I want to use the LWJGL library with the (runtime)compiled code. LWJGL requires some native libraries from the -Djava.library.path, which is set for the project. However the compiled code is not able to find this library path. I'm thinking it does not inherit this library path, and therefor LWJGL throws a NoClassDefFoundError. Else, it might interpret the relative library path wrong as memo://lib/lwjgl, but I have no way to check.

Stack:

Aug 22, 2011 2:14:58 PM customcompile.CustomCompile$2 run
SEVERE: null
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at customcompile.CustomCompile$2.run(CustomCompile.java:90)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.lwjgl.Sys
    at org.lwjgl.opengl.Display.<clinit>(Display.java:111)
...

I also must note that libraries included by the project are loaded successfully, however the LWJGL library loads additional native libraries - which does not work I think.

Custom classloader: package customcompile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;

/**
 *
 * @author Kaj Toet
 */
class MemoryClassLoader extends ClassLoader {


    private JavaCompiler compiler;
    private final MemoryFileManager manager;

    public MemoryClassLoader(JavaCompiler compiler, String classname, String filecontent) {
        this(compiler, Collections.singletonMap(classname, filecontent));
    }

    public MemoryClassLoader(JavaCompiler compiler, Map<String, String> map) {
            this.compiler=compiler;

            DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

            manager  = new MemoryFileManager(this.compiler);
            List<Source> list = new ArrayList<Source>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                list.add(new Source(entry.getKey(), Kind.SOURCE, entry.getValue()));
            }            

            List<String> optionList = new ArrayList<String>();
            // set compiler's classpath to be same as the runtime's
            //optionList.addAll(Arrays.asList("-cp", ".."));

            this.compiler.getTask(null, this.manager, diagnostics, optionList, null, list).call();
            for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
              CustomCompile.addDebugText(diagnostic.toString());
            }
    }
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        synchronized (this.manager) {
            Output mc = this.manager.map.remove(name);
            if (mc != null) {
                byte[] array = mc.toByteArray();
                return defineClass(name, array, 0, array.length);
            }
        }
        return super.findClass(name);
    }
}

Custom filemanager:

class MemoryFileManager extends ForwardingJavaFileManager<JavaFileManager> {
    public final Map<String, Output> map = new HashMap<String, Output>();

    MemoryFileManager(JavaCompiler compiler) {
        super(compiler.getStandardFileManager(null, null, null));
    }

    @Override
    public Output getJavaFileForOutput
            (Location location, String name, Kind kind, FileObject source) {
        Output mc = new Output(name, kind);
        this.map.put(name, mc);
        return mc;
    }  
}

Output:

class Output extends SimpleJavaFileObject {
    private final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Output(String name, Kind kind) {
        super(URI.create("memo:///" + name.replace('.', '/') + kind.extension), kind);
    }

    byte[] toByteArray() {
        return this.baos.toByteArray();
    }

    @Override
    public ByteArrayOutputStream openOutputStream() {
        return this.baos;
    }
}

Upvotes: 1

Views: 1754

Answers (2)

J-16 SDiZ
J-16 SDiZ

Reputation: 26920

Do you know which file (dll) do the class use? If you do, try working around the problem by calling System.loadLibrary manually before you do your compile.

Upvotes: 2

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74800

If you run the compiler API, the compiler will run in the same VM as your main program, sharing its memory with it.

The classes which you are compiling don't have any such settings - these are settings for the Java VM, not for any classes. If you want to load these classes later in your main program, too, they will also share the memory with your main program.

So, I don't see any sense in using these arguments.

Upvotes: 3

Related Questions