Why am I able to execute a program that is not in my PATH environment variable?
I was wondering why the command java -version
is globally accessible?
I could run it from any directory and its working:
How does it work?
This is what my system PATH
variable looks like:
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;
%SystemRoot%\system32;
%SystemRoot%;
%SystemRoot%\System32\Wbem;
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
As for me, java.exe
is located in %programfiles%\java\jre7\bin
Typically, there is a group of directories where executable files that are repeatedly used are to be found by your Windows, but in pratice there is no specific reason to use a command as global or not. Developers used to include it as "global" wherever they want to, they are free to do it. If you want to use any command as "global" you will need to add your command as a "Windows Enviroment Variable". To do this, you can use the folowing methods:
Adding manually with "Windows System properties":
Using the add-on tool Setx.exe:
It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. For example, to add a folder C:\New Folder to the path, the command would be
setx path "%PATH%;C:\New Folder"
- Scripts for Listing Environment Variables:
The "Set" command can be used in a command prompt together with a redirection to a text file to make a list of the current environment variables. The command might be
set > C:\env_list.txt
The file name "C:\env_list.txt" can be replaced by any of your choice.
Microsoft also has a VBScript that lists environment variables on this page.
- Registry Keys for Environment Variables:
For those who are experienced with editing the Registry, there is another way to make changes in environment variables. User environment variables are stored in the Registry in the key:
HKEY_CURRENT_USER\Environment
System variables are found in the key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Note that any environment variable that is in the form that needs to be expanded (for example, %PATH%) must be stored in the registry as a REG_EXPAND_SZ registry value. Editing the Registry is primarily for scripts used by systems administrators and is not recommended for the average PC user.
You can find more information about it here.
"Global Commands" are determined by your environment variables.
Depending on which OS you are using getting to it to set it is rather different.
The most generic way is by
Right-Click "My Computer" Select "Properties"
In Windows7 you will have to select "Advanced System Settings" in the pane on the left.
Once you have the System Properties dialog open, Select the "Advanced" tab.
At the bottom, there is a button "Environment Variables"
In the Environment Variables Dialog, you will be able to configure environmental variables per user or for the entire system.
For example, the System Variable "Path" simplified would look something like this...
C:\Program Files\Java\jre6\bin;C:\Ruby19\bin;
Executables in these paths can be called directly from Command Line.
The reason you can call calc, mspaint, cmd all from the Run Dialog is because C:\windows\system32 is in the Path environment variable.
You may add custom paths into the environmental variables, careful not to override the existing one and to separate the paths using a semi-colon. ";"
You can then run an executable from that path directly in command line without typing out the entire path, like the java -version command.
For more reading please see the following links...
Environment Variables
Understand and Configure Environment Variables
Here's a simple which
that displays all command hits in PATH for the current PATHEXT extensions:
@for %%I in ("%path:;=\" "%") do @for %%E in (%pathext:;= %) do @if exist "%%~dpI%1%%E" @echo %%~dpI%1%%E
Put that in where.cmd
(allowing which
to come from somewhere else if you get it!) and call it with where java
.
Now, in my comment above, I suggested that if java
does not appear in your PATH
, you can check in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
[ref], but after actually trying this, it does not work with the command line, only with things that use ShellExecuteEx
, such as Start Run and the start
CLI command.
When you enter a command, the shell looks at an environment variable named PATH (%PATH%, $PATH depending on OS). It queries every directory on the PATH to see if it encounters an executable that matches the command you entered. The first one it finds will be executed.
See other answers for some of the various ways to set the PATH variable.