i'm trying to make a batch script for running my Java files. I've found out that there is no way to prevent auto-closure of a batch script(except using pause keyword, tho it just waits for key press). I've also discovered that starting a new window will cause only main windows to close, not the new one too so i want a way that the command SET /P file=Java file:
is executed in the new window(I got the new window by using the start
keyword. Is there any way to accomplish this without downloading other softwares? this is the code i came up with yet:
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
set /P file=Java file to execute:
java %file%^.jar
start
I guess you're looking for that :
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
start cmd /V:ON /K "@set /P "file=Java file to execute: " && java -jar !file!^.jar"
EDIT: Using expansion with /V and use of /K instead of /C to keep the cmd windows open.
Explanations : To launch a console process in another windows and keep it open after the end of this process console we launch another cmd
process with the start
command. We use /V:ON
to use delayed expansion by default, meaning modified variables (like the one we prompt, %file%
) will be expanded on the fly with !
(so !file!
instead of %file%
). We use /K
to tell to this cmd process to not close when provided commands end. To this cmd
process, we provide the following commands :
@set /P "file=Java file to execute: "
This one will ask for the jar filename (without extension) to launch.
The @
means "do not echo the command itself on the console stdout" for a nice display.
java -jar %file%^.jar
This one launch the java interpreter (JVM) with the filename of a jar file to execute through the -jar
parameter, filename build from the previous prompt and the .jar
extension. The ^
here escapes the .
, it seems not useful here but maybe your script/env requires it.
We link the both commands with &&
which means : _if left command from &&
is successful (it exits with ERRORLEVEL 0
) then execute the right command from &&
.