2

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Marcel
  • 1,509
  • 1
  • 17
  • 39
  • How is `%wrapper_bat%` getting defined? `setenv.bat` doesn't include a `setlocal` does it? Is there any reason you aren't quoting any of your variables containing paths? If a file path contains a space or an ampersand your scripts will break. – rojo Jul 10 '17 at 12:43
  • I thought so at first, but apparently the wrapper that reads the property file doesn't like the quotes. Anyhow, it only doesn't work in one of the files, which is what made me wonder. I will add the setenv.bat to the question, wait. the wrapper is actually a jar, meaning it most likely uses the Java properties api which doesn't need quotes. – Marcel Jul 10 '17 at 12:51
  • Just a general observation, but you'll get the benefits of quoting potentially script breaking badness while retaining the explicit ability to quote or not to quote if you use the convention `set "varname=value"`. That way the value doesn't contain the quotation marks, but the content is still isolated from tokenization. Get in the habit of quoting the `"variable=value"` pair every time you `set` a string value. Upon retrieval, use `"%varname%"` quoted as needed. This disambiguates your code, and makes it easier to read. – rojo Jul 10 '17 at 12:56
  • Okay, thanks for the info. – Marcel Jul 10 '17 at 13:00
  • 1
    Any reason why your path names are using forward slashes in `setenv.bat`? Does using backslashes break something Java-ish? – rojo Jul 10 '17 at 13:02
  • Actually `/` is just fine , i did just stuck with the way that was automatically generated. I am using `/` now. Sadly that doesn't solve my problem ;D – Marcel Jul 10 '17 at 13:05
  • Could we see the relevant parts of `wrapper.bat` where `-r` is handled? Could be you're missing a colon with `call :label` and the script is looking for a file called "labelname" which doesn't exist, or perhaps some other simple syntax error. You could also try adding `pause`s within `wrapper.bat` and make sure `@echo` is `on` to see exactly which line results in your error – rojo Jul 10 '17 at 15:28
  • @rojo i addded the ´wrapper.bat´, all it does is delegating the parameters. – Marcel Jul 11 '17 at 07:11
  • 1
    `rem` out `@echo off` in `setenv.bat` and wherever else it occurs in your scripts. That's the best way to track down where exactly things are going wrong. – rojo Jul 11 '17 at 13:16
  • @rojo I have found the problem, the problem was, that in the `uninstallScript.bat` i have used `call` two times, the frist call changed the working directory, since i was working with relative paths it didn't work anymore. Gonna post it as an answer. Anyways, thanks for the help @rojo. – Marcel Jul 13 '17 at 09:07

1 Answers1

0

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 
Marcel
  • 1,509
  • 1
  • 17
  • 39