kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.75k stars 348 forks source link

Caused by: java.lang.ClassNotFoundException: com.github.kwhat.jnativehook.NativeHookException #388

Closed Dqmino closed 2 years ago

Dqmino commented 2 years ago

Exception in thread "main" java.lang.NoClassDefFoundError: com/github/kwhat/jnativehook/NativeHookException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632) Caused by: java.lang.ClassNotFoundException: com.github.kwhat.jnativehook.NativeHookException at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ... 7 more ?? please help

kwhat commented 2 years ago

There is something wrong with your class loader? I have no idea what you did; none of this stack trace involves this library. The class com.github.kwhat.jnativehook.NativeHookException clearly exists.

Dqmino commented 2 years ago

I tried using maven and adding it as a dependency and didnt work, i tried building path and still didnt work

kwhat commented 2 years ago

run the jar manually from the command line. I don't understand how adding this library via mvn would produce that stack trace.

Dqmino commented 2 years ago

I ran it with the command line to get this, if i just open it i would get the same thing but in dialogues

kwhat commented 2 years ago
kwhat@plutonium244 /tmp $ wget https://repo1.maven.org/maven2/com/github/kwhat/jnativehook/2.2.1/jnativehook-2.2.1.jar
--2021-12-03 13:05:37--  https://repo1.maven.org/maven2/com/github/kwhat/jnativehook/2.2.1/jnativehook-2.2.1.jar
Resolving repo1.maven.org... 199.232.192.209, 199.232.196.209
Connecting to repo1.maven.org|199.232.192.209|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 665625 (650K) [application/java-archive]
Saving to: ‘jnativehook-2.2.1.jar’

jnativehook-2.2.1.jar                          100%[====================================================================================================>] 650.02K  --.-KB/s    in 0.07s   

2021-12-03 13:05:37 (9.18 MB/s) - ‘jnativehook-2.2.1.jar’ saved [665625/665625]

kwhat@plutonium244 /tmp $ java -jar jnativehook-2.2.1.jar 

JNativeHook: Global keyboard and mouse listeners for Java.
Copyright (C) 2006-2021 Alexander Barker.  All Rights Reserved.
https://github.com/kwhat/jnativehook/

JNativeHook is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JNativeHook is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
Dqmino commented 2 years ago

so uh idk what to do, how do i fix this

kwhat commented 2 years ago

It works for me so you need to better articulate what you are doing and how you are using this library that is causing breakage. I strongly suspect you are using some auto-magic tool that is screwing up your class path.

Dqmino commented 2 years ago

no im building path, im not even using maven to depend

YadavGulshan commented 2 years ago

Actually, I'm also facing the similar issue. The stack trace say's

Caused by: java.lang.ClassNotFoundException: com.github.kwhat.jnativehook.keyboard.NativeKeyListener
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 19 more

I'm using intellij Idea, and I think I have correctly stated the library to intellij.

link to project.

keylogger file link: file (Pardon me, for literally copying the whole example file :sweat_smile: )

@kwhat

kwhat commented 2 years ago

Works for me:

java -classpath lib/jnativehook-2.2.1.jar:out/artifacts/GodsEYE_jar/GodsEYE.jar com.bufferoverflow.Main -l

YadavGulshan commented 2 years ago

My bad, maybe my pc have some dependency issue.

I ran the command stated by you in terminal, I got an exception from LoggerFactory.

Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 11 more

Anyways can you suggest me what's wrong if you're free? Today is the submission day of my project, And I'm having hard time determining what's wrong with the dependencies. Libraries I'm using:

╰─>$ ls lib
javax.mail.jar         jnativehook-2.2.1-javadoc.jar  slf4j-api-1.7.25.jar          slf4j-api-1.7.25-sources.jar
jnativehook-2.2.1.jar  jnativehook-2.2.1-sources.jar  slf4j-api-1.7.25-javadoc.jar

Thanks in Advance.

kwhat commented 2 years ago

Add the rest of the jars to your classpath.

YadavGulshan commented 2 years ago

Thanks, I think you should close this issue. As this is due to some class path stuff.

By the way my program just exits without giving any error. do I need to install all the dependency listed for linux? (I know answer is obviously yes but still program doesn't exits with any error that states the issue. Why so?)

kwhat commented 2 years ago

my program just exits without giving any error.

Because there is nothing to keep it running. https://github.com/YadavGulshan/GodsEyE/blob/master/src/com/bufferoverflow/utils/KeyLogger.java#L40 doesn't block execution.

YadavGulshan commented 2 years ago

I know I'm acting like a fool or so, but trust me, I am unable to figure out what is wrong.

I checked the example of the native key listener and even copied the whole code, but the situation is still the same. Can you help me with that?

kwhat commented 2 years ago

So, you need to keep your program running somehow. Usually its some kind of run loop that does something. The simplest way to do that would be while(true); but that probably isn't the best option. I would try extending GlobalScreen and making the NativeHookThread a daemon thread. You can do that in the registerNativeHook() method when it is constructed.

public static void registerNativeHook() throws NativeHookException {
    super.registerNativeHook();
    hookThread.setDaemon(true);
}
YadavGulshan commented 2 years ago

At the start, I was unable to grasp what you were asking me to do, but after re-reading and some workarounds, my program suddenly started to work.

Thank you very much 😊