Undefined symbol: _JNI_CreateJavaVM & _JNI_GetCreatedJavaVMs during building Xcode project

Ouverte
#550 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
38/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
java, swift

Piste de recherche

Commencez par les linkerSettings signalés de Package.swift et les produits SwiftJava et CSwiftJavaJNI, puis comparez le swift build réussi avec la compilation Xcode macOS qui échoue sur arm64. Reproduisez les erreurs de symboles non définis _JNI_CreateJavaVM et _JNI_GetCreatedJavaVMs à l’aide des détails fournis concernant Java et la toolchain. La tâche est terminée lorsque le projet Xcode lie les symboles JNI tout en conservant les types Swift Java générés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

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)

Langage dominant
Swift
Étoiles
1.3k
Forks
127
Merge moyen
1 h 6 min
PR mergées (30 j)
3

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de swiftlang/swift-java

Toutes les issues de swiftlang/swift-java

Issues similaires

Plus d'issues Swift

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.