From b08446a5dfc2925d3e8921b519de5a9312350255 Mon Sep 17 00:00:00 2001 From: Luke Howard Date: Mon, 4 Nov 2024 08:15:55 +1100 Subject: [PATCH] JavaKit: check each classPath element exists before starting VM Fixes #136. --- .../JavaKit/JavaKitVM/JavaVirtualMachine.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift b/Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift index 52882a41..9fb4db55 100644 --- a/Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift +++ b/Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift @@ -12,6 +12,12 @@ // //===----------------------------------------------------------------------===// +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + typealias JavaVMPointer = UnsafeMutablePointer public final class JavaVirtualMachine: @unchecked Sendable { @@ -54,6 +60,12 @@ public final class JavaVirtualMachine: @unchecked Sendable { // Construct the complete list of VM options. var allVMOptions: [String] = [] if !classPath.isEmpty { + let fileManager = FileManager.default + for path in classPath { + if !fileManager.fileExists(atPath: path) { + throw JavaKitError.classPathEntryNotFound(entry: path, classPath: classPath) + } + } let colonSeparatedClassPath = classPath.joined(separator: ":") allVMOptions.append("-Djava.class.path=\(colonSeparatedClassPath)") } @@ -268,4 +280,8 @@ extension JavaVirtualMachine { } } } + + enum JavaKitError: Error { + case classPathEntryNotFound(entry: String, classPath: [String]) + } }