Chris Suszyński
Chris Suszyński

Reputation: 1554

How to preserve GTK look & feel in Java using sudo

Often when you need to run some Java installers, you just do something like this:

$ sudo netbeans-8.1-linux.sh

And unfortunately this switches your nice, native GTK look & feel to some ugly, default Metal theme.

You can test it with this sample class:

// laf.java
import javax.swing.UIManager;

public class laf {
    public static void main(java.lang.String[] args) {
        try {
            System.out.println(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
    }
}

And some output:

$ javac laf.java
$ java laf
com.sun.java.swing.plaf.gtk.GTKLookAndFeel
$ sudo java laf
javax.swing.plaf.metal.MetalLookAndFeel

This is how it looks using sudo:

Standard Metal there on while using sudo with Java

Upvotes: 2

Views: 444

Answers (1)

Chris Suszyński
Chris Suszyński

Reputation: 1554

To properly fix this behavior, you can add a sudoers file:

vim /etc/sudoers.d/20_keep_java_laf

with contents:

Defaults env_keep+=GNOME_DESKTOP_SESSION_ID

This will be cause UIManager#getSystemLookAndFeelClassName() to resolve desktop, nice, GTK look & feel.

Now it is fixed:

After fix to sudoers

Upvotes: 1

Related Questions