fixed JPMS bug

This commit is contained in:
2022-07-01 04:36:02 +08:00
parent 2b8e150949
commit 2516139a31
14 changed files with 166 additions and 19 deletions

View File

@@ -1,6 +1,5 @@
module net.woggioni.envelope {
requires java.logging;
requires static lombok;
requires net.woggioni.envelope.loader;
requires java.instrument;
}

View File

@@ -19,6 +19,7 @@ import java.net.URLClassLoader;
import lombok.SneakyThrows;
import net.woggioni.envelope.Constants;
import net.woggioni.envelope.loader.ModuleClassLoader;
import net.woggioni.envelope.loader.JarFileModuleFinder;
import net.woggioni.envelope.loader.JarFile;
@@ -37,6 +38,14 @@ class MainRunner {
List<JarFile> classpath,
Consumer<Class<?>> runner) {
if(mainModuleName == null) {
if(mainClassName == null) {
throw new RuntimeException(
String.format(
"Missing main attribute '%s' from manifest",
Constants.ManifestAttributes.MAIN_CLASS
)
);
}
URL[] urls = classpath.stream().map(Launcher::getURL).toArray(URL[]::new);
try (URLClassLoader cl = new URLClassLoader(urls, ClassLoader.getSystemClassLoader().getParent())) {
Thread.currentThread().setContextClassLoader(cl);
@@ -67,6 +76,7 @@ class MainRunner {
ModuleLayer layer = controller.layer();
Module mainModule = layer.findModule(mainModuleName).orElseThrow(
() -> new IllegalStateException(String.format("Main module '%s' not found", mainModuleName)));
Optional<String> mainClassOpt = Optional.ofNullable(mainClassName);
runner.accept(Optional.ofNullable(mainClassName)
.or(() -> mainModule.getDescriptor().mainClass())
.map(className -> Class.forName(mainModule, className))