Search code examples
javawindowsbatch-fileserviceyajsw

Inconsistent behaviour with paths in YASJW batch scripts


Our company wants to easily install our Java Server application as a windows service, therefore we are using YAJSW to wrap the application. To make it a little more convenient I have created some small batch scripts that just require a click to install / uninstall / start / stop the service.

Install, start and stop are working fine, but when using uninstall, I am getting an error that some file couldn't be found. They all use the same wrapper config and all batch files lie in the same place, so how can it be that one can't find a file but the others can't?

Here is my folder structure:

lib\
|---YAJSW
    |----bat\
    |    |--- installService.bat
    |    |--- uninstallService.bat
    |    |--- and so on
    |----conf\
         |--- wrapper.conf
MyApplication.jar
installService.bat      //Basically a proxy batch doing some other stuff and then calling the original installService.bat
uninstallService.bat    //Also a proxy
startService.bat        //Proxy
stopService.bat         //Proxy

Those are two of the original files, one of the working ones and the one that's failing:

Here is the uninstallService.bat:

pushd %~dp0
call setenv.bat
%wrapper_bat% -r %conf_file%
popd

and here is the installService.bat:

pushd %~dp0
call setenv.bat
%wrapper_bat% -i %conf_file%
popd

If anyone wonders where %conf_file% comes from, that is being set by setenv.bat just like the rest of the necessary things for running the tasks.

They are the same, except that one is passing -r instead of -i.

Anyway, those are my proxy files:

installService.bat which is working fine:

::Make a clean copy of our default config
xcopy /y /Q lib\YAJSW\conf\wrapper.conf.default lib\YAJSW\conf\wrapper.conf

::Set current workingdirectory to current executing directory
SET workingdir=%cd%

::Replace all backslashes with 4 backslashes to keep YAJSW functioning
SET workingdirFixed=%workingdir:\=/%

::Set the working directory to the variable that we set up before
echo wrapper.working.dir=%workingdirFixed% >> lib\YAJSW\conf\wrapper.conf

::Call the install batch file which uses the config that we have created
call lib\YAJSW\bat\installService.bat

and uninstallService.bat which isn't working:

call stopService.bat
call lib\YAJSW\bat\uninstallService.bat

I really don't have a clue what's wrong here.

Edit

setenv.bat:

@echo off
rem quotes are required for correct handling of path with spaces

rem default java home
set wrapper_home=%~dp0/..

rem default java exe for running the wrapper
rem note this is not the java exe for running the application. the exe for running the application is defined in the wrapper configuration file
set java_exe="java"
set javaw_exe="javaw"

rem location of the wrapper jar file. necessary lib files will be loaded by this jar. they must be at <wrapper_home>/lib/...
set wrapper_jar="%wrapper_home%/wrapper.jar"
set wrapper_app_jar="%wrapper_home%/wrapperApp.jar"

rem setting java options for wrapper process. depending on the scripts used, the wrapper may require more memory.
set wrapper_java_options=-Xmx30m -Djna_tmpdir="%wrapper_home%/tmp" -Djava.net.preferIPv4Stack=true

rem wrapper bat file for running the wrapper
set wrapper_bat="%wrapper_home%/bat/wrapper.bat"
set wrapperw_bat="%wrapper_home%/bat/wrapperW.bat"

rem configuration file used by all bat files
set conf_file="%wrapper_home%/conf/wrapper.conf"

rem default configuration used in genConfig
set conf_default_file="%wrapper_home%/conf/wrapper.conf.default"

wrapper.bat:

echo %java_exe% %wrapper_java_options% -jar %wrapper_jar% %1 %2 %3 %4 %5 %6 %7 %8 %9
%java_exe% %wrapper_java_options% -jar %wrapper_jar% %1 %2 %3 %4 %5 %6 %7 %8 %9

Solution

  • I have found the problem, the problem was, that in the uninstallScript.bat i have used call two times, the first call changed the working directory, since i was working with relative paths the seconds call was having problems resolving the paths.

    To fix it, i inserted a pushd with the current directory as a parameter at the beginning and after the first call a popd.

    The file now looks like this:

    pushd %~dp0
    call stopService.bat
    popd
    call lib\YAJSW\bat\uninstallService.bat