Skip to content

Instantly share code, notes, and snippets.

@tomsontom
Created October 24, 2018 07:54
Show Gist options
  • Save tomsontom/d734cf74fd5c2a87be459bb5940bbfac to your computer and use it in GitHub Desktop.
Save tomsontom/d734cf74fd5c2a87be459bb5940bbfac to your computer and use it in GitHub Desktop.
Simple creation of new Java-Module-System-Layer
package javamodules;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SimpleLayerCreation {
private static Path JAVAFX_MODULES = Paths.get("/Users/tomschindl/Downloads/javafx-sdk-11/lib");
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Throwable {
Path[] array = Files.list(JAVAFX_MODULES) //
.filter(p -> {
String name = p.getFileName().toString();
return name.startsWith("javafx.") && name.endsWith(".jar");
}) //
.toArray(i -> new Path[i]);
Set<String> modules = Stream.of(array) //
.map(Path::getFileName) //
.map(Path::toString) //
.map(n -> n.substring(0, n.length() - ".jar".length())) //
.collect(Collectors.toSet());
URL[] urls = Stream.of(array).map( Path::toFile).map( f -> {
try {
return f.toURL();
} catch(Throwable t) {
return null;
}
} ).toArray( i -> new URL[i]);
URLClassLoader c = new URLClassLoader(urls, FeatureRichLayerCreation.class.getClassLoader());
ModuleFinder fxModuleFinder = ModuleFinder.of(array);
ModuleFinder empty = ModuleFinder.of(new Path[0]);
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration configuration = bootLayer.configuration();
Configuration newConfiguration = configuration.resolve(fxModuleFinder, empty, modules);
ModuleLayer moduleLayer = bootLayer.defineModulesWithManyLoaders(newConfiguration, FeatureRichLayerCreation.class.getClassLoader());
ClassLoader loader = moduleLayer.findLoader("javafx.base");
System.err.println(loader.loadClass("javafx.beans.property.SimpleStringProperty").newInstance());
System.err.println(loader.loadClass("com.sun.javafx.runtime.VersionInfo").newInstance());
c.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment