kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.71k stars 342 forks source link

java.lang.UnsatisfiedLinkError: URI scheme is not "file" with javafx:jlink #394

Open srilakshmikanthanp opened 2 years ago

srilakshmikanthanp commented 2 years ago

Hello i am using this library with javafx running with mvn javafx:run works fine but with mvn javafx:jlink fails to start with a launcher, here is javafx plugin xml,

<plugin>
   <groupId>org.openjfx</groupId>
   <artifactId>javafx-maven-plugin</artifactId>
   <version>0.0.6</version>
   <executions>
      <execution>
         <id>default-cli</id>
         <configuration>
            <launcher>launcher</launcher>
            <mainClass>facsimile/com.github.srilakshmikanthanp.facsimile.Launcher</mainClass>
         </configuration>
      </execution>
   </executions>
</plugin>

and Here is stake trace,

java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.UnsatisfiedLinkError: URI scheme is not "file"
        at com.github.kwhat.jnativehook@2.2-SNAPSHOT/com.github.kwhat.jnativehook.GlobalScreen.<clinit>(GlobalScreen.java:91)        
        at facsimile@1.0.0/com.github.srilakshmikanthanp.facsimile.Launcher.start(Launcher.java:34)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
        ... 1 more
Exception running application com.github.srilakshmikanthanp.facsimile.Launcher
srilakshmikanthanp commented 2 years ago

See the thread on stackoverflow for more info about the error and temporary fix.

kwhat commented 2 years ago

You need to implement your own com.github.kwhat.jnativehook.NativeLibraryLocator for most situations. I've implemented the com.github.kwhat.jnativehook.DefaultLibraryLocator as a convince, but it doesn't work in all situations which is why NativeLibraryLocator exists. In your particular situation, the solution outlined on stackoverflow is the correct answer. I am probably just going to remove the NativeLibraryLocator functionality entirely and assume a developer using this library should understand what the java.library.path property is and how to use it.

kwhat commented 2 years ago

If you want me to look into this further, please provide a project (preferably Jetbrains Idea) where I can duplicate this issue.

srilakshmikanthanp commented 2 years ago

You can find the actual project that produces the error in github. Could you give docs about how to implement com.github.kwhat.jnativehook.NativeLibraryLocator ?

srilakshmikanthanp commented 2 years ago

Fixed with the following code

package com.github.srilakshmikanthanp.facsimile.locator;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import com.github.kwhat.jnativehook.NativeLibraryLocator;
import com.github.kwhat.jnativehook.NativeSystem;

/**
 * This class is used to locate the native libraries.
 */
public class JLibLocator implements NativeLibraryLocator {
    /**
     * This method is used to regsiter the Locator.
     */
    public static void setAaDefaultLocator() {
        System.setProperty("jnativehook.lib.locator", JLibLocator.class.getCanonicalName());
    }

    /**
     * Locates the native libraries.
     */
    @Override
    public Iterator<File> getLibraries() {
        var libs = new ArrayList<File>(1);
        var os = NativeSystem.getFamily().toString().toLowerCase();
        var arch = NativeSystem.getArchitecture().toString().toLowerCase();
        var jhome = System.getProperty("java.home");
        var libName = System.mapLibraryName("JNativeHook");
        var lib = jhome + File.separator + os + File.separator + arch + File.separator + libName;
        var libFile = new File(lib);

        libs.add(libFile);

        return libs.iterator();
    }
}

And make sure that you manually copied the dill files so image folder looks like

├───bin
│   └───server
├───conf
│   └───security
│       └───policy
│           ├───limited
│           └───unlimited
├───darwin
│   ├───arm64
│   └───x86_64
├───include
│   └───win32
├───legal
│   ├───java.base
│   ├───java.datatransfer
│   ├───java.desktop
│   ├───java.logging
│   ├───java.prefs
│   ├───java.xml
│   └───jdk.unsupported
├───lib
│   └───security
├───linux
│   ├───arm
│   ├───arm64
│   ├───x86
│   └───x86_64
└───windows
    ├───arm
    ├───x86
    └───x86_64
srilakshmikanthanp commented 2 years ago

can we close this issue ?