This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 16230ebeaf Fix BZ 70262 - fix --rename option for service.bat
16230ebeaf is described below

commit 16230ebeaf34cc1905ec66850358733f074f8918
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Oct 21 20:47:57 2026 +0100

    Fix BZ 70262 - fix --rename option for service.bat
    
    Based on PR #1095 provided by StrongOx
---
 bin/service.bat                        | 60 ++++++++++++++++++++++++++++++----
 webapps/docs/changelog.xml             |  8 +++++
 webapps/docs/windows-service-howto.xml |  5 ++-
 3 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/bin/service.bat b/bin/service.bat
index 76b61545e1..d47436de89 100755
--- a/bin/service.bat
+++ b/bin/service.bat
@@ -41,6 +41,7 @@ set "SELF=%~dp0%service.bat"
 
 set DEFAULT_SERVICE_NAME=Tomcat@VERSION_MAJOR@
 set SERVICE_NAME=%DEFAULT_SERVICE_NAME%
+set "RENAMED_SERVICE_EXE="
 
 set "CURRENT_DIR=%cd%"
 
@@ -161,9 +162,28 @@ echo Failed removing '%SERVICE_NAME%' service
 exit /b 1
 :removed
 echo The service '%SERVICE_NAME%' has been removed
-if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" (
-    rename "%SERVICE_NAME%.exe" "%DEFAULT_SERVICE_NAME%.exe"
-    rename "%SERVICE_NAME%w.exe" "%DEFAULT_SERVICE_NAME%w.exe"
+rem Restore the default executable names only when install --rename changed 
them.
+rem The default executable is not a renamed copy, and renaming a file to 
itself fails.
+rem The source path is required: rename looks up a bare file name in the 
current directory, not in CATALINA_HOME\bin.
+if /I not "%SERVICE_NAME%" == "%DEFAULT_SERVICE_NAME%" (
+    if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" (
+        rename "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" 
"%DEFAULT_SERVICE_NAME%.exe"
+        if errorlevel 1 (
+            echo Failed to rename "%SERVICE_NAME%.exe" to 
"%DEFAULT_SERVICE_NAME%.exe"
+            exit /b 1
+        )
+        set "RENAMED_SERVICE_EXE=1"
+    )
+    if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%w.exe" (
+        rename "%CATALINA_HOME%\bin\%SERVICE_NAME%w.exe" 
"%DEFAULT_SERVICE_NAME%w.exe"
+        if errorlevel 1 (
+            echo Failed to rename "%SERVICE_NAME%w.exe" to 
"%DEFAULT_SERVICE_NAME%w.exe"
+            if defined RENAMED_SERVICE_EXE (
+                rename "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe" 
"%SERVICE_NAME%.exe"
+            )
+            exit /b 1
+        )
+    )
 )
 exit /b 0
 
@@ -192,11 +212,37 @@ if "%SERVICE_STARTUP_MODE%" == "" set 
SERVICE_STARTUP_MODE=manual
 if "%JvmMs%" == "" set JvmMs=128
 if "%JvmMx%" == "" set JvmMx=256
 
-if exist "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe" (
+rem Rename using the full source path. A bare file name is resolved against
+rem the current directory, so "install service_name --rename" failed unless
+rem it was started from CATALINA_HOME\bin. The new name must be a file name
+rem only. Skip when the requested name is already the default: renaming a
+rem file to itself fails, and there is nothing to rename. If the default
+rem executable is already absent, a previous --rename left the service name
+rem in place and install continues with that file.
+if /I not "%SERVICE_NAME%" == "%DEFAULT_SERVICE_NAME%" (
     if "x%RENAME%x" == "x--renamex" (
-        rename "%DEFAULT_SERVICE_NAME%.exe" "%SERVICE_NAME%.exe"
-        rename "%DEFAULT_SERVICE_NAME%w.exe" "%SERVICE_NAME%w.exe"
-        set "EXECUTABLE=%CATALINA_HOME%\bin\%SERVICE_NAME%.exe"
+        if exist "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe" (
+            if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" (
+                echo The file "%SERVICE_NAME%.exe" already exists. The 
executables were not renamed.
+                exit /b 1
+            )
+            if exist "%CATALINA_HOME%\bin\%SERVICE_NAME%w.exe" (
+                echo The file "%SERVICE_NAME%w.exe" already exists. The 
executables were not renamed.
+                exit /b 1
+            )
+            rename "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%.exe" 
"%SERVICE_NAME%.exe"
+            if errorlevel 1 (
+                echo Failed to rename "%DEFAULT_SERVICE_NAME%.exe" to 
"%SERVICE_NAME%.exe"
+                exit /b 1
+            )
+            rename "%CATALINA_HOME%\bin\%DEFAULT_SERVICE_NAME%w.exe" 
"%SERVICE_NAME%w.exe"
+            if errorlevel 1 (
+                echo Failed to rename "%DEFAULT_SERVICE_NAME%w.exe" to 
"%SERVICE_NAME%w.exe"
+                rename "%CATALINA_HOME%\bin\%SERVICE_NAME%.exe" 
"%DEFAULT_SERVICE_NAME%.exe"
+                exit /b 1
+            )
+            set "EXECUTABLE=%CATALINA_HOME%\bin\%SERVICE_NAME%.exe"
+        )
     )
 )
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 38e9f53c50..10b31cfd73 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -1013,6 +1013,14 @@
         scripts in <code>bin</code> are executable when the archive is
         extracted on a Unix platform.
       </update>
+      <fix>
+        <bug>70262</bug>: Fix <code>service.bat</code> so <code>install
+        &lt;name&gt; --rename</code> renames the Windows service executables in
+        <code>CATALINA_HOME\bin</code>. <code>remove</code> now restores the
+        default executable names only when the service name differs from the
+        default. Based on pull request <pr>1095</pr> provided by StrongOx.
+        (markt)
+      </fix>
     </changelog>
   </subsection>
 </section>
diff --git a/webapps/docs/windows-service-howto.xml 
b/webapps/docs/windows-service-howto.xml
index e0409a061b..8f8117ccaf 100644
--- a/webapps/docs/windows-service-howto.xml
+++ b/webapps/docs/windows-service-howto.xml
@@ -467,7 +467,7 @@ C:\> service.bat install MyService</source>
 <p>When installing the service with a non-default name,
 tomcat<version-major/>.exe and tomcat<version-major/>w.exe may be renamed to
 match the chosen service name. To do this, use the <code>--rename</code>
-option.</p>
+option. The install will fail if either renamed file already exists.</p>
 
 <source>Install the service named 'MyService' with renaming
 C:\> service.bat install MyService --rename</source>
@@ -514,6 +514,9 @@ C:\> tomcat<version-major/> 
//DS//Tomcat<version-major/></source>
 <source>Remove the service named 'MyService'
 C:\> tomcat<version-major/> //DS//MyService</source>
 
+<p>If a renamed service is removed with <code>service.bat remove</code>, the
+executables are restored to their default names on remove.</p>
+
 </section>
 <section name="Debugging services">
 <p>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to