Why am I able to execute a program that is not in my PATH environment variable?

9

1

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

Pacerier

Posted 2012-01-09T15:09:48.140

Reputation: 22 232

2

See here.

– Daniel Beck – 2012-01-09T15:44:34.023

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 – 2012-01-09T16:59:37.230

1@MarkHurd cmd (and CreateProcess) doesn't examine the App Paths registry entry, only ShellExecute [and therefore the Run dialog] does. – Random832 – 2012-01-09T21:21:31.253

@MarkHurd, as you've expected, it's not in my path variable.. – Pacerier – 2012-01-09T22:13:45.010

1@Pacerier The java executable is found in %SystemRoot%\System32\ – Dwayne Hinterlang – 2012-01-09T22:24:44.357

@DwayneHinterlang Ic, this is weird.. shouldn't it be the java.exe in %programfiles%\java\jre7\bin ? – Pacerier – 2012-01-09T22:45:01.243

@Pacerier I have a new install of JRE 8, where java.exe is found in multiple folders, must be a java thing. :S Included a snap shot for convenience. – Dwayne Hinterlang – 2012-01-09T22:55:45.173

And that's why which is really convenient on any Unix/Linux system. – Daniel Beck – 2012-01-09T23:03:40.340

@DanielBeck i'm lost.. what do you mean? – Pacerier – 2012-01-10T00:36:02.643

Well, what does it do? It tells you which instance of the program gets executed when you type the program name. Instant answer to your edited question. – Daniel Beck – 2012-01-10T05:16:59.090

@Random832: I didn't see your comment until after I confirmed it my self, but you're right App Paths are not checked by cmd. – Mark Hurd – 2012-01-10T06:10:14.823

@MarkHurd so do u mean that if java.exe is not in the path, its not possible for anything to run? – Pacerier – 2012-01-10T06:53:13.877

@Pacerier: Yes, if java.exe is not in one of the directories of PATH, and not in the current directory, then typing java.exe on the command line of cmd should return 'java.exe' is not recognized as an internal or external command, operable program or batch file. – Mark Hurd – 2012-01-12T06:30:34.677

Answers

13

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.

Diogo

Posted 2012-01-09T15:09:48.140

Reputation: 28 202

1However, java.exe is located in %programfiles%\java\jre7\bin. This folder doesn't appear in my system's Path variable.. – Pacerier – 2012-01-09T22:14:25.367

7Java.exe is also found in %SystemRoot%\System32\ which does appear in your PATH variable – Dwayne Hinterlang – 2012-01-09T22:26:43.433

5

"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

Dwayne Hinterlang

Posted 2012-01-09T15:09:48.140

Reputation: 583

3

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.

Mark Hurd

Posted 2012-01-09T15:09:48.140

Reputation: 379

2

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.

Dennis S.

Posted 2012-01-09T15:09:48.140

Reputation: 129