Change environment variable (JAVA_HOME) without logging out - java

I'm currently developing in 3 different Java versions and therefore often change my JAVA_HOME variable. However, I have to log out in order to get the variable updated. I update it via System Properties -> Advanced -> Environment Variables (Windows XP).
Surely there is a smarter/easier way of doing this.
I guess I could use the SET command in prompt, but that is not permanent and as I often use one Java version for a period of time I don't want to reset it every time I start the PC.
Any suggestions?

You don't have to log out to get an environment variable to stick, but you do have to restart whatever processes you're interested in. So for example, if you're using the command prompt, you will need to kill the command prompt and start a new one.

You could create 3 .bat files, each setting the JAVA_HOME variable and then starting your Development Environment.

I typically have separate entries in the environmental variables for separate JDKs or JREs:
JDK141
JDK156
JDK1624
And when needed change the JAVA_HOME variable to refer to one of those:
JAVA_HOME = %JDK156%
Add that to what Jon and Tim suggested and you get a very flexible way to switch versions.
Also remember that you can do
c:/program files/java/jdk156/bin/java.exe YourFolder/YourClass
As long as what you use isn't referring directly to JAVA_HOME you can force the use of a specific JRE/JDK if you specify the path in your command.
Cheers,
Wim

Have the latest version of the SDK installed (7 at this time), then use the Cross-compilation options of javac when compiling.

to change the Java home without restarting or re-logging:
Open Command prompt and type
"set JAVA_HOME=C:\Program Files (x86)\Java\your jdk folder" then press enter.
This will change the current environment settings until you restart or re-log to your windows account then it will read the settings from the windows environment settings.
If you want the new settings to be permanent go to System Properties -> Advanced -> Environment Variables and change the JAVA_HOME settings
I hope this helps

Related

Picked up _JAVA_OPTIONS in kali linux

Each time i execute java code i get this message:Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
This problem has solution for windows os but i can't find any solution for linux os.
I found a solution to this problem, I lost a lot of time of course. I'm definitely not sure what creates this situation, in my case it happened after installing NetBeans and changing the text display settings, I turned on anti-aliasin.
These settings are defined in these configuration files, just remove them:
/etc/X11/Xsession.d/90kali-themes.conf
/etc/environment.d/95kali-themes.conf
Java Option disabling in terminal
if your using java11-jdk in linux this might be working for disabling the java option from displaying in terminal.
_SILENT_JAVA_OPTIONS="$_JAVA_OPTIONS" && unset _JAVA_OPTIONS && alias java='java "$_SILENT_JAVA_OPTIONS"'
This is not warning, it is information.
It literally says that some settings were picked up from _JAVA_OPTIONS environmental variable. It is used to configure some switches "globally" for every java application that will be run in given environment. You must have this set either on system or user level. If you remove that variable, information will be gone.
ON Kali Linux ANDROID STUDIO
This information comes because an environment variable called _JAVA_OPTIONS setup by default. I solved it by Unseating the variable.
**To unset the value of this variable, use unset _JAVA_OPTIONS.
After that Android Studio will no longer display the information and Android Studio will be able to build projects that were previously failing to build..

Path variable not staying set

I'm working on setting up my development environment in Windows 7, installing Maven, etc. I've been running into path issues and have read, ad nauseum, other posts that pointed me in the right direction. My problem is, however, that my PATH variable (JAVA_HOME) isn't staying set.
When I try
mvn --version
I get
Error: JAVA_HOME not found in your environment.
Please set the JAVA_HOME variable in your environment to match the location
of your Java installation.
So, I set it
set JAVA_HOME=C:\Tools\Java
and then mvn --version works. But if I close and then reopen the Windows terminal I just end up getting the original error. Super frustrating.
I've also added that path to the Environmental Variables in the Systems Settings (with the semi-colon spacing, etc) Path section.
You're only setting it within that shell. This is perfectly normal behaviour for environment variables - not just on Windows, but on other OSes too.
I don't know about Windows 7, but on Windows 8 if I press the Windows key and start typing "Environment Variables" I get an option to open the control panel applet for editing the user or system environment variables. That's where you want to put it. The right dialog looks like this:
If the method above doesn't get to it, you can use the System Properties dialog, which has a button near the bottom for it:
This is how the shell works in Windows, Mac OSX and UNIX and I suspect all operating systems.
Each prompt has it's own environment which is separate to any other process you have running. You can set a variable temporarily, but this is not saved to disk or preserved because you might set it in a script but you don't want it to affect the whole system.
If you want to change an environment variable in Windows you need to do Start -> Right Click Computer -> Properties -> Advanced Setting -> Environment Variables -> Add Property.
On Linux, you add your SET line to the ~/.bashrc file
To set environment variables in Windows, go to the System control panel (the quickest way is to right click Computer in the Start Menu and select Properties), and select Advanced system settings, and then Environment Variables...
Go to System ->Advanced System Setting ->Environment Variables. In System Variables, Click on New and provide the Following:
Variable name as : JAVA_HOME
Variable Value as :E:/JdkInstallions/Jdk1.7 (as on my system)
This should resolve the problem you are getting.

