Open a command prompt and setting the default path

batch filecommand line

I want a .bat file which opens a command prompt and then set my default directory also set the enviromenet variable JAVA_HOME

For now I am manually openning command prompt and then setting my project directry path and then setting the environment variable.

I am using commands as :

open cmd.exe
cd d:\Code
set JAVA_HOME=C:\Jdk.1.6

I want that when I execute the batch file that execute the commant before openning. How to do that ?

Best Answer

Assuming you want specifically a batch file to be clickable directly on Windows desktop, a simple, yet not elegant way, would be to use the following code in batch file:

@cd d:\Code
@set JAVA_HOME=C:\Jdk.1.6"
@cmd.exe /K

It will set the path and the variable, and in that context launch a nested command session with /K parameter - which will force it to wait for user input (instead of disappearing).

A more elegant solution would be to create a new shortcut on the desktop and configure it as follows:

  • Command: cmd /K set JAVA_HOME=C:\Jdk.1.6
  • Start in: d:\Code
Related Question