java.lang.NoSuchFieldError: handle

1.3k Views Asked by At

I recently come across an issue with my Eclipse SWT GUI application. I was trying to acquire a window handle with the following code:

Display.getDefault().syncExec(()->{
            handle = Display.getDefault().getActiveShell().handle;
        });

worked perfectly on my 64bit Windows 10 OS.

Then when I deploy the same app on a 32bit Windows machine (a VM I setup on my pc using Hyper-v), I get the following weird error:

java.lang.NoSuchFieldError: handle

The documentation for this handle says:

long org.eclipse.swt.widgets.Control.handle

the handle to the OS resource(Warning: This field is platform dependent)

IMPORTANT: This field is not part of the SWTpublic API. It is marked public only so that it can be >sharedwithin the packages provided by SWT. It is not available on allplatforms and should never be >accessed from application code.

Could this be the reason that was causing this issue?

If we can't use this property, what are the alternatives? I know JNI / JNA deals with native code, they still rely on SWT implementation to acquire the handle as far as I know.

Thanks for taking time on my question!

Cheers!

1

There are 1 best solutions below

0
On

I would have thought that Javadoc makes the situation absolutely clear. SWT internals should not be used, they are completely different on each platform (which may include 32/64 bit versions on the same OS), they may even change between releases.

The 32 bit version of the Windows SWT does have a handle field but it is an int rather than a long in the 64 bit version - which is what is giving you this error. SWT for other platforms doesn't have a handle field at all.

If you really must access something like this you will have to write code specific to each platform / window system / architecture. For Eclipse plug-ins this can be done with platform specific fragments. I suppose for this particular case you could also use reflection to look for both int and long 'handle' fields.

Also note that current versions of Eclipse no longer support 32 bit at all.