简体   繁体   中英

Run the bat file outside the jar file folder

I have a jar file and a bat file inside the same folder. I created the jar file from the Eclipse by using export

In the bat file, I have the code like this below

java -Xms512M -Xmx512M -jar main.jar -o true
PAUSE

I can run the jar file no problem because it is in the same folder. However, if I place my bat file let say on my desktop, I cannot run the jar file. I am wondering is there a way to run my bat file without running it inside the same folder of the jar file?

Just replace the main.jar with the absolute or relative (ie from the location of the bat file) path of the jar file.

using absolute path:

bat file like this:

java -Xms512M -Xmx512M -jar C:\Users\admin\workspace\Main\main.jar -o true
PAUSE

using relative path:

bat file in C:\\Users\\admin\\Desktop like this:

java -Xms512M -Xmx512M -jar ..\workspace\Main\main.jar -o true
PAUSE

In the batch file you can change to the directory before executing the command.

cd ..\workspace\Main
java -Xms512M -Xmx512M -jar main.jar -o true
PAUSE

If you want the current working directory to remain in the location of the batch file add setlocal before changing directory

setlocal
cd ..\workspace\Main
java -Xms512M -Xmx512M -jar main.jar -o true
PAUSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM