devshift / jnativehook

Automatically exported from code.google.com/p/jnativehook
0 stars 0 forks source link

WebStart crashes #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Extract attached file and open as project with NetBeans IDE.
2. Fix javaws.jar reference (if necessary). 
3. Run project using WebStart configuration.
4. You will get Java WebStart error. 

What version of the product are you using? 

I am using JNativeHook 1.0.2288 and DetBeans 7.0.1

On what operating system?

I tried it on:

  * Windows XP 32-bit jdk1.6.0_29 and jdk1.7.0_01
  * Mac X OS Snow Leopard 1.6.0_29-b11-402.jdk
  * Linux Ubuntu 11.04 32-bit java-6-sun-1.6.0.26

When I run example (demo) application as regular Java application it works 
well. 

WebStart error occurs in all these environments.

Original issue reported on code.google.com by ai...@jug.lt on 5 Dec 2011 at 12:26

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Let me begin by stating that I have never used java web start.  Now based on 
what I have read it should be possible to use JNativeHook with Java web start.

I would start by extracting the lib folder in the JNativeHook.jar file.  Create 
new jar files for each native library you wish to provide support for (Ex: 
lib/Windows/x86/corelib.jar would contain JNativeHook.dll).  Then add the 
appropriate JNLP configuration:

<resources os="Windows" arch="x86">
<nativelib href="lib/Windows/x86/corelibs.jar"/>
</resource>

Let me know if that works for you.

See the following links for additional information: 
http://lopica.sourceforge.net/ref.html#nativelib
http://lopica.sourceforge.net/ref.html#resources
http://www.velocityreviews.com/forums/t126704-java-webstart-and-jni.html

Original comment by heispsyc...@gmail.com on 12 Jan 2012 at 7:36

GoogleCodeExporter commented 9 years ago
System.loadLibrary(<LibraryName>)

Sometimes works sometimes not with Java WebStart. This problem is old enought:  

http://www.java.net/node/683179
http://java.dzone.com/articles/jni-java-web-start-applet

For this reason I tried to work only with 

System.load(libFile.getAbsolutePath());

To speed up figuring out where is problem  
I used some third party JNI libraries (JXGrabKey, JNIDemoCdl (
http://netbeans.org/projects/samples/downloads/download/Samples/CPlusPlus/JNIDem
o.zip))