Do I need to restart my system after setting JAVA in system environment's path variable?

I did the following steps but java does not seem to be working for me, do I need to restart my system if yes then why?
1. Right click My Computer->Advence Option->Environment Varibles->System Variables
2. variable : Path->Edit
3. Copy your jdk bin directory i.e.C:\Program Files\Java\jdk1.7.0_51\bin
4. Paste it after putting a semi-colon(;) in value section
i am getting below error while running java in cmd
C:\Users\User>java -version
Error: opening registry key 'Software\JavaSoft\Java Runtime Environment'
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.
No, but you will need to close and recreate any cmd windows, running java programs, or the like.
To check it's correct, open a new cmd window and type set -> review the information for PATH and JAVA_HOME.
You should set two things:
JAVA_HOME
PATH
The first lies under the "User variables for [your user name]" section. Add the JAVA_HOME variable and set it to where your jdk is installed. i.e. C:\Program Files\Java\jdk1.7.0_51\
The second lies under the "System variables" section. You should find the "path" variable, and edit it. Then, append ";%JAVA_HOME%\bin" (minus the quotes) to the end of the path variable.
After you have done this, save the variables and close that window clicking the "ok" button. Close down all instances of the command prompt (and any IDE you may be developing with such as Eclipse or NetBeans), and reopen one command prompt. Then, if you would like to test whether or not your changes worked and are in effect, try the following:
echo %JAVA_HOME%
This should output where you set your java home variable to.
echo %PATH%
At the end of what is output, you should see your java home\bin addition to the path variable
java -version
If you can run this command from the command line, it means that your environment was set up correctly and java is now in your path.
Not necessary. Quit and Open the command prompt again (if any) that runs the java process and type java -version to check if it installed successfully.
If you are using mac, source ~/.bashrc or source ~/.zshrc should refresh your environmental variable. No need to re-start the terminal even.
No, you don't need to restart your system.
However, you have to restart your command prompt application to update the changes done on Environment variables.
NO.
You don't need to restart the system, but just the application like cmd or any IDE you are using for java development (i.e Eclipse) need to restart.
And to confirm that the java path is set that you have mentioned in JAVA_HOME environment variable, you can open cmd and you can check with commands echo %JAVA_HOME% or echo %PATH%.
I reinstall the JDK and set the JAVA_HOME and PATH variable again. now it's working.
In my case under user variable section(for particularuser) JAVA_HOME set to jre not jdk path . I change it to JDK path , It worked for me
yes need to restart cmd prompt after env variable change

I'm trying to set my JAVA_HOME path to use my javac.exe?

