Friday, July 18, 2014

Java Desktop (e.g. Swing) Network Deployment Batch File

Most networks are fast enough these days to run your JAR right from the network share, but file locking makes deployment difficult with multiple users.

This batch file checks the network location for a newer versions of the JAR file and lib/ folder, and updates the local copy only if a newer copy is available. Then the latest copy is executed locally.

Network-Java-App-Deploy.bat
@echo off

rem JAR file and installation folder (no trailing slashes):
set jar_file=myjavaprogram.jar
set dist_dir=\\server\share\folder

rem users should have read/write access to this folder:
set inst_dir=%appdata%\%jar_file%

rem customize the text to appear in the command window title bar
title Java Application Updater
cls

echo.
echo Java Application Updater
echo.
echo Please feel free to use, reuse, modify, reproduce and distribute.
echo Code is as-is, without warrantee.  User assumes all liability for use.
echo.
echo JAR:   %jar_file%
echo FROM:  %dist_dir%
echo TO:    %inst_dir%
echo.
echo Close the window to cancel . . .
pause

echo.
echo when redistributing, please keep the following URLs 
echo here in the code, so that other's may see the sources
echo and the versions of this batch file:
echo.
echo  - http://javajon.blogspot.com/
echo.

echo.
echo verify %inst_dir%, %username%, temp and lib ...
mkdir "%inst_dir%"
mkdir "%inst_dir%\%username%"
mkdir "%inst_dir%\%username%\temp"
mkdir "%inst_dir%\%username%\lib"

echo.
echo copy application jar file:  %username%\%jar_file% ...
xcopy /D/C/H/R/I/F/K/Y/V "%dist_dir%\%jar_file%" "%inst_dir%\%username%\"

echo.
echo copy lib folder:  %username%\lib ...
xcopy /D/C/H/R/I/F/K/Y/E/V "%dist_dir%\lib\*.*" "%inst_dir%\%username%\lib\"
echo.

cd /D "%inst_dir%\%username%"
cd "%inst_dir%\%username%"
start %jar_file%

goto :skip_xcopy_switches_explanation

 These are the XCOPY command line switches we are using:
 
  single: /D/C/H/R/I/F/K/Y/V
  folder: /D/C/H/R/I/F/K/Y/E/V

 These are the above switches briefly described:
  
  /D - Copy files that are newer than the destination files
  /C - Continue even on error
  /H - Copy hidden and system files, too
  /R - Overwrite read-only files
  /I - Copies more than one file to destination 
    (assumes destination is a directory)
  /F - Displays full path and destination while copying files
  /K - Copies attributes (as opposed to resetting read-only attributes)
  /Y - Copy without confirming replacement of existing files
  /E - Copies any subdirectories, even if they are empty
  /V - Verifies each file as it is written to the destination file

:skip_xcopy_switches_explanation

No comments:

Post a Comment