Get all system properties in java

Return all system properties in Java

To return all the system properties in Java, firstly use the System.getProperties() method.

java.util.Properties prop = System.getProperties();

After that, use the list() method to list all of them.

Example

public class MainClass   public static void main(String[] args)  java.util.Properties prop = System.getProperties(); prop.list(System.out); > >

Output

java.runtime.name=OpenJDK Runtime Environment sun.boot.library.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0. java.vm.version=25.141-b16 java.vm.vendor=Oracle Corporation java.vendor.url=http://java.oracle.com/ path.separator=: java.vm.name=OpenJDK 64-Bit Server VM file.encoding.pkg=sun.io user.country=US sun.java.launcher=SUN_STANDARD sun.os.patch.level=unknown java.vm.specification.name=Java Virtual Machine Specification user.dir=/home/cg/root/3757524 java.runtime.version=1.8.0_141-b16 java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment java.endorsed.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0. os.arch=amd64 java.io.tmpdir=/tmp line.separator= java.vm.specification.vendor=Oracle Corporation os.name=Linux sun.jnu.encoding=UTF-8 java.library.path=/home/cg/root/GNUstep/Library/Librari. java.specification.name=Java Platform API Specification java.class.version=52.0 sun.management.compiler=HotSpot 64-Bit Tiered Compilers os.version=3.10.0-862.9.1.el7.x86_64 user.home=? user.timezone= java.awt.printerjob=sun.print.PSPrinterJob file.encoding=UTF-8 java.specification.version=1.8 user.name=? java.class.path=/home/cg/root/GNUstep/Library/Librari. java.vm.specification.version=1.8 sun.arch.data.model=64 java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0. sun.java.command=MainClass java.specification.vendor=Oracle Corporation user.language=en awt.toolkit=sun.awt.X11.XToolkit java.vm.info=mixed mode java.version=1.8.0_141 java.ext.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0. sun.boot.class.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0. java.vendor=Oracle Corporation file.separator=/ java.vendor.url.bug=http://bugreport.sun.com/bugreport/ sun.cpu.endian=little sun.io.unicode.encoding=UnicodeLittle

Samual Sam

Learning faster. Every day.

Источник

Java – How to display all System properties

In Java, you can use System.getProperties() to get all the system properties.

 Properties properties = System.getProperties(); properties.forEach((k, v) -> System.out.println(k + ":" + v)); // Java 8 

1. Example

 package com.mkyong.display; import java.util.Properties; public class DisplayApp < public static void main(String[] args) < Properties properties = System.getProperties(); // Java 8 properties.forEach((k, v) ->System.out.println(k + ":" + v)); // Classic way to loop a map //for (Map.Entry entry : properties.entrySet()) < // System.out.println(entry.getKey() + " : " + entry.getValue()); //>// No good, output is truncated, long lines end with . //properties.list(System.out); > > 
 sun.desktop:windows awt.toolkit:sun.awt.windows.WToolkit java.specification.version:10 file.encoding.pkg:sun.io sun.cpu.isalist:amd64 sun.jnu.encoding:Cp1252 java.class.path:D:\maven-examples\maven-profiles\target\classes; java.vm.vendor:"Oracle Corporation" sun.arch.data.model:64 user.variant: java.vendor.url:http://java.oracle.com/ user.timezone: os.name:Windows 10 java.vm.specification.version:10 sun.java.launcher:SUN_STANDARD user.country:MY sun.boot.library.path:C:\opt\Java\jdk-10\bin sun.java.command:com.mkyong.password.DisplayApp jdk.debug:release sun.cpu.endian:little user.home:C:\Users\mkyong user.language:en java.specification.vendor:Oracle Corporation java.version.date:2018-04-17 java.home:C:\opt\Java\jdk-10 file.separator:\ java.vm.compressedOopsMode:Zero based line.separator: java.specification.name:Java Platform API Specification java.vm.specification.vendor:Oracle Corporation java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment user.script: sun.management.compiler:HotSpot 64-Bit Tiered Compilers java.runtime.version:10.0.1+10 user.name:mkyong path.separator:; os.version:10.0 java.runtime.name:Java(TM) SE Runtime Environment file.encoding:UTF-8 java.vm.name:Java HotSpot(TM) 64-Bit Server VM java.vendor.version:18.3 java.vendor.url.bug:http://bugreport.java.com/bugreport/ java.io.tmpdir:C:\Users\mkyong\AppData\Local\Temp\ java.version:10.0.1 user.dir:D:\maven-examples\maven-profiles os.arch:amd64 java.vm.specification.name:Java Virtual Machine Specification java.awt.printerjob:sun.awt.windows.WPrinterJob sun.os.patch.level: java.library.path:C:\opt\Java\jdk-10\bin;C:\WINDOWS\Sun\Java\bin; java.vendor:Oracle Corporation java.vm.info:mixed mode java.vm.version:10.0.1+10 sun.io.unicode.encoding:UnicodeLittle java.class.version:54.0 Process finished with exit code 0 

