Calling NSWindow with JNI

Viewed 118

I tried call Swift function from Java using JNI. My Swift function creates NSWindow with some content, and it's work, but when I call it from Java I get the next message: "NSWindow drag regions should only be invalidated on the Main Thread! This will throw an exception in the future" and my app crashes

My swift function:

@_silgen_name("swiftHelloWorld")
public func swiftHelloWorld() {
    let delegate = AppDelegate()
    let application = NSApplication.shared
    application.setActivationPolicy(.regular)
    application.delegate = delegate
    application.run()
}

My Java code:

public class SwiftHelloWorld {

    static {
        System.loadLibrary("SwiftHelloWorld");
    }

    public static native void printHelloWorldImpl();

    public static void main(final String[] args) {
        printHelloWorldImpl();
    }
}

My C code:

#include <jni.h>
#include <stdio.h>
#include "helloworld_SwiftHelloWorld.h"
#include "helloworld_SwiftHelloWorld_swift.h"

JNIEXPORT void JNICALL Java_helloworld_SwiftHelloWorld_printHelloWorldImpl (JNIEnv *env, jclass clazz) {
    swiftHelloWorld();
    printf("%s", "Hello World from JNI!");
}

Swift header:

void swiftHelloWorld();
0 Answers
Related