swiftlang / swiftlang/swift-java

Undefined symbol: _JNI_CreateJavaVM & _JNI_GetCreatedJavaVMs during building Xcode project

Open
#550 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
1.2k
Forks
123
Avg merge
1d 7h
Merged PRs (30d)
16

Description

Commit hash: d087f65caa3dfb1f0582568ce6e2ee0cf69ed717

Context:

I have built simple Swift package, that contains jar file and uses SwiftJava to get artifacts of Java types that were declared in that .jar file, to use them inside the library. I have provided swift-java.config file for desired types.

I used swift build command for the package. Which resulted in successful building and corresponding Swift's representation of Java types were generated in .build directory of my package. So, from my understanding I have set everything up correctly, otherwise those files wouldn't even be generated.

But, once I added this local package to Xcode macOS project I keep getting error from SwiftJava package, which prevents my project from building:

Undefined symbol: _JNI_CreateJavaVM
Undefined symbol: _JNI_GetCreatedJavaVMs

Undefined symbols for architecture arm64:
  "_JNI_CreateJavaVM", referenced from:
      SwiftJava.JavaVirtualMachine.(in _6C47C84FF2986D473D4896464216BF8F).init(classpath: [Swift.String], vmOptions: [Swift.String], ignoreUnrecognized: Swift.Bool) throws -> SwiftJava.JavaVirtualMachine in SwiftJava.o
  "_JNI_GetCreatedJavaVMs", referenced from:
      closure #2 @Sendable (inout SwiftJava.JavaVirtualMachine?) throws -> SwiftJava.JavaVirtualMachine in static SwiftJava.JavaVirtualMachine.shared(classpath: [Swift.String], vmOptions: [Swift.String], ignoreUnrecognized: Swift.Bool, replace: Swift.Bool) throws -> SwiftJava.JavaVirtualMachine in SwiftJava.o
ld: symbol(s) not found for architecture arm64

I have tried to specify path for linker in my Package.swift file, in order to find correct libjvm.dylib file, but that doesn't seem to be related to this, since SwiftJava is complaining.

Package.swift content of my package I'm adding to my project

// swift-tools-version: 6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import CompilerPluginSupport
import PackageDescription
import class Foundation.FileManager
import class Foundation.ProcessInfo

func findJavaHome() -> String {
    if let home: String = ProcessInfo.processInfo.environment["JAVA_HOME"] {
        return home
    }
    
    let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home"
    
    if let home: String = try? String(contentsOfFile: path, encoding: .utf8) {
        if let lastChar = home.last, lastChar.isNewline {
            return String(home.dropLast())
        }
        
        return home
    }
    
    fatalError("Please set the JAVA_HOME environment variable to point where Java is installed: \(path)")
}

let java_home = findJavaHome()

let javaIncludePath = "\(java_home)/include"
let javaPlatformIncludePath = "\(javaIncludePath)/darwin"

let homebrewJavaPath = "/opt/homebrew/opt/openjdk/libexec/openjdk.jdk/Contents/Home"

let package = Package(
    name: "MyPackage",
    platforms: [.macOS(.v15)],
    products: [.library(name: "MyPackage", type: .dynamic, targets: ["MyPackage"])],
    dependencies: [
        .package(url: "https://github.com/swiftlang/swift-java", branch: "main")
    ],
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .target(
            name: "MyPackage",
            dependencies: [
                .product(name: "SwiftJava", package: "swift-java"),
                .product(name: "CSwiftJavaJNI", package: "swift-java"),
                .product(name: "SwiftJavaConfigurationShared", package: "swift-java"),
                .product(name: "SwiftRuntimeFunctions", package: "swift-java")
            ],
            exclude: ["swift-java.config"],
            swiftSettings: [
                .unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
                .swiftLanguageMode(.v5)
            ],
            linkerSettings: [
                    .unsafeFlags([
                        "-L\(homebrewJavaPath)/lib/server",
                        "-ljvm"
                    ])
                ],
            plugins: [
                .plugin(name: "JExtractSwiftPlugin", package: "swift-java"),
                .plugin(name: "SwiftJavaPlugin", package: "swift-java")
            ]
        ),
    ]
)

Probably some relevant info about system:

MacBook Air M1
Operating system: MacOS 26.2
swift-driver version: 1.127.14.1 Apple Swift version 6.2.1 (swiftlang-6.2.1.4.8 clang-1700.4.4.1)
Target: arm64-apple-macosx26.0

/usr/libexec/java_home -V:
Matching Java Virtual Machines (1):
25.0.2 (arm64) "Homebrew" - "OpenJDK 25.0.2" /opt/homebrew/Cellar/openjdk/25.0.2/libexec/openjdk.jdk/Contents/Home

java -version:
openjdk version "25.0.2" 2026-01-20
OpenJDK Runtime Environment Homebrew (build 25.0.2)
OpenJDK 64-Bit Server VM Homebrew (build 25.0.2, mixed mode, sharing)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported Package.swift linkerSettings and the SwiftJava and CSwiftJavaJNI products, then compare the successful swift build with the failing Xcode macOS build on arm64. Reproduce the undefined _JNI_CreateJavaVM and _JNI_GetCreatedJavaVMs errors using the supplied Java and toolchain details. Done means the Xcode project links the JNI symbols while retaining the generated Swift Java types.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, swift
Domain
build-system, developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.