2. Sorting

Example to display all the system properties in Alphabetical order.

 package com.mkyong.display; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; public class DisplayApp < public static void main(String[] args) < Properties properties = System.getProperties(); // Thanks Java 8 LinkedHashMapcollect = properties.entrySet().stream() .collect(Collectors.toMap(k -> (String) k.getKey(), e -> (String) e.getValue())) .entrySet().stream().sorted(Map.Entry.comparingByKey()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); collect.forEach((k, v) -> System.out.println(k + ":" + v)); > > 
 awt.toolkit:sun.awt.windows.WToolkit file.encoding:UTF-8 file.encoding.pkg:sun.io file.separator:\ java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment java.awt.printerjob:sun.awt.windows.WPrinterJob //. sun.cpu.endian:little sun.cpu.isalist:amd64 sun.desktop:windows sun.io.unicode.encoding:UnicodeLittle sun.java.command:com.mkyong.password.DisplayApp sun.java.launcher:SUN_STANDARD sun.jnu.encoding:Cp1252 sun.management.compiler:HotSpot 64-Bit Tiered Compilers sun.os.patch.level: user.country:MY user.dir:D:\maven-examples\maven-profiles user.home:C:\Users\mkyong user.language:en user.name:mkyong user.script: user.timezone: user.variant: 

References

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Источник

Java System Properties

Java maintains a Set of system properties that can be accessed in the runtime by executing programs. Each system property is a key-value pair. For example, one such system property is “java.version”=”1.7.0_09“.

We can retrieve all the system properties via System.getProperties() or we can also retrieve individual property via System.getProperty(key) method.

Please note that access to system properties can be restricted by the Java security manager and policy file. By default, Java programs have unrestricted access to all the system properties.

1. Java System Properties List

The following is a list of important system properties in each category.

1.1. Runtime Environment Properties

java.home JRE home directory, e.g., “ C:\Program Files\Java\jdk1.7.0_09\jre “.
java.library.path JRE library search path for search native libraries. It is usually but not necessarily taken from the environment variable PATH.
java.class.path JRE classpath e.g., ‘.’ (dot – used for current working directory).
java.ext.dirs JRE extension library path(s), e.g, “ C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext;C:\Windows\Sun\Java\lib\ext “.
java.version JDK version, e.g., 1.7.0_09 .
java.runtime.version JRE version, e.g. 1.7.0_09-b05 .

1.2. File System Properties

file.separator symbol for file directory separator such as ‘d:\test\test.java’ . The default is ‘\’ for windows or ‘/’ for Unix/Mac.
path.separator symbol for separating path entries, e.g., in PATH or CLASSPATH . The default is ‘;’ for windows or ‘:’ for Unix/Mac.
line.separator symbol for end-of-line (or new line). The default is «\r\n» for windows or «\n» for Unix/Mac OS X.
user.name the user’s name.
user.home the user’s home directory.
user.dir the user’s current working directory.

1.4. Operation System Properties

os.name the OS’s name, e.g., “ Windows 7 “.
os.version the OS’s version, e.g., “ 6.1 “.
os.arch the OS’s architecture, e.g., “ x86 “.

2. Get the Value of a System Property

As discussed earlier, You can get the list of all the system properties via System.getProperties() or also retrieve individual property via System.getProperty(key) .

