简体   繁体   English

Java程序在注册侦听器后终止

[英]Java program terminates after registering Listeners

Hello people on the internet! 互联网上的大家好!

I have a problem with my code in java, I'm shure it's just a beginner error, but I can't quite get WHAT the problem is. 我的Java代码有问题,我确定这只是一个初学者的错误,但我无法完全理解问题所在。

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;

public class MultiThreadChatClient
{
private static Socket clientSocket = null;
private static PrintStream os = null;
private static boolean closed = false;

private static void start() 
{           
    int portNumber = 2222;
    String host = "localhost";

    System.out.println("Host=" + host + ", port=" + portNumber);

    try
    {
        clientSocket = new Socket(host, portNumber);
        os = new PrintStream(clientSocket.getOutputStream());
        os.println("KeyListener ID: " + new Random().nextInt());
    } 
    catch (UnknownHostException e) 
    {
        System.err.println("Finner ikke hosten " + host);
    }
    catch (IOException e) 
    {
        System.err.println("Error, fant ingen åpen server på " + host);
    }

    if ( clientSocket == null || os == null )
    {
        closed = true;
    }
    Listener.startListener();
}

public void stop()
{
    closed = true;
    os.close();
    try
    {
        clientSocket.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

public static void send(String msg)
{
    if (!closed)
    {
        os.println(msg);
    }
}

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

And the class: 和班级:

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;
import org.jnativehook.mouse.NativeMouseWheelEvent;
import org.jnativehook.mouse.NativeMouseWheelListener;

public class Listener implements NativeKeyListener, NativeMouseInputListener, NativeMouseWheelListener
{
public void nativeKeyPressed(NativeKeyEvent e) 
{   
    MultiThreadChatClient.send(NativeKeyEvent.getKeyText(e.getKeyCode()));
}

public void nativeKeyReleased(NativeKeyEvent e) 
{
}

public void nativeKeyTyped(NativeKeyEvent e)
{
}

public void nativeMouseClicked(NativeMouseEvent e) 
{
}

public void nativeMousePressed(NativeMouseEvent e) 
{
}

public void nativeMouseReleased(NativeMouseEvent e) 
{
}

public void nativeMouseMoved(NativeMouseEvent e)
{
}

public void nativeMouseDragged(NativeMouseEvent e) 
{
}

public void nativeMouseWheelMoved(NativeMouseWheelEvent e)
{
}

public static void startListener()
{
    try 
    {
        GlobalScreen.registerNativeHook();
    }
    catch (NativeHookException ex)
    {
        System.err.println("Et problem oppsto.");
        System.err.println(ex.getMessage());

        System.exit(1);
    }
    Listener listener = new Listener();
    GlobalScreen.getInstance().addNativeKeyListener( listener );
    GlobalScreen.getInstance().addNativeMouseListener( listener );
    GlobalScreen.getInstance().addNativeMouseMotionListener( listener );
    GlobalScreen.getInstance().addNativeMouseWheelListener( listener );
}
}

My problem is that right after registering the key listeners in the Listener class the program terminates with the code '0'. 我的问题是,在将侦听器中注册了关键侦听器之后,程序立即以代码“ 0”终止。

It used to work until I did something and now it just terminates upon start. 它一直起作用,直到我做了一些事情,现在它才在启动时终止。 I tried to start a thread just to keep the program alive, and that worked, but noone of the listeners worked. 我试图启动一个线程只是为了使程序保持活动状态,并且该方法起作用了,但是没有一个侦听器起作用。

Thank you in advance! 先感谢您!

Update: 更新:

I tried with the exampleclass from https://code.google.com/p/jnativehook/wiki/examples but event that does not work anymore?! 我尝试了https://code.google.com/p/jnativehook/wiki/examples中的exampleclass,但是事件不再起作用了吗?

the same problem as with the above problem. 与上述问题相同的问题。 Both my classes ( the ones above ) and the example calsses used to work, but now they all just terminates after registering the listeners ( The main method in the example classes ) 我的类(上面的类)和示例calscal都可以正常工作,但是现在它们都只是在注册侦听器后终止(示例类中的main方法)

Is it my eclipse or anything that is wrong here? 是我的月食还是这里有什么问题?

如果不运行您的代码,我的猜测是jvm的“ main”线程启动了侦听器,但随后没有阻塞,因此退出了“ main”方法。

Your program runs and ends. 您的程序运行并结束。 It is over, finished and is done. 它结束了,完成了并且完成了。 So it "stops". 因此它“停止”。

Starting a (new) thread would be the right way. 启动(新)线程将是正确的方法。 But do not use a thread to "keep the program alive", use it for something useful. 但是不要使用线程“使程序保持活动状态”,而是将其用于有用的事情。 In your case, a thread could open a socket and listen on that socket for incoming messages (you are writing a chat client, right?). 在您的情况下,线程可以打开套接字并在该套接字上侦听传入消息(您正在编写聊天客户端,对吗?)。 If a message comes in, the thread will handle it and response. 如果出现消息,则线程将处理它并做出响应。

Or make a new thread out of your listener - instead of a method startListener , you could create a method called startListenerThread . 或者做一个新的线程你的听众的-而不是一个方法startListener ,你可以创建一个名为方法startListenerThread

Your progamm flow would be: 您的程序流将是:

  • start main method (the main thread) 启动main方法(主线程)
  • the main thread starts the socket thread 主线程启动套接字线程
  • the main thread starts the .... (what ever) 主线程启动....(无论如何)
  • the main thread waits until all other threads have finished (it blocks) 主线程等待,直到所有其他线程都完成(它阻塞)

Starting with Windows Vista, Microsoft made un/under-documented changes to the way SetWindowsHookEx() works. 从Windows Vista开始,Microsoft对SetWindowsHookEx()的工作方式进行了未记录或未记录在案的更改。 Basically they have set a upper limit on the number of hooks that can be used and they have also set processing time limits on each callback. 基本上,它们为可使用的挂钩数设置了上限,并且还为每个回调设置了处理时间限制。 There is no way to fix, detect or otherwise work around this new behavior. 无法修复,检测或以其他方式解决此新行为。 Please let me know how you managed to keep that many copes of the hook running and what version of the library you are using. 请让我知道您如何设法使大量的钩子运行,以及您使用的是哪个版本的库。 Additional information on the problem can be found here . 有关此问题的其他信息,请参见此处

Update: I wanted to answer the original question about the inconsistencies in the way the application terminates after reaching the end of main. 更新:我想回答有关应用程序到达主程序结束后终止方式不一致的原始问题。 There were some issues in the way the native thread was attaching to the JVM prior to 1.2.0 RC1 that were causing part of the issue you were describing above. 在1.2.0 RC1之前,本机线程连接到JVM的方式中存在一些问题,这些问题导致了您在上面描述的问题的一部分。 That native thread should now be attached as a user thread, opposed to a daemon thread, which will prevent the application from terminating as long as the native thread is registered. 现在,该本地线程应该作为用户线程(而不是守护程序线程)作为用户线程附加,这将防止应用程序在注册了本地线程后终止。 I believe this was the behavior you were originally expecting from the library. 我相信这是您最初希望从库中获得的行为。

I rebooted my computer and it worked again. 我重新启动计算机后,它又可以工作了。 My program does not properly shut down and gets stuck in memory as a Listener. 我的程序无法正常关闭,并作为侦听器卡在内存中。 It seems that the program only works until there are about 8 other of the same application in memory running. 似乎该程序只有在内存中有大约8个同一应用程序运行时才起作用。 I do not understand how this happen, but after I fix my shutdown precedure everything should be fine again. 我不知道这是怎么发生的,但是在修复关机程序之后,一切应该都会恢复正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java程序在JNI方法调用后终止 - Java Program terminates after JNI method call 使用java.util.Timer后程序未终止 - Program not terminates after java.util.Timer usage 在 Java 中执行 toUpperCase 方法后程序随机终止 - Program randomly terminates after executing toUpperCase method in Java 批处理文件在执行Java程序后立即终止(如果从另一个Java程序调用了批处理文件) - Batch file immediately terminates after executing a Java program (if batch file is called from another Java program) 为什么java程序在执行增量运算符后终止? 在下面的代码中,它只打印 2 而不是条件语句 - Why does java program terminates after executing increment operators? In the below code it only prints 2 and not the condition statements 程序终止后,是否可以保存输入信息? - Is there a way to save input information after the program terminates? Eclipse Mars中的Java程序是否立即终止而没有输出? - Java program in Eclipse Mars, immediately terminates with no output? Java程序终止而不是遍历选项 - Java program terminates instead of looping through options Java + DOM:注册和使用修改监听器:教程? - Java+DOM: Registering and using modification listeners: tutorials? Java侦听器-键入时鼠标左键未注册 - Java listeners - Mouse left-press not registering while typing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM