Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:23:29+00:00 2026-05-25T00:23:29+00:00

I have a program like this: it compiles files in memory and then executes

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:23:31+00:00Added an answer on May 25, 2026 at 12:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that looks something like this: public partial class It {
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a piece of code like this: Class Program { static StreamReader sr
Lets say I have hierarchy like this (This is just a test program. Please
So I have this program that I really like, and it doesn't support Applescript.
Suppose I have an SConstruct file that looks like this: env = Environment() env.Program(a,
I have a ClickOnce environment like this: \\Fileserver\ClickOnceApps\App1.application C:\Documents and Settings\user\Start Menu\Programs\publisher\app1.appref-ms My understanding
I have a program that looks like the following: double[4][4] startMatrix; double[4][4] inverseMatrix; initialize(startMatrix)
I have a little console C# program like Class Program { static void main(string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.