
1. Overview
Sometimes, working with Java applications, we need to access the values of system properties and environment variables.
In this tutorial, we’ll learn how to retrieve the user name from a running Java application.
2. System.getProperty
One way to get information about the user, more precisely, its name, we can use the System.getProperty(String). This method takes the key. Usually, they’re uniform and predefined, such as java.version, os.name, user.home, etc. In our case, we’re interested in the user.name:
String username = System.getProperty("user.name");
System.out.println("User: " + username);
An overloaded version of this method System can protect us from non-existent properties.getProperty(String, String), which takes a default value.
String customProperty = System.getProperty("non-existent-property", "default value");
System.out.println("Custom property: " + customProperty);
Aside from working with predefined system properties, this method allows us to get the values of the custom properties we’ve passed with the -D prefix. If we run our application with the following command:
java -jar app.jar -Dcustom.prop=`Hello World!`
Inside an application, we can use this value
String customProperty = System.getProperty("custom.prop");
System.out.println("Custom property: " + customProperty);
This can help us make flexible and extensible codebase by passing values at the startup.
Additionally, it’s possible to use System.setProperty, but changing crucial properties might have unpredictable effects on the system.
3. System.getenv
Additionally, we can use environment variables to get a user name. Usually, it can be found by either USERNAME or USER:
String username = System.getenv("USERNAME");
System.out.println("User: " + username);
The environment variables are read-only but also provide an excellent mechanism to get information about the system the application runs in.
4. Conclusion
In this article, we discussed a couple of ways to get the required information about the environment. Environment variables and properties are a convenient way to gain more information about the system. Also, they allow custom variables to make an application more flexible.
As usual, all the examples are available over on GitHub.
Commercials Cooperation Advertisements:
(1) IT Teacher IT Freelance

立刻註冊及報名電腦補習課程吧!
电子计算机 -教育 -IT 電腦班” ( IT電腦補習 ) 提供一個方便的电子计算机 教育平台, 為大家配對信息技术, 電腦 老師, IT freelance 和 programming expert. 讓大家方便地就能找到合適的電腦補習, 電腦班, 家教, 私人老師.
We are a education and information platform which you can find a IT private tutorial teacher or freelance.
Also we provide different information about information technology, Computer, programming, mobile, Android, apple, game, movie, anime, animation…
(2) ITSec
www.ITSeceu.uk
Secure Your Computers from Cyber Threats and mitigate risks with professional services to defend Hackers.
ITSec provide IT Security and Compliance Services, including IT Compliance Services, Risk Assessment, IT Audit, Security Assessment and Audit, ISO 27001 Consulting and Certification, GDPR Compliance Services, Privacy Impact Assessment (PIA), Penetration test, Ethical Hacking, Vulnerabilities scan, IT Consulting, Data Privacy Consulting, Data Protection Services, Information Security Consulting, Cyber Security Consulting, Network Security Audit, Security Awareness Training.
Contact us right away.
Email (Prefer using email to contact us):
SalesExecutive@ITSec.vip