java-native-access/jna · error · UnsupportedOperationException

Set sun.java2d.noddraw=true to enable transparent windows

Error message

Set sun.java2d.noddraw=true to enable transparent windows

What it means

W32WindowUtils.setWindowAlpha only works when per-pixel alpha is available; isWindowAlphaSupported() returns false unless the DirectDraw/Direct3D pipeline is disabled via sun.java2d.noddraw=true (Java 6/7 era requirement). When unsupported, the method throws UnsupportedOperationException telling you to set that system property.

Source

Thrown at contrib/platform/src/com/sun/jna/platform/WindowUtils.java:805

            }
        }

        /** Return the last alpha level we set on the window. */
        private byte getAlpha(Window w) {
            if (w instanceof RootPaneContainer) {
                JRootPane root = ((RootPaneContainer)w).getRootPane();
                Byte b = (Byte)root.getClientProperty(TRANSPARENT_ALPHA);
                if (b != null) {
                    return b.byteValue();
                }
            }
            return (byte)0xFF;
        }

        @Override
        public void setWindowAlpha(final Window w, final float alpha) {
            if (!isWindowAlphaSupported()) {
                throw new UnsupportedOperationException("Set sun.java2d.noddraw=true to enable transparent windows");
            }
            whenDisplayable(w, new Runnable() {
                @Override
                public void run() {
                    HWND hWnd = getHWnd(w);
                    User32 user = User32.INSTANCE;
                    int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
                    byte level = (byte)((int)(255 * alpha) & 0xFF);
                    if (usingUpdateLayeredWindow(w)) {
                        // If already using UpdateLayeredWindow, continue to
                        // do so
                        BLENDFUNCTION blend = new BLENDFUNCTION();
                        blend.SourceConstantAlpha = level;
                        blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
                        user.UpdateLayeredWindow(hWnd, null, null, null, null,
                                                 null, 0, blend,
                                                 WinUser.ULW_ALPHA);
                    }

View on GitHub (pinned to d036ad9781)

Solutions

  1. Launch the JVM with -Dsun.java2d.noddraw=true (must be set before any AWT/Swing initialization).
  2. Set it programmatically as the very first line of main() before any Window is created: System.setProperty("sun.java2d.noddraw", "true").
  3. Alternatively use AWT's native setOpacity/setWindowOpacity (Java 7+) instead of WindowUtils when available.
  4. Guard the call with WindowUtils.isWindowAlphaSupported() and degrade to opaque windows if false.

Example fix

// before
WindowUtils.setWindowAlpha(frame, 0.7f);
// after
public static void main(String[] args) {
    System.setProperty("sun.java2d.noddraw", "true"); // before any AWT init
    ...
    WindowUtils.setWindowAlpha(frame, 0.7f);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!WindowUtils.isWindowAlphaSupported()) { /* fallback */ } else { WindowUtils.setWindowAlpha(w, alpha); }

Try / catch

try { WindowUtils.setWindowAlpha(frame, 0.7f); } catch (UnsupportedOperationException e) { // fallback to opaque or Java 7+ setOpacity }

Prevention

When it happens

Trigger: Calling WindowUtils.setWindowAlpha(window, alpha) on Windows while -Dsun.java2d.noddraw=true is NOT set (default on Java 6 where it must be set; on Java 7+ the check may still fail in some configurations).

Common situations: Adding window-transparency to a Java 6 Swing app and forgetting the JVM flag; launching via javaw without VM args; embedding the app in a launcher (e.g. Launch4j/exe wrappers) that does not pass -Dsun.java2d.noddraw=true; conflicts with DirectDraw rendering causing flicker when the flag is absent.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/e0c5e4623481b544. Report an issue: GitHub.