I'm on Windows 7 and I'm trying to set my JAVA_HOME path to use PhoneGap on android, when i create a project using the command line "cmd" it tells me that it can't find javac.exe? although i'm sure that I've set the variable right
here's my stuff:
Variable name: JAVA_HOME
Variable value: C:\Progra~1\Java\jdk1.7.0_17\bin
(I've also tried to use: C:\Progra~1\Java\jdk1.7.0_17\bin\javac.exe)
My javac.exe location on my laptop C:\Program Files\Java\jdk1.7.0_17\bin\javac.exe
and it still tells me that it can't locate the javac.exe
What I type to create the project and i meet this problem is this:
C:\Users\user>cd C:\Users\user\Desktop\phonegap-2.8.1\lib\android\bin
C:\Users\user\Desktop\phonegap-2.8.1\lib\android\bin>create.bat
Cannot locate javac.exe using the PATH environment variable.
Retry after adding directory containing javac.exe to the PATH variable.
Remember to open a new command window after updating the PATH variable.
Visit http://java.oracle.com if you need to install Java (JDK).
Also, i've tries restarting the windows and closing the command prompt and redownloading the JDK.
Thank You.
cmd is using the values from the PATH variable to locate javac :
set "PATH=%PATH%;C:\Program Files\Java\jdk1.7.0_17\bin"
For other softwares (Tomcat, Maven, etc.) JAVA_HOME must indicate the root of your JDK :
set "JAVA_HOME=C:\Program Files\Java\jdk1.7.0_17\"
No need to restart Windows. When using set the changes are taken immediately.
If you choose to modify those variables in the Control Panel, you have to close and relaunch cmd for the changes to take effect.
You have to add java
C:\Program Files\Java\jdk1.7.0_17\bin
Note that your path may be different.
to your environment variables and restart your console.
You can find how to set your variables here:
http://www.itechtalk.com/thread3595.html
Set the JAVA_HOME Variable
Right-click the My Computer icon on your desktop and select Properties.
Click the Advanced system settings. Click the
Environment Variables button. Under System Variables, click New.
Enter the variable name as JAVA_HOME.
Enter the variable value as the installation path for the Java Development Kit(C:\Program Files\Java\jdk1.7.0_17).
Click OK.
Click Apply Changes.
You might need to restart windows.
Set the JAVA_HOME environmental variable to
C:\Program Files\Java\jdk1.7.0_17
Press
Win + Break > Advanced System Settings > Environment Variables > System Variables
Modify/set JAVA_HOME to the above. (No need to restart)
I already had this error when I tried to create a new phonegap project for android but unfortunately I don't remember how exactly I have fixed this.
I don't know if you have followed the phonegap documentation so here is the link : new project, especially the part 3B (be careful about ; in your PATH).
If you don't know what to do now, maybe you should do it again step by step.
May be a little old, but since this is the first goolge result, I might as well add a little something.
You need to create the JAVA_HOME variable AND edit the PATH variable, adding ;%JAVA_HOME%\bin (with a ;) so cmd and some other apps can actually use javac and the rest of the tools

reading environment variables from java using Eclipse

Everywhere I search, it says you can get an environment variable by using System.getenv(str).
It's not working for me. Here's what I am doing:
OS : Mac OS x 10.7
Java 1.6.x
If I do export abc=/hello/ in my terminal and then echo $abc, it gives me the variable. If I close the terminal, reopen it again and do echo $abc, it's gone. To overcome this, I edited my .bash_profile file and inserted export abc=/hello/. Close the terminal, do echo $abc and it works. So I understood that the env variable is permanent now.
Now if in my java console app, I print System.getenv("abc"), it returns null. What am I missing?
The reason that you needed to put the export in your .bash_profile is that setting environment variables in a shell only persist the variables in that shell, and - since you used export - to children of that shell, or in other words, other programs launched by that shell.
If you're running your java code from Eclipse, and you launch Eclipse from a shell with your environment variables set, then your program should see the added environment variables. To launch Eclipse from the shell, you'll need to use the OS X open command:
$ open /Applications/eclipse/Eclipse.app
Alternately, you can set the environment variables within your Eclipse project, and you'll need to do this if you're not launching Eclipse from a shell with the proper environment. In the Run Configurations dialog, look for a tab named Environment. Here you'll find a table for adding environment variables that will be passed to your program.
It's better to add the environment variables to the Run Configuration since that way they'll always be available to your project. Your code doesn't actually care where the environment variables are coming from, and adding them to the project is simpler, and will work the same way on different platforms.
Of course, when you run your program outside Eclipse, you'll need to make sure that the same environment variables exist in the shell where you e.g. run java.
Eclipse does not use the system's env variables unless launched directly from the shell (which is how it is generally launched, by clicking its icon). In that case you will have to explicitly set the required env variables in the environment tab of the run configuration of the program.
I too faced The same issue , I resolved it this way.
Open Terminal
cd to the folder where eclipse.app is located E.g cd /Users/Shared/eclipse/jee-2020-09
Type open Eclipse.app/
Eclipse will now open and will be able to access the system environment variables as well.
Check it using the code:
System.getenv().forEach((k, v) -> {
System.out.println("ENV : " + k + ":" + v);
});

Categories