Simple creation of new Java-Module-System-Layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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