首页 > 解决方案 > JAVA KeyEvent 序列化错误

问题描述

我正在尝试KeyEvent通过套接字对象输出流发送一个包含字段的对象,javadoc 说它KeyEvent正在实现Serializable接口,但是每当我尝试发送它时都会抛出一个java.io.NotSerializableException.

我要发送的对象是KeyboardCommand

import java.awt.Robot;

public interface ICommandAction {
void doAction(Robot operator);
public enum ActionType {GenericAction, MouseAction, KeyboardAction, CheckBooleanAction};
ActionType getAction();
}

import java.awt.Robot;
import java.io.Serializable;

public class CommandBase implements Serializable, ICommandAction{
private static final long serialVersionUID = 2L;
private ActionType action;

public CommandBase(ActionType action){
    this.action = action;
}

public ActionType getAction(){
    return this.action;
}

public void doAction(Robot operator){
    try {
        operator.wait(20);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

import java.awt.Robot;
import java.awt.event.KeyEvent;

public class KeyboardCommand extends CommandBase {

private static final long serialVersionUID = 8L;
private KeyEvent event;

public KeyboardCommand(KeyEvent event) {
    super(ActionType.KeyboardAction);
    this.event = event;
}

/**
 * Presses and releases the event's key 
 */
public void key(Robot operator, int delay){
    operator.keyPress(event.getExtendedKeyCode());
    operator.delay(delay);
    operator.keyRelease(event.getExtendedKeyCode());
}

/**
 * Performs the keyboard action
 */
@Override
public void doAction(Robot operator){
    boolean isUpperCase = Character.isUpperCase(event.getKeyChar());
    if(isUpperCase) operator.keyPress(KeyEvent.SHIFT_DOWN_MASK);
    key(operator, 20);
    if(isUpperCase) operator.keyRelease(KeyEvent.SHIFT_DOWN_MASK);
    System.out.println("Keyboard command executed");
}

@Override
public String toString(){
    return "KeyCode: " + this.event.getKeyCode() + ", keyChar: " + this.event.getKeyChar();
}        
}

public synchronized void sendCommand(CommandBase command){
    try {
        commandOutputStream.writeObject(command); //error is here
        commandOutputStream.flush();
        System.out.println("Sent " + command.getAction().toString());
    } catch (IOException e) {
        System.out.println("Failed to send " + command.getAction().toString());
        try {
            commandOutputStream.close();
            running = false;
            this.dispose();
            System.out.println("Closing command output stream");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    }
}

错误代码

错误commandOutputStream.writeObject(command)在线,这意味着它无法在发件人方面进行序列化。

据我所知,一切都应该是Serializable,如果有人知道我在哪里搞砸了你的帮助,我们将不胜感激:)

微型例子:(KeyboardCommand相同)

public class Constants {
final static int senderPort = 20000; 
final static int receiverPort = 20001;
}

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketAddress;

import javax.swing.JFrame;

import commands.CommandBase;
import commands.KeyboardCommand;

public class ControlFrame extends JFrame implements KeyListener, Runnable{
private static final long serialVersionUID = 12L;
private Socket sendAction;
private ObjectOutputStream commandOutputStream; 

public boolean running;

public ControlFrame(Socket socket){
    super("Controller");
    this.running = true;

    this.sendAction = socket;

    setSize(Toolkit.getDefaultToolkit().getScreenSize().width / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    setVisible(true);
    try {
        this.commandOutputStream = new ObjectOutputStream(sendAction.getOutputStream());
        System.out.println("Command output stream initialized");
    } catch (IOException e) {
        System.out.println("Command output stream failed to initialize");
        e.printStackTrace();
    } 

    Thread t = new Thread(this);
    t.start();
}

public synchronized void sendCommand(CommandBase command){
    try {
        commandOutputStream.writeObject(command);
        commandOutputStream.flush();
        System.out.println("Sent " + command.getAction().toString());
    } catch (IOException e) {
        System.out.println("Failed to send " + command.getAction().toString());
        try {
            commandOutputStream.close();
            running = false;
            this.dispose();
            System.out.println("Closing command output stream");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    }
}

@Override
public void keyTyped(KeyEvent e) {
    System.out.println("Key typed");
}

@Override
public void keyPressed(KeyEvent e) {
    System.out.println("Key pressed");
    System.out.println("Key command: " + e.toString());
    sendCommand(new KeyboardCommand(e));
}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println("Key released");
}

@Override
public void run() {
    addKeyListener(this);

    while(running){
        if(sendAction.isOutputShutdown())
            running = false;
    }
}
}

import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import commands.CommandBase;
import commands.ICommandAction.ActionType;
import commands.KeyboardCommand;

public class Receiver {

public static void main(String[] args) throws IOException, ClassNotFoundException, AWTException {

    ServerSocket acceptConnection = new ServerSocket(Constants.receiverPort);
    Socket receiver = acceptConnection.accept();

    ObjectInputStream receiverInput = new ObjectInputStream(receiver.getInputStream());
    Robot operator = new Robot();

    while(true){
        Object command = receiverInput.readObject();
        if(((CommandBase)command).getAction().equals(ActionType.KeyboardAction)){
            ((KeyboardCommand)command).doAction(operator);
        }
    }
}

}

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import FrameResources.ControlFrame;

public class Sender {

public static void main(String[] args) throws IOException {

    final InetAddress thisIP = InetAddress.getLocalHost();

    Socket sender = new Socket();
    sender.bind(new InetSocketAddress(thisIP, Constants.senderPort));

    sender.connect(new InetSocketAddress(thisIP, Constants.receiverPort));

    ControlFrame frame = new ControlFrame(sender);
}
}

在这个例子中,同样的错误发生了。

如果有任何不清楚的地方,请评论问题。

谢谢你的时间:D

标签: javaoopserializationclient-serverkeyevent

解决方案


你是对的,KeyEvent 是原因,可能是因为它包含对源对象的引用,你的 JFrame,一个可能包含可序列化和不可序列化字段的复杂对象......但你并不真的需要这些信息,因此想到了一个明显的解决方案——不要序列化 ​​KeyEvent,而是序列化和反序列化 KeyEvent 包含的关键信息、keyCode、keyChar 和 extendedKeyCode 字段。例如,注意更改:

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.Serializable;

public class KeyboardCommand extends CommandBase {

    private static final long serialVersionUID = 8L;
    // private KeyEvent event;
    private char keyChar;
    private int keyCode;
    private int extendedKeyCode;

    public KeyboardCommand(KeyEvent event) {
        super(ActionType.KeyboardAction);
        // this.event = event;
        keyChar = event.getKeyChar();
        keyCode = event.getKeyCode();
        extendedKeyCode = event.getExtendedKeyCode();
    }

    /**
     * Presses and releases the event's key
     */
    public void key(Robot operator, int delay) {
        // operator.keyPress(event.getExtendedKeyCode());
        operator.keyPress(extendedKeyCode);
        operator.delay(delay);
        // operator.keyRelease(event.getExtendedKeyCode());
        operator.keyRelease(extendedKeyCode);
    }

    /**
     * Performs the keyboard action
     */
    @Override
    public void doAction(Robot operator) {
        // boolean isUpperCase = Character.isUpperCase(event.getKeyChar());
        boolean isUpperCase = Character.isUpperCase(keyChar);
        if (isUpperCase)
            operator.keyPress(KeyEvent.SHIFT_DOWN_MASK);
        key(operator, 20);
        if (isUpperCase)
            operator.keyRelease(KeyEvent.SHIFT_DOWN_MASK);
        System.out.println("Keyboard command executed");
    }

    @Override
    public String toString() {
        // return "KeyCode: " + this.event.getKeyCode() + ", keyChar: " + this.event.getKeyChar();
        return "KeyCode: " + keyCode + ", keyChar: " + keyChar;
    }
}

推荐阅读