Error in reading Ubuntu 14.04 mouse event file (/dev/input/event3) with java programmig

498 views Asked by At

I want to handle mouse event in Linux terminal via java programming. I wrote two program via c++ and java that they do same process.

when i use c++ programming to open and read file ("/dev/input/event3"-mouse event file), there is no problem while running executable file. (Ubuntu 14.04 terminal and Ubuntu server 14.04 ---> no problem).

But when i used java programming to open and read the same file ,there is a problem while running executable result file "java -jar program.jar" in Ubuntu 14.04 "terminal". No error for opening file , but reading file make an error like this:

java.io.IOException: Invalid argument
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(Unknown Source)
    at eventfileopen.m.main(m.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

But when i run the same jar file on Ubuntu server 14.04, there is no error and the program work perfectly. How can I resolve the problem?(Problem = run the java program on Ubuntu 14.04 with no error.) I guess the problem is related to Ubuntu GUI and JDK,but no idea for resolving that. My java program code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

public class m {

    public static void main(String[] args) {

        File f = new File("/dev/input/event3");
        FileInputStream is = null;
        try {
            is = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] bytes = new byte[16];
        while(true){
            try 
            {
                is.read(bytes);
            } catch (IOException e) 
            {
                e.printStackTrace();   //-------> error while run on Ubuntu 14.04 and no error on Ubuntu server
            }
            System.out.println(Arrays.toString(bytes));
        }
    }
}

1

There are 1 answers

1
Ilya Polenov On BEST ANSWER

Check that your buffer size is correct. In recent Linux kernels size of input_event structure is dependent on many things, but mainly on cpu architecture. Here's its declaration:

struct input_event {
      struct timeval time;
      __u16 type;
      __u16 code;
      __s32 value;
};

On 32-bit systems it's very likely that your buffer size is accepted(16 bytes).

On 64-bit systems it's very likely that the buffer should be no less than 24 bytes. The reason is that timeval struct takes twice as much space(8 bytes more) than on 32-bit systems.

I haven't found a reliable way to get input_event struct size from Java and personally I use JNI for that.