0

I have a .bat file which is my .jar file launcher. The .jar file is a native application that talks with a Chrome extension. I have the following .bat file:

@echo off
rem set JARFILE = %CD%\MyNativeApp.jar
java -jar C:\Users\reza.ahmadi\workspace\SimpleNativeApp\bin\MyNativeApp.jar
pause

The .jar and the .bat files are in the same folder (along with my manifest). It works well.

My problem: It does not work if I replace that path there with just the file name, meaning:

java -jar MyNativeApp.jar

does not work for me. I am working with a Win 8 machine.

Any comments would be appreciated.

Reza Ahmadi
  • 357
  • 3
  • 12

2 Answers2

2

You can dynamically substitute the path by getting it from the currently executing batch file (%~dp0):

java -jar "%~dp0MyNativeApp.jar"
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
1

If the jar file is in the same folder that the batch file, you can use

@echo off
    setlocal enableextensions disabledelayedexpansion

    for %%a in ("%~dp0\MyNativeApp.jar") do set "JARFILE=%%~fa"
    java -jar "%JARFILE%"

    pause

You can find here more information on how to retrieve the current active folder or the folder where the batch file is stored

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126