Linux中的Java-root和non-root的不同外观类

问题描述:

我注意到Java为root和非root用户提出了不同的外观类.我试图了解如何使LAF保持一致.此外,即使在用户/根目录中也不一致:取决于用户/根目录的登录方式:

I noticed that Java proposes different look and feel classes for root and non-root users. I am trying to understand how to make LAF consistent. Moreover, it's inconsistent even within a user/root: depends on how user/root logged in:

示例代码(在laf.jar中编译和打包):

Sample code (compiled and packaged in laf.jar):

import javax.swing.UIManager;

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

场景1 以普通用户身份登录到计算机(以GUI模式)

Scenario 1 Logs in to machine (in GUI mode) as a regular user

示例输出(以用户的身份)

[xxx@yyy Downloads]$ java -classpath laf.jar laf
com.sun.java.swing.plaf.gtk.GTKLookAndFeel

样本输出(通过su切换到 root )

Sample output (switch to root via su)

[root@yyy Downloads]# java -classpath ./laf.jar laf
javax.swing.plaf.metal.MetalLookAndFeel

场景2 以root用户身份登录到计算机(以GUI模式)

Scenario 2 Logs in to machine (in GUI mode) as root

样本输出(作为 root )

[root@yyy Downloads]# java -classpath ./laf.jar laf
com.sun.java.swing.plaf.gtk.GTKLookAndFeel

方案3 以普通用户身份通过​​SSH登录到计算机(类似于上述方案1,但在这种情况下-相同的LAF)

Scenario 3 Logs in to machine via SSH as a regular user (similar as scenario #1 above, but in this case - same LAF)

示例输出(以用户的身份)

[xxx@yyy Downloads]$ java -classpath laf.jar laf
javax.swing.plaf.metal.MetalLookAndFeel

示例输出(切换到 root )

[root@yyy Downloads]# java -classpath ./laf.jar laf
javax.swing.plaf.metal.MetalLookAndFeel

软件版本:

[root@yyy Downloads]# java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build pxa6470sr9fp10-20150708_01(SR9 FP10))
IBM J9 VM (build 2.6, JRE 1.7.0 Linux amd64-64 Compressed References     20150701_255667 (JIT enabled, AOT enabled)
J9VM - R26_Java726_SR9_20150701_0050_B255667
JIT  - tr.r11_20150626_95120.01
GC   - R26_Java726_SR9_20150701_0050_B255667_CMPRSS
J9CL - 20150701_255667)
JCL - 20150628_01 based on Oracle jdk7u85-b15

[root@yyy Downloads]# cat /etc/redhat-release 
Red Hat Enterprise Linux Workstation release 6.7 (Santiago)

getSystemLookAndFeelClassName的第一行是:

public static String getSystemLookAndFeelClassName() {
    String systemLAF = AccessController.doPrivileged(
                         new GetPropertyAction("swing.systemlaf"));

因此您可以使用用户的JAVA_OPTS进行设置

So you can use the JAVA_OPTS of the user to set

-Dswing.systemlaf=javax.swing.plaf.metal.MetalLookAndFeel

默认.

将此添加到用户的.rc-文件:

add this to the .rc-File of the user:

set JAVA_OPTS=-Dswing.systemlaf=javax.swing.plaf.metal.MetalLookAndFeel
export JAVA_OPTS

致谢