23 Mart 2021 Salı

System Sınıfı genv() vs getProperty

Giriş
getenv() işletim sistemi tarafından sağlanan ortam değişkenlerini (environment variables) döndürür. 
- getProperty() JVM'i çalışırken belirtilen değişkenleri döndürür.


getenv metodu
Ortam değişkenine erişim sağlar. Döndürülen map değiştirilemez. Açıklaması şöyle
Returns an unmodifiable string map view of the current system environment.
Şöyle yaparız.
 Map<String, String> env = System.getenv();
Şöyle yaparız.
System str = System.getenv("...");
getProperty metodu
Açıklaması şöyle
java.lang.System.getProperty()’ API underlyingly uses ‘java.util.Hashtable.get()’ API. Please be advised that ‘java.util.Hashtable.get()’ is a synchronized API. It means only one thread can invoke the ‘java.util.Hashtable.get()’ method at any given time. If a new thread tries to invoke ‘java.util.Hashtable.get()’ API when the first thread is still executing it, the new thread will be put in a BLOCKED state. When a thread is in the BLOCKED state, it won’t be able to progress forward. Only when the first thread completes executing the ‘java.util.Hashtable.get()’ API, new thread will be able to progress forward. Thus if ‘java.lang.System.getProperty()’ or ‘java.util.Hashtable.get()’ is invoked in critical code paths, it will impact the response time of the transaction.
Bu metod sadece String döner. Eğer bir property değerini Integer olarak okumak istersek Integer.getInteger() kullanılır

Örnek
Elimizde şöyle bir JVM parametresi olsun
“-DappName=buggyApp”
Şöyle yaparız
System str = System.getProperty("appName");
java.ext
extension dizinleri şöyle alınır.
System str = System.getProperty("java.ext.dirs");
Çıktı olarak şunu alırız.
D:\glassfish4\jdk7\jre\lib\ext;C:\windows\Sun\Java\lib\ext
java.home
Şöyle yaparız. D:\jdk1.8.0\jre gibi bir çıktı verir.
System str = System.getProperty("java.home");
java.version
Java sürümü şöyle alınır.
System str = System.getProperty("java.version")
1.8.0_91 gibi bir sonuç alırız.

os.name
Şöyle yaparız
public final class OsHelper {

  public static final String OS = System.getProperty("os.name").toLowerCase();

  public static boolean isLinux() {
    return OS.contains("nux");
  }

  public static boolean isUnixFamily() {
    return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix"));
  }
  
  public static boolean isMac() {
    return (OS.contains("mac") || OS.contains("darwin"));
  }
  
  public static boolean isWindows() {
    return OS.contains("windows");
  }
}
sun
Sistemin 32 veya 64 bit olduğunu anlamak için şöyle yaparız.
System.getProperty("sun.arch.data.model")
user.home
Kullanıcının home dizini şöyle alınır. Linux'ta /home/myuser/Downloads gibi bir şey verir.
File downloads = new File(System.getProperty("user.home"), "Downloads");
Daha kötü bir yöntemle şöyle yaparız.
File directory = new File(System.getProperty("user.dir") 
         + System.getProperty("file.separator")+ "Images";
user.name
Kullanıcının adı şöyle alınır.
String userName = System.getProperty("user.name"); //platform independent

Hiç yorum yok:

Yorum Gönder