5

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?

Community
  • 1
  • 1
missingfaktor
  • 90,905
  • 62
  • 285
  • 365

6 Answers6

3

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

vamsi
  • 93
  • 9
2

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.

DieterDP
  • 4,039
  • 2
  • 29
  • 38
1

First of all, Properties is a java class that is used to hold properties that maybe needed for your program. The basic properties that you are talking about are provided by the operating system. Not all of these can be changed. If you try, you will get a SecutrityException (You can't change the os.name for instance). The basic properties are read from the memory of the computer (basically) you can add additional variables to this by setting environment variables in the operating system you are using. Such as in Win95 you can add to the autoexec.bat the line: set BARTENDER_NAME=Carl This line can go in any batch file and BARTENDER_NAME will equal Carl until you reset it. In your java program If you add the line System.out.println(System.getProperty("BARTEDER_NAME")); You'll get Carl as the output. In the bash shell on Linux or Unix you'd use BARTENDER_NAME=Carl export BARTENDER_NAME

You can create your own set of properties for your java program and store them in a file and load them using the Properties load() method. Hope this helps

from

http://www.coderanch.com/t/387634/java/java/Permanently-setting-System-property

vamsi
  • 93
  • 9
0

Ok this is off the top of my head so it's extremely hacky and stuff.

Get hold of a process and run the command line command that will set the system variables. This isn't portable but it should suffice for short term till you find a better solution.

Thihara
  • 7,031
  • 2
  • 29
  • 56
0

Because there is not a standard solution for this, I would recommend you to use a Factory Pattern for this. It means something like:

envManager = null

if system is Windows

  `envManager = WindowsEnvManager`

else if system is Linux

  `envManager = LinuxEnvManager`

else if system is Mac

  `envManager = macEnvManager`

persistEnvironment(envManager);

and the persistEnvironment method would call the specific functions on EnvManager.

artaxerxe
  • 6,281
  • 21
  • 68
  • 106
  • You appear to have misunderstood the question. This is not an architecture/design question. What I am interested in is how I can implement these `XEnvManager`s, `persistEnvironment` etc. – missingfaktor Jun 19 '12 at 12:59
0

How about using Java Preferences API. That way you would store this kind of data in the Registry if you run on Windows. Simple tutorial here.

You can store the preferences per system or per user and the preferences are persistent as well as you desire.


Edit

Example:

package com.stackoverflow.Q11100967;

import java.util.prefs.Preferences;

/**
 * @author maba, 2012-06-20
 */
public class App {

    public static void main(String[] args) {
        Preferences preferences = Preferences.systemNodeForPackage(App.class);

        if (!preferences.getBoolean("installed", false)) {
            // Install the stuff...
            preferences.putBoolean("installed", true);
            preferences.put("version", "1.2.3");
        }
    }
}

On Windows the preferences will be stored at HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft/Prefs/com/stackoverflow/Q11100967.

In order for this to work you have to run your process with admin privileges or a similar approach.


Edit2

On Linux the preferences would be stored at /etc/.java/.systemPrefs/com/stackoverflow/Q11100967/ in a file called prefs.xml with the following content:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE map SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<map MAP_XML_VERSION="1.0">
  <entry key="installed" value="true"/>
  <entry key="version" value="1.2.3"/>
</map>
maba
  • 47,113
  • 10
  • 108
  • 118