@@ -23,19 +23,41 @@ public final class JavaVirtualMachine: @unchecked Sendable {
23
23
/// The JNI environment for the JVM.
24
24
public let environment : JNIEnvironment
25
25
26
- public init ( vmOptions: [ String ] = [ ] ) throws {
26
+ /// Initialize a new Java virtual machine instance.
27
+ ///
28
+ /// - Parameters:
29
+ /// - classPath: The directories, JAR files, and ZIP files in which the JVM
30
+ /// should look to find classes. This maps to the VM option
31
+ /// `-Djava.class.path=`.
32
+ /// - vmOptions: Options that should be passed along to the JVM, which will
33
+ /// be prefixed by the class-path argument described above.
34
+ /// - ignoreUnrecognized: Whether the JVM should ignore any VM options it
35
+ /// does not recognize.
36
+ public init (
37
+ classPath: [ String ] = [ ] ,
38
+ vmOptions: [ String ] = [ ] ,
39
+ ignoreUnrecognized: Bool = true
40
+ ) throws {
27
41
var jvm : JavaVMPointer ? = nil
28
42
var environment : UnsafeMutableRawPointer ? = nil
29
43
var vmArgs = JavaVMInitArgs ( )
30
- vmArgs. version = JNI_VERSION_21
31
- vmArgs. ignoreUnrecognized = jboolean ( JNI_TRUE)
44
+ vmArgs. version = JNI_VERSION_1_6
45
+ vmArgs. ignoreUnrecognized = jboolean ( ignoreUnrecognized ? JNI_TRUE : JNI_FALSE)
46
+
47
+ // Construct the complete list of VM options.
48
+ var allVMOptions : [ String ] = [ ]
49
+ if !classPath. isEmpty {
50
+ let colonSeparatedClassPath = classPath. joined ( separator: " : " )
51
+ allVMOptions. append ( " -Djava.class.path= \( colonSeparatedClassPath) " )
52
+ }
53
+ allVMOptions. append ( contentsOf: vmOptions)
32
54
33
55
// Convert the options
34
- let optionsBuffer = UnsafeMutableBufferPointer< JavaVMOption> . allocate( capacity: vmOptions . count)
56
+ let optionsBuffer = UnsafeMutableBufferPointer< JavaVMOption> . allocate( capacity: allVMOptions . count)
35
57
defer {
36
58
optionsBuffer. deallocate ( )
37
59
}
38
- for (index, vmOption) in vmOptions . enumerated ( ) {
60
+ for (index, vmOption) in allVMOptions . enumerated ( ) {
39
61
let optionString = vmOption. utf8CString. withUnsafeBufferPointer { buffer in
40
62
let cString = UnsafeMutableBufferPointer< CChar> . allocate( capacity: buffer. count + 1 )
41
63
_ = cString. initialize ( from: buffer)
0 commit comments