Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use JEP from JVM, initialized from C++ native code #410

Closed
Daniel-Alievsky opened this issue Jun 7, 2022 · 4 comments
Closed

Cannot use JEP from JVM, initialized from C++ native code #410

Daniel-Alievsky opened this issue Jun 7, 2022 · 4 comments

Comments

@Daniel-Alievsky
Copy link

Daniel-Alievsky commented Jun 7, 2022

Hello! I successfully used JEP from usual Java environment, where JVM is called via "java.exe" (Windows-8).

But really I need to use it from little other environment: C++ server, which initializes JVM itself (JNI technology). And I see that my simplest test does not work.

This is my code:

    public static Object testJep() {
        final Interpreter context = new SharedInterpreter();
        context.exec("def test():\n    return 'Hello from JEP'\n");
        return context.invoke("test");
    }

This is the result:

java.lang.NullPointerException: Cannot invoke "java.lang.ClassLoader.getResourceAsStream(String)" because "<local6>" is null
    jep.ClassList.loadClassList(ClassList.java:282)
    jep.ClassList.<init>(ClassList.java:69)
    jep.ClassList.getInstance(ClassList.java:415)
    jep.Jep.setupJavaImportHook(Jep.java:202)
    jep.Jep.configureInterpreter(Jep.java:187)
    jep.SharedInterpreter.configureInterpreter(SharedInterpreter.java:64)
    jep.Jep.<init>(Jep.java:159)
    jep.SharedInterpreter.<init>(SharedInterpreter.java:56)
    com.siams.stare.extensions.tests.ExampleJep.testJep(ExampleJep.java:21)
    com.siams.stare.extensions.tests.ExampleJep.process(ExampleJep.java:17)
    net.algart.stare.execution.Executor.execute(Executor.java:429)

It seems to be your bug.
Below is your code, that I found in debugger:

            while (in == null && i < classloadersToTry.length) {
                cl = classloadersToTry[i];
                in = cl.getResourceAsStream(rsc);  // line 282!!!
                i++;
            }

You think that classloadersToTry contains only non-null values, but it is not so:

        ClassLoader[] classloadersToTry = new ClassLoader[] {
                Thread.currentThread().getContextClassLoader(),
                Jep.class.getClassLoader() };

When JVM is started not by normal java.exe, but via JNI, Thread.currentThread().getContextClassLoader() will be null by default.

Could you fix this?

@Daniel-Alievsky
Copy link
Author

I understand that there is workaround like the following:

class JepTools {
    static {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        if (contextClassLoader == null) {
            Thread.currentThread().setContextClassLoader(JepTools.class.getClassLoader());
        }
    }
...
}

But you understand that this is a last resort and a highly undesirable solution for a generic library.

@bsteffensmeier
Copy link
Member

This is a duplicate of #401 and a fix has already been written and will be included in the next release of Jep.

In the meantime if it is unnacceptable to set a context classloader for your thread you could also set a custom ClassEnquirerer in your JepConfig that uses a different ClassLoader.

@Daniel-Alievsky
Copy link
Author

Ok, I prefer to wait for a new release. When, approximately, are you going to do this?

@bsteffensmeier
Copy link
Member

I have added a Roadmap to the wiki with our release plans. Next release will come out around October 2022.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants