Friday 12 May 2017

How to detect OS uses KDE or Gnome environment in Java?

In this post, I am going to explain, how can you detect whether your os is using KDE or GNOME.

Following snippet return true, if the desktop is using Gnome.

         public static boolean isGnome() {
                 return System.getenv("GNOME_DESKTOP_SESSION_ID") != null;
         }

Following snippet return true, if the desktop is using KDE.

         public static boolean isKDE() {
                 return System.getenv("KDE_SESSION_VERSION") != null;
         }
        
Find the following working application.

DesktopUtil.java
package com.sample;

public class DesktopUtil {
 
 /**
  * 
  * @return true if the desktop is uding GNOME
  */
 public static boolean isGnome() {
  return System.getenv("GNOME_DESKTOP_SESSION_ID") != null;
 }

 /**
  * 
  * @return true if the desktop is using KDE
  */
 public static boolean isKDE() {
  return System.getenv("KDE_SESSION_VERSION") != null;
 }

}

DesktopUtilTest.java
package com.sample;

import com.sample.DesktopUtil;

public class DesktopUtilTest {
 public static void main(String args[]){
  System.out.println("Is Gnome : " + DesktopUtil.isGnome());
  System.out.println("Is KDE : " + DesktopUtil.isKDE());
 }
}

No comments:

Post a Comment