Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set system variables from Java?

Tags:

java

I need to set certain system variables from within the program. My Google fu is failing me in finding any way to do it. How do I do it? (I am okay with hacky approaches. I need to be able to run this app on Windows, Linux, and Mac.)

Edit:

Adding here my comment from below the post, as it isn't readily visible there:

The best link I could found was this, and it sets the variables only in memory. They do not persist after the program exit.

Edit:

I am writing an installer and need to somehow record at system level that installation happened (along with paths to some directories). The next time user runs the setup, the installer will check if the variables already exist in the system, in which case a user will be given an appropriate warning.

If twiddling with environment variables is not a good idea, what will be the best approach to achieve the above?

like image 610
missingfaktor Avatar asked Jun 19 '12 12:06

missingfaktor


2 Answers

Use following methods of system class

// Get a system property
String dir = System.getProperty("user.dir");

// Set a system property
String previousValue = System.setProperty("application.property", "newValue");

for more details reffer

http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CF8QFjAA&url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fessential%2Fenvironment%2Fsysprop.html&ei=oHLgT6agKcborAf_3L3-DA&usg=AFQjCNGWSWRjk3ityPQqreuwx_O7Bp7kdg&sig2=Y1tfYzdXAmNX-hpB8Z64kw

like image 151
vamsi Avatar answered Oct 11 '22 07:10

vamsi


If you want your environment variables to persist after your program ends, I would suggest you use the Properties class. It can be persisted to a file very easily, and vice versa.

like image 40
DieterDP Avatar answered Oct 11 '22 07:10

DieterDP