Add path for native library at runtime in Java regarding non modular and modular projects

330 views Asked by At

I have two projects using Java 11 from AdoptOpenJDK with ant for building purposes. Both using the same code base except of namings.

The first one is a non modular project with the code in the unnamed module. This one runs without Exception and adds the library as expected.

This is the code of the main class:

package nonmodularsampleproject;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 *
 * @author Robert
 */
public class NonModularSampleProject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String installationDirectory = System.getProperty("user.dir");
        String pathToAdd = installationDirectory + "\\" + "natives" + "\\" + "displays";
        try {
            addLibraryPath(pathToAdd);
        } catch (Exception ex) {
            System.err.println("Error while add the Native-Library-Path to the System.");
        }

        System.loadLibrary("displays");        
    }
    
    private static void addLibraryPath(String pathToAdd) throws Exception { 
        Module baseModule = ClassLoader.class.getModule(), unnamedModule = NonModularSampleProject.class.getModule();
        System.out.println("ClassLoader.class.getModule() "  + ClassLoader.class.getModule());
        System.out.println("ModularSampleProject.class.getModule() "  + NonModularSampleProject.class.getModule());
        System.out.println("module names: " + baseModule.getName() + " " + unnamedModule.getName());
        
        try {               
            baseModule.addOpens("java.lang", unnamedModule);
        } catch (IllegalCallerException ex) {
            System.err.println("IllegalCallerException " + ex);
        }
        
        final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
        usrPathsField.setAccessible(true);

        //get array of paths
        final String[] paths = (String[]) usrPathsField.get(null);

        //check if the path to add is already present
        for (String path : paths) {
            if (path.equals(pathToAdd)) {
                return;
            }
        }

        //add the new path
        final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
        newPaths[newPaths.length - 1] = pathToAdd;
        usrPathsField.set(null, newPaths);
        
    }        
    
}

The second one is a modular project with the code in a named module. This one throws an Exception. Adding the library fails.

This is the code of the module info:

module ModularSampleProject {
    requires java.base;
}

This is the code of the main class:

package modularsampleproject;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 *
 * @author Robert
 */
public class ModularSampleProject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String installationDirectory = System.getProperty("user.dir");
        String pathToAdd = installationDirectory + "\\" + "natives" + "\\" + "displays";
        try {
            addLibraryPath(pathToAdd);
        } catch (Exception ex) {
            System.err.println("Error while add the Native-Library-Path to the System.");
        }

        System.loadLibrary("displays");        
    }
    
    private static void addLibraryPath(String pathToAdd) throws Exception { 
        Module baseModule = ClassLoader.class.getModule(), namedModule = ModularSampleProject.class.getModule();
        System.out.println("ClassLoader.class.getModule() "  + ClassLoader.class.getModule());
        System.out.println("ModularSampleProject.class.getModule() "  + ModularSampleProject.class.getModule());
        System.out.println("Module names: " + baseModule.getName() + " " + namedModule.getName());
        
        try {               
            baseModule.addOpens("java.lang", namedModule);
        } catch (IllegalCallerException ex) {
            System.err.println("IllegalCallerException " + ex);
        }
        
        final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
        usrPathsField.setAccessible(true);

        //get array of paths
        final String[] paths = (String[]) usrPathsField.get(null);

        //check if the path to add is already present
        for (String path : paths) {
            if (path.equals(pathToAdd)) {
                return;
            }
        }

        //add the new path
        final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
        newPaths[newPaths.length - 1] = pathToAdd;
        usrPathsField.set(null, newPaths);
    }    
    
}

These two other great posts on stackoverflow lead to this approach.
Adding new paths for native libraries at runtime in Java
How do I set the java.library.path dynamically with java 13+?

This post helped also a lot.
http://fahdshariff.blogspot.de/2011/08/changing-java-library-path-at-runtime.html

In the modular project the call baseModule.addOpens("java.lang", namedModule) throws an IllegalCallerException, whereas in the non modular project everything works smooth.

This is the exception stack trace:

SCHWERWIEGEND: Error while open the package java.lang of the module java.base to the module ModularSampleProject
java.lang.IllegalCallerException: java.lang is not open to module ModularSampleProject
    at java.base/java.lang.Module.addOpens(Module.java:762)
    at ModularSampleProject/modularsampleproject.ModularSampleProject.addLibraryPath(ModularSampleProject.java:44)
    at ModularSampleProject/modularsampleproject.ModularSampleProject.main(ModularSampleProject.java:28)

What is wrong with the modular project?

0

There are 0 answers