简体   繁体   English

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

[英]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方法的工作方式如下:

  • Check the 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,则使用它。
  • Otherwise, if the operating system is Windows, it returns the WindowsLookAndFeel . 否则,如果操作系统是Windows,则返回WindowsLookAndFeel
  • Otherwise, it checks the 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
  • Otherwise, checks for Mac OS X and Solaris and returns appropriate values for these operating systems. 否则,检查Mac OS X和Solaris并返回这些操作系统的适当值。
  • If all other checks failed, it returns the "cross platform" L&F, which is 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命令(如susudo )不会传播环境变量。 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.

相关问题 如何以root身份启动Java程序,但降级为非root用户 - How to start Java program as root but downgrade to non-root user 作为非root用户的Java进程消耗100%的CPU,但是root用户很好 - Java process as non-root consumes 100% CPU, but as root is fine 高山Linux,非root用户,Java 7,Setcap:libjli.so:没有这样的文件或目录(java所需) - Alpine Linux, Non-Root User, Java 7, Setcap: libjli.so: No such file or directory (needed by java) 在Linux上,人们是否chroot Java Web应用程序或使用IPTables并以非root身份运行? - On Linux do people chroot a Java Web Application or use IPTables and run as non-root? 以非root用户身份使用Solaris SMF运行Java应用程序 - Running Java Application with Solaris SMF as Non-Root User 可以使用Chef以非root用户身份安装Java吗? - Can Java be installed as non-root user with Chef? 如何在CentOs 6上以非root用户身份运行Java服务 - How to run java service as a non-root user on CentOs 6 Java 应用程序无法以 debian 中的非 root 用户身份连接到 mysql - Java application fails to connect to mysql as non-root user in debian 在root用户和非root用户下,Swing GUI看起来有所不同 - Swing GUI looks different under root user vs. non-root user 在非根路由上获得响应 404 - Getting response 404 on non-root routes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM