[英]Java in Linux - different look and feel classes for root and non-root
I noticed that Java proposes different look and feel classes for root and non-root users. 我注意到Java为root用户和非root用户提出了不同的外观和感觉类。 I am trying to understand how to make LAF consistent. 我试图了解如何使LAF保持一致。 Moreover, it's inconsistent even within a user/root: depends on how user/root logged in: 而且,即使在用户/ root中也不一致:取决于用户/ root登录的方式:
Sample code (compiled and packaged in laf.jar
): 示例代码(在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) {
}
}
}
Scenario 1 Logs in to machine (in GUI mode) as a regular user 方案1以普通用户身份登录到计算机(在GUI模式下)
Sample output (as user ) 样本输出(以用户身份)
[xxx@yyy Downloads]$ java -classpath laf.jar laf
com.sun.java.swing.plaf.gtk.GTKLookAndFeel
Sample output (switch to root via su
) 样本输出(通过su
切换到root )
[root@yyy Downloads]# java -classpath ./laf.jar laf
javax.swing.plaf.metal.MetalLookAndFeel
Scenario 2 Logs in to machine (in GUI mode) as root 方案2以root身份登录到计算机(在GUI模式下)
Sample output (as root ) 示例输出(以root身份)
[root@yyy Downloads]# java -classpath ./laf.jar laf
com.sun.java.swing.plaf.gtk.GTKLookAndFeel
Scenario 3 Logs in to machine via SSH as a regular user (similar as scenario #1 above, but in this case - same LAF) 场景3作为普通用户通过SSH登录到计算机(类似于上面的场景#1,但在这种情况下 - 相同的LAF)
Sample output (as user ) 样本输出(以用户身份)
[xxx@yyy Downloads]$ java -classpath laf.jar laf
javax.swing.plaf.metal.MetalLookAndFeel
Sample output (switch to root ) 示例输出(切换到root )
[root@yyy Downloads]# java -classpath ./laf.jar laf
javax.swing.plaf.metal.MetalLookAndFeel
Software versions: 软件版本:
[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)
This is less about root, and more about environment variables. 这不是关于root的,而是关于环境变量的更多信息。
Basically, the UIManager.getSystemLookAndFeelClassName
method works like this: 基本上, UIManager.getSystemLookAndFeelClassName
方法的工作方式如下:
swing.systemlaf
system property. 检查swing.systemlaf
系统属性。 This allows the user to override whatever the system wants to choose. 这允许用户覆盖系统想要选择的任何内容。 If it is not null, it's used. 如果它不为null,则使用它。 WindowsLookAndFeel
. 否则,如果操作系统是Windows,则返回WindowsLookAndFeel
。 sun.desktop
property. 否则,它会检查sun.desktop
属性。 If sun.desktop
is set to gnome
, and GTK is available natively, it returs the GTKLookAndFeel
如果将sun.desktop
设置为gnome
,并且GTK本机可用,则会重新启动GTKLookAndFeel
MetalLookAndFeel
. 如果所有其他检查都失败,则返回“跨平台”L&F,即MetalLookAndFeel
。 So, the part that is relevant to Linux/Unix is the part that checks sun.desktop
. 因此,与Linux / Unix相关的部分是检查sun.desktop
的部分。 This property is set when the JVM starts up. JVM启动时设置此属性。 It is set to gnome
if the environment variable GNOME_DESKTOP_SESSION_ID
exists, ignoring its contents, and it is set to null otherwise. 如果环境变量GNOME_DESKTOP_SESSION_ID
存在,则将其设置为gnome
,忽略其内容,否则设置为null。 I believe this is the pertinent native source code that does this. 我相信这是相关的原生源代码 。
So, on Linux, if that environment variable is set (and GTK is available), your L&F will be set to GTKLookAndFeel
. 因此,在Linux上,如果设置了该环境变量(并且GTK可用),则您的L&F将设置为GTKLookAndFeel
。 If not, it will be set to MetalLookAndFeel
. 如果没有,它将被设置为MetalLookAndFeel
。
When you log in to a Gnome-based Linux using the desktop manager, your environment will have that variable set. 使用桌面管理器登录基于Gnome的Linux时,您的环境将设置该变量。 But the su
command does not propagate environment variables by default. 但su
命令默认不传播环境变量 。 Thus, when you su to any user, not necessarily root, you lose the GNOME_DESKTOP_SESSION_ID
environment variable, and Java will default to MetalLookAndFeel
. 因此,当你对任何用户(不一定是root)su时,你会丢失GNOME_DESKTOP_SESSION_ID
环境变量,而Java将默认为MetalLookAndFeel
。
You can cause your environment to be passed through su
by using su -p
, or if you are using sudo
, using sudo -E
. 您可以使用su -p
或使用sudo -E
使用sudo
来使您的环境通过su
传递。
The ssh
command, like su
and sudo
, does not propagate environment variables. ssh
命令(如su
和sudo
)不会传播环境变量。 This can also be worked around using ~/.ssh/environment
. 这也可以使用~/.ssh/environment
。
However, as already stated - you can easily force a specific L&F by passing the -Dswing.systemlaf=...
switch to the java command. 但是, -Dswing.systemlaf=...
- 您可以通过将-Dswing.systemlaf=...
切换到java命令来轻松强制执行特定的L&F。
The first line of getSystemLookAndFeelClassName
is: getSystemLookAndFeelClassName
的第一行是:
public static String getSystemLookAndFeelClassName() {
String systemLAF = AccessController.doPrivileged(
new GetPropertyAction("swing.systemlaf"));
So you can use the JAVA_OPTS of the user to set 因此您可以使用用户的JAVA_OPTS进行设置
-Dswing.systemlaf=javax.swing.plaf.metal.MetalLookAndFeel
As default. 默认情况下。
add this to the .rc
-File of the user: 将此添加到用户的.rc
-File:
set JAVA_OPTS=-Dswing.systemlaf=javax.swing.plaf.metal.MetalLookAndFeel
export JAVA_OPTS
Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.