Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was wondering why the command java -version is globally accessible?

I could run it from any directory and its working:

enter image description here

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

share|improve this question
2  
See here. –  Daniel Beck Jan 9 '12 at 15:44
    
I don't have the details at hand at the moment which is why this is a comment and not an answer, but if java.exe (or java.com, etc) is not in your PATH there is another registry entry that can specify executable paths. –  Mark Hurd Jan 9 '12 at 16:59
1  
@MarkHurd cmd (and CreateProcess) doesn't examine the App Paths registry entry, only ShellExecute [and therefore the Run dialog] does. –  Random832 Jan 9 '12 at 21:21
    
@MarkHurd, as you've expected, it's not in my path variable.. –  Pacerier Jan 9 '12 at 22:13
1  
@Pacerier The java executable is found in %SystemRoot%\System32\ –  Dwayne Hinterlang Jan 9 '12 at 22:24

4 Answers 4

up vote 12 down vote accepted

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:

  1. Adding manually with "Windows System properties": enter image description here

  2. 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" 
  1. 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.

  1. 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.

share|improve this answer
1  
However, java.exe is located in %programfiles%\java\jre7\bin. This folder doesn't appear in my system's Path variable.. –  Pacerier Jan 9 '12 at 22:14
7  
Java.exe is also found in %SystemRoot%\System32\ which does appear in your PATH variable –  Dwayne Hinterlang Jan 9 '12 at 22:26

"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

Java.exe Search on a New Install

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.