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.
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.
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).
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 ...
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
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).
Related
What is the proper (2021 way) of creating a permanent environment variable on a Mac (macOS Big Sur) and then use it within a Java project.
There are many very old posts regarding this topic. None of them seem to work properly nowadays.
How to add a permanent environment value (through terminal)?
And how can I use it in a Java code?
I'm also not sure how I was able to add my testvar=testvalue to the list, because I tried so many files (although it seems none of them worked), by adding export testvar=testvalue to the following files:
/etc/paths
~/.bashrc
~/.bash_profile
~/.profile
/etc/profile
Also after inserting it into each file I used source {file}.
So at this point I have no idea which is the proper way to create and have it permanently, and being able to use it in my Java code.
So far, I can print the variables into the terminal like this:
printenv
My variables are getting listed, example:
testvar=testvalue
In my Java code, I get null when using:
System.getenv("testvar")
However using an other variable names that were not created by me, but the macOS system (eg. "USER") prints the value as expected.
macOS Big Sur uses zsh as the default login shell and interactive shell.
If you’re using a Bash profile, such as to set environment variables, aliases, or path variables, you should switch to using a zsh equivalent.
For example:
.zprofile is equivalent to .bash_profile and runs at login, including over SSH
.zshrc is equivalent to .bashrc and runs for each new Terminal session
You can create .zprofile and enter the enter the environment variables there.
Reference
you can edit zprofile using the following command
sudo nano ~/.zprofile
and add your PATH variable.
# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH
to add multiple values to the PATH variable, just add more PATH keys. For example, this is how I added multiple path variables in my M1 mac Monterey
# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
PATH="/Users/<name>/.local/bin"
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export PATH
This depends on the shell which you are using. For Big Sur, the standard shell is zsh, which might explain why .bashrc and other bash-related configuration files did not work. If you want to set environment variables for your account in zsh, try to create a ~/.zshenv file and put the variable declarations there.
See also: http://zsh.sourceforge.net/Doc/Release/Files.html#Files
I would like to delete the 'CATALINA_HOME' using java code. Please let me know if someone know this type of problem.
Suppose already set 'CATALINA_HOME' = C:\Program Files\Apache
While installing the software, my software should use the my customize tomcat7
and that store in different location.
'CATALINA_HOME' = D:\Installer\Apache
while running the software it find the tomcat this location 'CATALINA_HOME' = C:\Program Files\Apache but my location is
'CATALINA_HOME' = D:\Installer\Apache
I would like to delete the 'CATALINA_HOME' using java code.
Sorry, but you can't. A program (at least on Linux / Unix / Solaris) cannot modify the environment of its parent shell. At most, a program only sees a snapshot of its parent's environment variables ... as they were when the parent forks the child process.
(Windows variables work a bit differently, but I think the same restriction applies ...)
The only thing that a Java can do (reliably / portably) is to change the environment variables that a child process of your Java process will inherit. You can do that using ProcessBuilder.
Now in theory, a program could attempt to modify the source of the variables' values:
On a Unix like system, they often come from one of the shell's RC files; e.g. $HOME/.profile, $HOME/.bashrc and so on ... depending on the shell. Or the variable may have been set in the "init" script that launches Tomcat.
On a Windows system, the may have come from the registry, or a BAT file or ...
The problem is two-fold:
It is impossible to be entirely sure where the value really comes from.
Even if you can be sure, changing the source of the value won't affect the respective shell's current value for the variable.
Note: this is not a shortcoming of Java. You would have the same problem when coding in C, C++, Python ...
I have noticed that some of my environment variables are not being picked up by the JVM.
In my .bash_profile I defined the following:
IO_HOME='some_value'
export IO_HOME
and by doing in shell:
echo $IO_HOME
I get the correct result.
But neither System.getProperties() nor System.getenv() is showing this variable being set. I tried both Java 6 and Java 7.
Is there something I am missing?
Exporting environment to spawned processes is pretty stable; if System.getenv() is not including a variable then it is because it is not in the environment. A couple of things to check, both relating to how the process is started:
Are you starting the java process from an environment where the variable is exported? For example, if it is in your .bash_profile and you are executing the java program from a menu or desktop then you have to log out and log in after adding it in .bash_profile for your desktop to see the variable.
Is the variable explicitly removed from environment for the process? ProcessBuilder allows this, as do most of all APIs that spawn processes.
One thing to try is to start the process from command line shell, after ensuring the variable is exported in that shell.
From Windows, I recently saw some crazy behaviour where IntelliJ refused to show all env vars from System.getenv() after setting either user or system env vars. The trick was to launch IntelliJ from a DOS box. For Windows users: Maybe a reboot (or logoff/logon) will fix the issue.
In mac OSX and in Linux CentOS, I insert a new system environment variable (i.e. "MYAPP") using .bashrc & .bash_profile. I even restarted my laptop (mac) and my server (linux).
When I use the command line "env", that environment variable showed with the correct value. But somehow every time I try to get it in a Java app (desktop app or web app or EJB or servlet any other java app) in either mac or linux, that environment variable ("MYAPP") is not retrieved.
I tried to iterate through the entire environment variables that Java can retrieve and it turns out that it retrieves every environment variables other than "MYAPP". This is very odd.
Anyone know how to solve this?
Did you export MYAPP=...? Exporting the variable makes it available to child processes, like java being run by your shell.
In Linux, if you only set the variable (or export it) in a bash session, it will be available to a kind of "sub" session, which is only available to the command you just executed, and nothing else.
You could probably use the dot operator in bash (also called "source" command). From the page:
When a script is run using `source' it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.
So you could try doing . export VARIABLE=value, and then running your java program. This is similar to setting a variable in a Windows terminal, and then opening a new terminal and expecting the env var to be there. It won't.
This way, you are telling bash "this command should be available in this specific session (the session's process)". OTherwise you are telling it "set this env var for the bash session that will end after I run this export command" thus, it won't exist when you run your Java program.
After having defined and exported the environment variable. Launch your IDE from the same Terminal.
Try to write
"$System.env.STOREPWD"
I do most of my work against JDK 1.5 - but occasionally I have to change to 1.6. it is a bit painful to have to manually go and change my 'JAVA_HOME' system variable whenever I need to work on a project specific to one or the other (and no, Eclipse doesn't play well in these scenarios - trust me...I've tried.)
I'm looking for a registry script or windows shell script or for that matter any means by which I can "toggle" this system variable with something that is easy to run.
I've messed with the 'set' command, but that only sets the variable for that particular command instance - not globally.
Thanks in advance.
EDIT #1:
Points of advise:
Use the JAVA_HOME variable in your path variable as well, that way you only have to change the JAVA_HOME (which is used in many projects anyways [maven, ant, etc])
Write the command into a couple batch scripts for easy use
When you make the change the windows command session will not reflect it right away. You must close and reopen it.
You could use setx for that purpose
Like so:
setx /M JAVA_HOME "C:\Program Files (x86)\Java\jdk1.6.0_17"