Using code:

    static void loadResoureLibrary(final String name) {
        try {
            String osName = System.getProperty("os.name").toLowerCase();
            String libName;
            if (osName.startsWith("windows")) {
                libName = name + ".dll";
                osName = "windows";
            } else if (osName.equals("mac os x")) {
                osName = "osx";
                libName = "lib" + name + ".dylib";
            } else {
                libName = "lib" + name + ".so";
            }
            String libPath = "/org/jnativehook/lib/"
                    + osName
                    + "/" + NativeSystem.getArchitecture().toString().
                    toLowerCase() + "/" + libName;
            File libFile = new File(System.getProperty("java.io.tmpdir")
                    + System.getProperty("file.separator", File.separator) + libName);
            FileOutputStream tempLibOutputStream = new FileOutputStream(libFile);
            byte[] array = new byte[4 * 1024];
            InputStream is = TestJNativeHookLibLoad.class.getResourceAsStream(libPath);
            int read;
            while ((read = is.read(array)) > 0) {
                tempLibOutputStream.write(array, 0, read);
            }
            tempLibOutputStream.close();
            is.close();
            libFile.deleteOnExit();
            System.load(libFile.getAbsolutePath());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

These libraries loads from *.jar file with no problems.

JNativeHook library crashes Java WebStart executing System.load(...)    

Original comment by ai...@jug.lt on 22 Jan 2012 at 10:44

GoogleCodeExporter commented 9 years ago
I did some debugging on Win32, code execution stops while executing the 
following LOC within MsgLoop function : 
  jmethodID getInstance_ID = env->GetStaticMethodID(clsGlobalScreen, "getInstance", "()Lorg/jnativehook/GlobalScreen;");

I could get a line further by changing the returned type from 
"Lorg/jnativehook/GlobalScreen;" to "org/jnativehook/GlobalScreen", in JNI 
terms returning not the reference to the object, but a static object itself.
Anyway it crashes a few lines below.

Do you have any plan to fix this?

Original comment by moretto....@gmail.com on 20 Feb 2012 at 11:13

GoogleCodeExporter commented 9 years ago
I found a workaround moving the following instructions out of the MsgLoop 
thread, within JNI_OnLoad:

  int iret = jvm->AttachCurrentThread((void**)&env,NULL);
    jclass clsGlobalScreen = env->FindClass("org/jnativehook/GlobalScreen");

  jmethodID getInstance_ID = env->GetStaticMethodID(clsGlobalScreen, "getInstance", "()Lorg/jnativehook/GlobalScreen;");

    //A reference to the GlobalScreen Object
    jobject objScreen = env->CallStaticObjectMethod(clsGlobalScreen, getInstance_ID);
  objGlobalScreen = env->NewGlobalRef(objScreen);

    //Get the ID of the GlobalScreen Objects dispatch event method.
    idDispatchEvent = env->GetMethodID(clsGlobalScreen, "dispatchEvent", "(Lorg/jnativehook/NativeInputEvent;)V");

    //Class and Constructor for the NativeKeyEvent Object
    jclass clsLocalKeyEvent = env->FindClass("org/jnativehook/keyboard/NativeKeyEvent");
    clsKeyEvent = (jclass)env->NewGlobalRef(clsLocalKeyEvent);
    idKeyEvent = env->GetMethodID(clsKeyEvent, "<init>", "(IJIIII)V");

    //Class and Constructor for the NativeMouseEvent Object
    jclass clsLocalMouseEvent = env->FindClass("org/jnativehook/mouse/NativeMouseEvent");
    clsMouseEvent = (jclass)env->NewGlobalRef(clsLocalMouseEvent);
    idMouseButtonEvent = env->GetMethodID(clsMouseEvent, "<init>", "(IJIIII)V");
    idMouseMotionEvent = env->GetMethodID(clsMouseEvent, "<init>", "(IJIII)V");

Original comment by moretto....@gmail.com on 20 Feb 2012 at 11:37

GoogleCodeExporter commented 9 years ago
The current trunk does a much better job of handling threads and JVM attachment 
in native code.  The implementation is not quite done yet but you may want to 
take a look at it.  Development has slowed down briefly due to a paper 
associated with this project that is in the final stages of approval.  Once 
that is done (Hopefully this week) I plan on finishing development on a 1.1 
version and addressing this bug.  I should have a preview releases done in 
early March for at least Windows and Linux.  I will let you know when the trunk 
will build on win32. 

Original comment by heispsyc...@gmail.com on 21 Feb 2012 at 12:05

GoogleCodeExporter commented 9 years ago
Hello, 

  can you please give me details on the toolchain you use to build Win32 and Win64 natives? 

I can build them, but sometimes I get java exceptions while loading the 
library: UnsatisfiedLinkError exceptions.
The DLLs you provided don't show this issue, it may be very useful to me have 
the details of your build toolchain.

Thanks,

  regards

Original comment by moretto....@gmail.com on 22 Feb 2012 at 6:36

GoogleCodeExporter commented 9 years ago
I am using the MinGW-w64 toolchain on linux to build the win32 and 64 dll's.  
The build.properties files for each platform is available in the svn repository 
under branches/cross_compile.  Either MinGW or MinGW-w64 should work for 
building on windows as well.  If your still having problems paste your 
properties files and I will have a look.

Original comment by heispsyc...@gmail.com on 23 Feb 2012 at 6:55

GoogleCodeExporter commented 9 years ago
Ok, I may have found a bug that is leading to the UnsatisfiedLinkError 
exception on windows.  Try changing "native.libs=-lcrtdll -luser32 -lkernel32" 
line in your build properties file to "native.libs=-lmsvcrt -luser32 -lkernel32"

Original comment by alex%1st...@gtempaccount.com on 6 Mar 2012 at 9:18

GoogleCodeExporter commented 9 years ago
On a separate note, the trunk now compiles and seems to function correctly for 
windows.  Give it a try and see if it fixes the issue with web-start.  The fix 
mentioned previously has been applied to the trunk as of revision 457.

Original comment by alex%1st...@gtempaccount.com on 7 Mar 2012 at 12:32

GoogleCodeExporter commented 9 years ago

Original comment by alex%1st...@gtempaccount.com on 11 Mar 2012 at 7:18

GoogleCodeExporter commented 9 years ago
To the best of my knowledge, there is not reason that the code in the trunk 
should not work yet it continues to cause a null pointer issue under webstart.  
This leads me to believe that the problem is caused by a BUG in Oracles 
implementation of webstart.  I am currently implementing a workaround similar 
to the one you suggested on the trunk.  It should be ready shortly.

Original comment by alex%1st...@gtempaccount.com on 11 Mar 2012 at 11:27