Skip to content

JavaKit: check each classPath element exists before starting VM #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/JavaKit/JavaKitVM/JavaVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup that's the way, thank you


typealias JavaVMPointer = UnsafeMutablePointer<JavaVM?>

public final class JavaVirtualMachine: @unchecked Sendable {
Expand Down Expand Up @@ -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)")
}
Expand Down Expand Up @@ -268,4 +280,8 @@ extension JavaVirtualMachine {
}
}
}

enum JavaKitError: Error {
case classPathEntryNotFound(entry: String, classPath: [String])
}
}