How to Set System Property in Java

Sheeraz Gul Feb 02, 2024
How to Set System Property in Java

Today’s tutorial teaches us about System properties. It also educates about how to set system properties in Java.

Set System Property in Java

Java provides a set of system properties for its operations. The system property is a pair of key = value for example, java.version = 1.7.0_09.

Java has a long list of built-in properties, which can be retrieved using the method System.getProperties(). Now, what if we want to set a custom property?

Java also provides a method to set a custom property based on your requirement. For example, we can use System.setProperties(), or we can set a custom property using the command prompt. Here are the two methods:

Use Java Code:

System.setProperty("Custom_Key", "Custom_Value");

Use Command Prompt:

java -DCustom_Key="Custom_Value" application_launcher_class

Both the methods above take two parameters, one the custom key and the other the value of the key. Let’s try to set a custom property using Java code.

Example Code:

package delftstack;

import java.util.Properties;

public class Example {
  public static void main(String[] a) {
    System.setProperty("Delftstack_Key", "Delftstack_Value");
    // List all System properties
    Properties System_Properties = System.getProperties();
    System_Properties.list(System.out);
  }
}

The code above will set a custom property and show it in the list of all system properties.

Output:

-- listing properties --
java.specification.version=17
sun.cpu.isalist=amd64
sun.jnu.encoding=Cp1252
java.class.path=C:\Users\Sheeraz\eclipse-workspace\De...
java.vm.vendor=Oracle Corporation
sun.arch.data.model=64
user.variant=
java.vendor.url=https://java.oracle.com/
java.vm.specification.version=17
os.name=Windows 10
sun.java.launcher=SUN_STANDARD
user.country=US
sun.boot.library.path=C:\Program Files\Java\jdk-17.0.2\bin
sun.java.command=delftstack.Example
jdk.debug=release
sun.cpu.endian=little
user.home=C:\Users\Sheeraz
user.language=en
java.specification.vendor=Oracle Corporation
jdk.module.path=C:\Users\Sheeraz\OneDrive\Desktop\New...
java.version.date=2022-01-18
java.home=C:\Program Files\Java\jdk-17.0.2
file.separator=\
java.vm.compressedOopsMode=32-bit
line.separator=

java.vm.specification.vendor=Oracle Corporation
java.specification.name=Java Platform API Specification
user.script=
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
java.runtime.version=17.0.2+8-LTS-86
user.name=Sheeraz
path.separator=;
os.version=10.0
java.runtime.name=Java(TM) SE Runtime Environment
file.encoding=Cp1252
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
Delftstack_Key=Delftstack_Value
java.vendor.url.bug=https://bugreport.java.com/bugreport/
java.io.tmpdir=C:\Users\Sheeraz\AppData\Local\Temp\
java.version=17.0.2
user.dir=C:\Users\Sheeraz\eclipse-workspace\Demos
os.arch=amd64
java.vm.specification.name=Java Virtual Machine Specification
sun.os.patch.level=
native.encoding=Cp1252
java.library.path=C:\Program Files\Java\jdk-17.0.2\bin;...
java.vm.info=mixed mode, sharing
java.vendor=Oracle Corporation
java.vm.version=17.0.2+8-LTS-86
sun.io.unicode.encoding=UnicodeLittle
java.class.version=61.0
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook