英文:
JNativeHook Class not found
问题
以下是您提供的内容的翻译:
我需要一个在焦点在外的 GUI 组件中也能工作的 KeyListener,因此在进行了研究后,我找到了 JNativeHook 库。但是,当我下载了 ZIB 文件(从这里)并将 jnativehook-2.1.0.jar 添加到我的 Eclipse 项目(属性 -> Java Build Path -> Libraries -> Add External Jar)后,在运行测试程序时出现了错误消息。
>错误:找不到或加载主类 key.GlobalKeyListenerExample
>原因:java.lang.NoClassDefFoundError: org/jnativehook/keyboard/NativeKeyListener
这是我第一次使用外部 Jar 包,所以我想问一下我是否做错了什么,或者我需要做些什么来解决这个问题。
我的程序:
package key;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class GlobalKeyListenerExample implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("按下的键: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException e1) {
e1.printStackTrace();
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("释放的键: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("输入的键: " + e.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("注册本机钩子时出问题。");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new GlobalKeyListenerExample());
}
}
英文:
I needed a KeyListener which also works outside a focused GUI component, so after performing research I came across the JNativeHook library. But as soon as I downloadet the ZIB file (from here) and added the jnativehook-2.1.0.jar to my Eclipse project (Properties -> Java Build Path -> Libraries -> Add External Jar) a Error message occurs while running a testprogramm.
>Error: Could not find or load main class key.GlobalKeyListenerExample
>Caused by: java.lang.NoClassDefFoundError: org/jnativehook/keyboard/NativeKeyListener
This is the first time where I use a external Jar, so I wanted to ask if I did something wrong, or what I need to do to solve this Problem.
My Program:
package key;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class GlobalKeyListenerExample implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException e1) {
e1.printStackTrace();
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new GlobalKeyListenerExample());
}
}
答案1
得分: 2
我也遇到了同样的问题。
对我来说解决方案是将 JNativeHook.jar
放在 classpath
中,而不是 modulepath
。
英文:
I've experienced the same problem.
The solution for me was to put the JNativeHook.jar
in the classpath
, instead of the modulepath
.
答案2
得分: 0
右键单击您的项目 ==> 构建路径(在删除按钮下方) ==> 添加外部库
这对我起作用。
英文:
right click on your project ==> build path (under the delete button) ==> add external libraries
this worked for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论