2.1. List of all System Properties

Properties pros = System.getProperties(); pros.list(System.out);

2.2. Get a system property value by its key

System.getProperty("java.home"); 

In Java, you can set a custom system property either from the command line or from the application code itself.

In the given example, the application will be able to access the property with key custom_key . It’s value will be available as custom_value .

$ java -Dcustom_key="custom_value" application_launcher_class

Similar to the above example, after executing this code, the application will be able to access the property with key custom_key . It’s value will be available as custom_value .

System.setProperty("custom_key", "custom_value");

That’s all for this basic tutorial for reading and writing system properties in java.

Источник

List all System Properties in Java

This example show how you can list all system properties of a running java application. You can use this as a reference to look up which properties are by default available.

You can easily print all the system properties to the System.out console.

package com.memorynotfound; public class ListAllSystemProperties < public static void main(String. args) < System.getProperties().list(System.out); >>

Loop over System Properties

The Sytem.getProperties().propertyNames(); returns an enumeration on which you can loop over all the system properties.

package com.memorynotfound; import java.util.Enumeration; import java.util.Properties; public class ListAllSystemProperties < public static void main(String. args) < Properties systemProperties = System.getProperties(); Enumeration enuProp = systemProperties.propertyNames(); while (enuProp.hasMoreElements()) < String propertyName = (String) enuProp.nextElement(); String propertyValue = systemProperties.getProperty(propertyName); System.out.println(propertyName + ": " + propertyValue); >> >

Output all System Properties

java.runtime.name: Java(TM) SE Runtime Environment sun.boot.library.path: /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib java.vm.version: 25.20-b23 user.country.format: BE gopherProxySet: false java.vm.vendor: Oracle Corporation java.vendor.url: http://java.oracle.com/ path.separator: : java.vm.name: Java HotSpot(TM) 64-Bit Server VM file.encoding.pkg: sun.io user.country: US sun.java.launcher: SUN_STANDARD sun.os.patch.level: unknown java.vm.specification.name: Java Virtual Machine Specification user.dir: /Users/memorynotfound/Documents/projects/memorynotfound/java/tutorials java.runtime.version: 1.8.0_20-b26 java.awt.graphicsenv: sun.awt.CGraphicsEnvironment java.endorsed.dirs: /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/endorsed os.arch: x86_64 java.io.tmpdir: /var/folders/ts/tv2s4k357c79jmw4_rkbt7b40000gn/T/ line.separator: java.vm.specification.vendor: Oracle Corporation os.name: Mac OS X sun.jnu.encoding: UTF-8 java.library.path: /Users/memorynotfound/Library/Java/Extensions:/Library/Java/Extensions:/[. ] java.specification.name: Java Platform API Specification java.class.version: 52.0 sun.management.compiler: HotSpot 64-Bit Tiered Compilers os.version: 10.10.2 http.nonProxyHosts: local|*.local|169.254/16|*.169.254/16 user.home: /Users/memorynotfound user.timezone: Europe/Brussels java.awt.printerjob: sun.lwawt.macosx.CPrinterJob file.encoding: UTF-8 java.specification.version: 1.8 user.name: memorynotfound java.class.path: /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/lib/[. ] java.vm.specification.version: 1.8 sun.arch.data.model: 64 java.home: /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre sun.java.command: com.memorynotfound.ListAllSystemProperties java.specification.vendor: Oracle Corporation user.language: en awt.toolkit: sun.lwawt.macosx.LWCToolkit java.vm.info: mixed mode java.version: 1.8.0_20 java.ext.dirs: /Users/memorynotfound/Library/Java/Extensions:/Library/[. ] sun.boot.class.path: /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/[. ] java.vendor: Oracle Corporation file.separator: / java.vendor.url.bug: http://bugreport.sun.com/bugreport/ sun.cpu.endian: little sun.io.unicode.encoding: UnicodeBig socksNonProxyHosts: local|*.local|169.254/16|*.169.254/16 ftp.nonProxyHosts: local|*.local|169.254/16|*.169.254/16 sun.cpu.isalist: 

References

Источник

Читайте также:  Copy on write php
Оцените статью