Normally, in windows an application is not saved into the PATH
variable of the system for accessing it easily in the command prompt unless the setup (installer) provides this option.
For example, if you try to use an application that provides a console interface without define it as an environment variable, you'll need to provide the entire path to the executable :
C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe http://google.com google.pdf
That’s a lot of typing (the entire executable path C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe
while in other platforms you can execute wkhtmltopdf http://google.com google.pdf
) in the command prompt, especially for something that as developer, you have to run often.
Fortunately, you can customize this behaviour creating environment variables by yourself.
What are environment variables
An Environment variable is an identifier that has a value related to the current environment, like the Operating System or user sessions, it in most of the cases is an absolute path (C:\path\to\something
).
After include i.e the EXECUTABLE path in the PATH variable of the system, then you'll be able to invoke the executable directly in the console without provide the full path but only its name:
wkhtmltopdf http://google.com google.pdf
Better, isn't ? However, note that is not bad to use the full path, we just create the environment variable to write less. For example, typing wkhtmltopdf
(the .exe
can be omitted) in the command line on Windows will show you the utilities available for wkhtmltopdf.
Note: in this article, we are going to add the wkhtmltopdf application as an environment variable in Windows. You only need to change the absolute path with yours in case you're adding another environment variable.
Before continue
To add an environment variable and type less, you need to provide the path to the folder where the executable is located, never the path with executable.
REM Wrong
C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe
REM Correct
C:/Program Files/wkhtmltopdf/bin
Windows 7 and 8
To open the environment variables menu, press the Windows key to open up the Start Menu or Start Screen, then search for "advanced system settings". Alternatively, you can access this menu if you go to the Control Panel, then Security , finally click on System and click on the Advanced system settings hyperlink in the left hand pane.
Once the System Properties window opens, click on the “Environment Variables” button.
In the System Variables box, look for a variable called Path
. Select that and click on the Edit button.
In 7 and 8, the variable value for Path is nothing more than a long string of text with various locations around the system. In order to add an entry to your path in Windows 7 and 8, you have to precede the new folder with a semicolon, so:
;C:/Program Files/wkhtmltopdf/bin
Click ok and you're ready to go.
Windows 10
To open the Environment variables menu, go to Control Panel > System and Security and finally in System. Click on the Advanced System Settings in the left panel.
Alternatively, you can open directly the environment variables menu by pressing Windows + R (Run app) and then type SystemPropertiesAdvanced.exe
:
The System Properties dialog will be shown, to continue click on Environment Variables in the Advanced Tab.
The Environment variables menu will be shown, select the Path variable in the User variables area and click on Edit.
Finally click OK in the Edit environment variable menu and you are ready to go,now you'll be able to write just the name of the executable (without path and extension) anywhere in the command prompt, i.e wkhtmltopdf.
Open a command prompt and try accesing the executable from any path in the system without type the full path:
Note: remember that you need to open a new command prompt to access the variable. If you have an active window of cmd.exe
then close it, and open again and the variable will be available.