use try-with-resources syntax in WorkerMain.java

This commit is contained in:
xuwei-k 2025-09-07 12:50:11 +09:00
parent f75241ff95
commit ee10ff7e23
1 changed files with 2 additions and 10 deletions

View File

@ -126,14 +126,11 @@ public final class WorkerMain {
throw new RuntimeException("missing jvmRunInfo element"); throw new RuntimeException("missing jvmRunInfo element");
} }
RunInfo.JvmRunInfo jvmRunInfo = info.jvmRunInfo; RunInfo.JvmRunInfo jvmRunInfo = info.jvmRunInfo;
URLClassLoader cl = createClassLoader(jvmRunInfo, ClassLoader.getSystemClassLoader()); try (URLClassLoader cl = createClassLoader(jvmRunInfo, ClassLoader.getSystemClassLoader())) {
try {
Class<?> mainClass = cl.loadClass(jvmRunInfo.mainClass); Class<?> mainClass = cl.loadClass(jvmRunInfo.mainClass);
Method mainMethod = mainClass.getMethod("main", String[].class); Method mainMethod = mainClass.getMethod("main", String[].class);
String[] mainArgs = jvmRunInfo.args.stream().toArray(String[]::new); String[] mainArgs = jvmRunInfo.args.stream().toArray(String[]::new);
mainMethod.invoke(null, (Object) mainArgs); mainMethod.invoke(null, (Object) mainArgs);
} finally {
cl.close();
} }
} else { } else {
throw new RuntimeException("only jvm is supported"); throw new RuntimeException("only jvm is supported");
@ -144,13 +141,8 @@ public final class WorkerMain {
if (info.jvm) { if (info.jvm) {
RunInfo.JvmRunInfo jvmRunInfo = info.jvmRunInfo; RunInfo.JvmRunInfo jvmRunInfo = info.jvmRunInfo;
ClassLoader parent = new ForkTestMain().getClass().getClassLoader(); ClassLoader parent = new ForkTestMain().getClass().getClassLoader();
ClassLoader cl = createClassLoader(jvmRunInfo, parent); try (URLClassLoader cl = createClassLoader(jvmRunInfo, parent)) {
try {
ForkTestMain.main(id, info, this.originalOut, cl); ForkTestMain.main(id, info, this.originalOut, cl);
} finally {
if (cl instanceof URLClassLoader) {
((URLClassLoader) cl).close();
}
} }
} else { } else {
throw new RuntimeException("only jvm is supported"); throw new RuntimeException("only jvm is supported");