Path variable not staying set - java

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.

Related

How to set java path properly on windows 8 machine?

I feel really bad I need to ask here but I've struggled with this issue since morning :)
All I need is to set java 6 jdk to path. I set this to path in system environmental props:
C:\Program Files\Java\jdk1.6.0_45\bin;C:\java\maven\apache-maven-3.2.5\bin
and this to JAVA_HOME:
C:\Program Files\Java\jdk1.6.0_45
However, java -version still prints out: java version "1.8.0_25".
Thank you for any answer and have a nice rest of day :)
Windows 8
Drag the Mouse pointer to the Right bottom corner of the screen
Click on the Search icon and type: Control Panel
Click on -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
Close the window.
Reopen Command prompt window, and run your java code.
refer here

Java command not found

I'm trying to get the java command to work on my computer. Whenever I try to use it, I get the error "'java' is not recognized as an internal or..."
What I've done
So far I've traced the issue through my system environment variables, which has a variable PATH that links to "C:\ProgramData\Oracle\Java\javapath" or javapath for short.
There are 3 files in the javapath folder, and if you click on them a popup says:
But when I examine the properties of the shortcut and click "open folder location" it sends me to the right place (?? what is this).
Just in case, I tried to recreate the shortcut; I called it java2 and it now works (opens a command box for a split second and closes) when I click on it. However, testing it in command prompt with the line "java2 -version" returns "'java2' is not recognized as an internal or external..."
Details
I just installed java 8, and I've verified that it was installed correctly
I'm running a 64 bit windows 7 OS
Have you checked your environment variables for Windows? When you installed the JDK/JRE the directory should have been added to the PATH variable. The directory added needs to contain the java.exe executable. It should be located in C:\Program Files\Java or something like that if you installed it using the default settings. Once you find that directory find the bin directory inside it.
In order to check that:
Right click My Computer and hit Properties
On the left side hit Advanced System Settings
Hit the Advanced tab and then Environment Variables down at the bottom
Find the PATH variable and then hit edit. Confirm that the location of the JDK /bin directory is there, if it is not then add it.
Also, if the JAVA_HOME variable isn't already set, this may be a good idea.

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);
});

Change environment variable (JAVA_HOME) without logging out

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

Categories