2

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

Chris Suszyński
  • 1,494
  • 17
  • 23

1 Answers1

1

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

Chris Suszyński
  • 1,494
  • 17
  • 23
  • Wouldn't it make more sense to pass on the environmental variables using a `sudo` switch per http://askubuntu.com/questions/70534/what-are-the-differences-between-su-sudo-s-sudo-i-sudo-su? – tresf Nov 15 '16 at 05:47