3

Using Java, How can I add environment variable permanently to the existing env variables.

so that when I do a restart operation for windows or Linux, this environment variable is still there.

Oleksi
  • 12,583
  • 4
  • 51
  • 76
becks
  • 2,468
  • 7
  • 29
  • 62
  • Which OS are you using? Or are you looking for an OS agnostic solution? – Dave Aug 27 '12 at 16:15
  • 1
    Are you trying to write some system management tool using Java, or are you trying to modify configuration for your own app? Because if it is the latter, I would say move you configuration to someplace else. – Dilum Ranatunga Aug 27 '12 at 16:31
  • following up on what @DilumRanatunga said, the Preferences API is a great way to save per-user config in a system agnostic way. – jtahlborn Aug 27 '12 at 16:34
  • ... but the preferences API make no guarantees about anything. Very unfortunate. – Dilum Ranatunga Aug 27 '12 at 16:36
  • I'm using Windows but I need this functionality for both platforms – becks Aug 27 '12 at 16:37

5 Answers5

1

You might want to take a look at this.

In Windows you can set a Path Variable from command line so it should do the trick.

I realize this is only applicable to Windows.

Community
  • 1
  • 1
Niklas
  • 399
  • 1
  • 14
0

Not in any cross platform sort of way. In Linux, these are typically controlled via shell init scripts. You would have to edit one of those (which one depends on the user, system, and shell type). In Windows, this is controlled via system configuration (i'd imagine there are some windows specific APIs to modify those).

jtahlborn
  • 50,774
  • 5
  • 71
  • 112
0

coppy the path of jdk upto C:\Program Files\Java\jdk1.6.0\bin from program and past in user variables and put ;.; at the end and give name . and in system variables click on new and enter the name and past the path....and save ...

0

go to command prompt .. to check current paths >echo %path% to set path >set path="C:\Program Files\Java\jdk1.6.0\bin" enter ok now check and run java program

-2

Environment variables are platform specific. Windows stores them in Registry.

*In the registry the User environment variables are stored at HKEY_CURRENT_USER\Environment and the System environment variables are stored at HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment (from http://demins.blogspot.co.il/2007/10/where-does-windows-xp-store-evrironment.html)*

There are a lot of ways to access windows registry from java. You can for example execute command line using utility named reg that has a reach command line. You can also use one of interoparability APIs like JaWin, Jinterop, Jintegra. You can also refer to my solution explained here.

On linux you can use command line like export MYVAR=myvalue. I mean execute this command line from java using Runtime.exec() or ProcessBuilder. The problem is that this variable will not become really persistent. It will be visiable for all users until the computers is restarted. To make it really persistent you have to modify user login script (e.g. bashrc file for most linux systems if users's default shell is bash).

AlexR
  • 109,181
  • 14
  • 116
  • 194
  • 1
    for the linux solution, running `export` from a sub-process will not influence anything except subsequent processes run in that same sub-process (it won't influence anything else done by the current user, and certainly not any other users). in fact, that command won't even run as is because you need a shell to execute it. – jtahlborn Aug 27 '12 at 16:27
  • @jtahlborn, sorry that I forgot to mention to run command as `/bin/sh export ...` or something like this. I believe that this detail can be discovered by OP himself. – AlexR Aug 27 '12 at 16:45
  • that addresses one minor part of what i said, but none of the major points. – jtahlborn Aug 27 '12 at 16:49