I've created the following script, which has the purpose to install & configure a Windows Service. When called, Windows brings up the UAC window which must be acknowledged by the user to continue the process.
@echo off
CALL "%~dp0.\installAnchorService.exe"
sc config "FLEXnet Licensing Service" start= auto
The problem is, that normally the batch file isn't started in administrator mode. I already found a solution for that here: How to request Administrator access inside a batch file.
This works fine when i start the bat-file directly with double-click. When i start it via Java, the script falls into an endless loop. Somehow the VBS creation fails. Do I have to call my batch script in a special way that it's executed correctly in java? My approach was:
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "start", "createService.bat");
builder.start();
Any thoughts/hints are appreciated...
Full script:
@echo off
:: BatchGotAdmin
:-------------------------------------
REM Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
CALL "%~dp0.\installAnchorService.exe"
sc config "FLEXnet Licensing Service" start= auto