Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Windows Bat: Detect JVM is 32 or 64 bit


Find location of java.exe
To run a java application, we need to find the java path: we can use define our own environment variable like MYAPP__JAVA_HOME or use JAVA_HOME or JRE_HOME or we can use system default java.
The code looks like below:

Detect JVM is 32 or 64 bit
Afterwards, we may have to detect JVM is 32 or 64 bit, so we can set different jvm parameters according: for example, if it's 32 bit java, set -Xmx 2g; if it's 64 bit java, set -Xmx 4g.
if not "%MYAPP__JAVA_HOME%" == "" (
  set "CURRENY_JAVA_HOME=%CV_JAVA_HOME%"
) else (
 if not "%JAVA_HOME%" == "" (
  set "CURRENY_JAVA_HOME=%JAVA_HOME%"
 ) else (
  if not "%JRE_HOME%" == "" (
   set "CURRENY_JAVA_HOME=%JRE_HOME%"
  )
 }
)
set _RUNJAVA="%MYAPP__JAVA_HOME%\bin\java" 
 
To detect 32 or 64 bit java, the code looks like below:
set is64Java=false
java -version 2>&1 | findstr /i "64-Bit" 1>nul 2>&1
if %errorlevel% == 0 (
set is64Java=true
)
Plain old java -version reveals information about JVM bitness only if installed JRE is 64 bit, in case
of 32 bit JVM it doesn't provide any information related to architecture but in case of 64 bit JVM it
prints :
C:\>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)

while in case of 32 bit JVM it will print
C:\> java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

'java -version 2>&1 | findstr /i "64-Bit"' checks whether there is 64-Bit in java -version, if so, it means it's a 64 bit jdk, if not, it's a 32 bit jdk.

Resources
http://stackoverflow.com/questions/5675459/how-to-get-java-version-from-batch-script
http://javarevisited.blogspot.com/2012/01/find-jvm-is-32-or-64-bit-java-program.html
http://blogs.msdn.com/b/oldnewthing/archive/2012/07/31/10334556.aspx

Solr: Run Embedded Jetty As Windows Service


In previous posts, I introduced how to build a package to include embedded jetty, solr.war, and solr.home in one package, reduce size of the package, and to start/stop it programmatically.
Part 1: Shrink Solr Application Size
Part 2: Use Proguard to Shrink Solr Application Size
Part 3: Use Pack200 to Shrink Solr Application Size
Start Stop Embedded Jetty Programmatically

In this article, I would like to introduce how to use Apache commons-daemon to install it as a Windows Service.

First download latest commons-daemon-*-bin-windows.zip from here.
Download latest commons-daemon-*-src.tar.gz from here.

In src\samples folder in commons-daemon-*-src.tar.gz, you can learn how to create a service, and the scripts to install and remove windows services:
ProcrunService.java, ProcrunServiceInstall.cmd, ProcrunServiceRemove.cmd.

The java code to start/stop embedded jetty server is here.
Next we create 2 scripts to install it as a windows service, and remove the windows services. 
installEmbededJettyService.bat
@echo off
setlocal
set MYPATH=%~dp0
set PATH_PRUNSRV=%MYPATH%
set SERVICE_JAVA=EmbededJetty
set PR_DESCRIPTION=Embedded Jetty Server
set PR_DISPLAYNAME=Embedded Jetty Server
set "PR_LOGPATH=%MYPATH%/../logs"
set PATH="%MYPATH%/../lib/servlet-api-3.0.jar;%MYPATH%/../lib/jetty-all.jar;%MYPATH%/../lib/startjetty.jar"

rem Allow prunsrv to be overridden
if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv

echo Installing %SERVICE_JAVA%
%PRUNSRV% //DS//%SERVICE_JAVA%
%PRUNSRV% //IS//%SERVICE_JAVA% --Install="%MYPATH%prunsrv"

if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_JAVA%' service
goto end
:installed
echo The service '%SERVICE_JAVA%' has been installed.

set MY_JVMOPTIONS=-server;-Xms512M;-Xmx2048M
echo Setting the parameters for %SERVICE_JAVA%
%PRUNSRV% //US//%SERVICE_JAVA% --StdOutput auto --StdError auto ^
--Classpath=%PATH% --JvmOptions=%MY_JVMOPTIONS% --Startup=manual ^
--StartMode=java --StartClass=com.codeexample.solr.EmbeddedSolrJettyServer --StartParams=start;-dynamicPort;true;%1;%2;%3;%4;%5;%6 ^
--StopMode=java  --StopClass=com.codeexample.solr.EmbeddedSolrJettyServer  --StopParams=shutdown

if not errorlevel 1 goto updated
echo Failed updating '%SERVICE_JAVA%' service
goto end
:updated
echo The service '%SERVICE_JAVA%' has been updated.

echo Installation of %SERVICE_JAVA% is complete
:end
endlocal 
@echo on
uninstallEmbededJettyService.bat
@echo off
setlocal
set MYPATH=%~dp0
echo %MYPATH%
set PATH_PRUNSRV=%MYPATH%
set SERVICE_JAVA=EmbededJetty
set "PR_LOGPATH=%MYPATH%/../logs"
if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv

echo Removing %SERVICE_JAVA%
%PRUNSRV% //DS//%SERVICE_JAVA%

if not errorlevel 1 goto removed
echo.
echo Failed uninstalling '%SERVICE_JAVA%' service
goto end
:removed
echo The service '%SERVICE_JAVA%' has been removed
:end
endlocal
@echo on

We can copy prunmgr.exe to the bin folder, and rename it as ${service-name}w.exe, so user can run it to edit it, or start, stop it like below:

2 Scripts to start and stop the windows servers.
startService.bat
@echo off
setlocal
set MYPATH=%~dp0
set PATH_PRUNSRV=%MYPATH%
set SERVICE_JAVA=EmbededJetty
if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv

if [%1] == [] goto startService

echo Changing the parameters for %SERVICE_JAVA%
%PRUNSRV% //US//%SERVICE_JAVA% --StartParams=start;-dynamicPort;true;%1;%2;%3;%4;%5;%6 

if not errorlevel 1 goto updated
echo Failed updating '%SERVICE_JAVA%' service
goto end
:updated
echo The service '%SERVICE_JAVA%' has been updated.

:startService
"%PRUNSRV%" //ES//%SERVICE_JAVA%
if not errorlevel 1 goto started
echo Failed starting '%SERVICE_JAVA%' service
goto end

:started
echo %SERVICE_JAVA% is started.

:end
endlocal 
@echo on
stopService.bat
@echo off
setlocal
set MYPATH=%~dp0
set PATH_PRUNSRV=%MYPATH%
set SERVICE_JAVA=EmbededJetty
if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv
:startService
%PRUNSRV%  //SS//%SERVICE_JAVA%
if not errorlevel 1 goto stopped
echo Failed stopping '%SERVICE_JAVA%' service
goto end

:stopped
echo %SERVICE_JAVA% is stopped.

:end
endlocal 
@echo on

Windows Commands/Batch All In One


Command
http://ss64.com/nt/
type  filename
Sleep some time
sleep 5
timeout 5
These 2 commands are not available in every windows machine, in practice, we can use ping to cause delay.
ping 1.1.1.1 -n 1 -w 1000 >NUL 2>NUL
start command: run command in a separate window.
call: Calls one batch program from another without stopping the parent batch program. 
find
find [/v] [/c] [/n] [/i] "string" [[Drive:][Path]FileName[...]]
/v(reverse), /c(count), /n(show line number), /i(case-insensitive)
for %f in (*.bat) do find "PROMPT" %f 
dir c:\ /s /b | find "CPU"
sort
/r, /+n, 
more
+n : Displays first file beginning at the line specified by n.
/c, /s
The following commands are accepted at the more prompt:
SPACEBAR Display next page
ENTER Display next line
f Display next file
= Show line number
p n Display next n lines
s n Skip next n lines
q, ?
Widnows Batch

Read until there are 3 lines in file result
@ECHO OFF
:readFileLoop
if exist result (
    set /a "x = 0"
    for /F "tokens=*" %%L in (result) do set /a "x = x + 1"
if "%x%" EQU  "3" (
goto :end
)
) > NUL 2>&1
goto :readFileLoop
@ECHO ON
Using batch files
If statement
CompareOp : 
EQU, NEQ, LSS, LEQ, GTR, GEQ
For - for /?
for {%variable|%%variable} in (set) do command [ CommandLineOptions]
Use %variable to carry out for from the command prompt. Use %%variable to carry out the for command within a batch file.
Directories only: for /D
Recursive: for /R
Iterating a range of values: for /L
Iterating and file parsing: for /F
eol=c Specifies an end of line character (just one character).
skip=n Specifies the number of lines to skip at the beginning of the file.
delims=xxx
tokens=x,y,m-n
for /F "eol=; tokens=2,3* delims=," %i in (myfile.txt) do @echo %i %j %k
This command parses each line in Myfile.txt, ignoring lines that begin with a semicolon and passing the second and third token from each line to the FOR body (tokens are delimited by commas or spaces). The body of the FOR statement references %i to get the second token, %j to get the third token, and %k to get all of the remaining tokens.
Using batch parameters
%0-%9
When you use batch parameters in a batch file, %0 is replaced by the batch file name, and %1 through %9 are replaced by the corresponding arguments that you type at the command line. To access arguments beyond %9, you need to use the shift command. 
modifiers: %~f1, %~d1, %~p1, %~n1, %~x1, %~s1, %~a1, %~t1, %~z1
setlocal/endlocal
Use setlocal to change environment variables when you run a batch file. Environment changes made after you run setlocal are local to the batch file. Cmd.exe restores previous settings when it either encounters an endlocal command or reaches the end of the batch file.
Parse Command Args
:parseParams
set key=%~1
set value=%~2
if "%key%" == "" goto :eof
if "%key%" == "-MY_JAVA_OPTIONS" (   
    shift
shift
if "%value%" == "" (
        echo Empty value for -MY_JAVA_OPTIONS
        goto end
    )
SET MY_JAVA_OPTIONS=%value%
    goto parseParams %*
) else (
rem skip some other parameters
shift
    goto parseParams %*
)
goto :eof

:parseArgs
  REM recursive procedure to split off the first two tokens from the input
if "%*" NEQ "" (
for /F "tokens=1,2,* delims== " %%i in ("%*") do call :assignKeyValue %%i %%j & call :parseArgs %%k
)
goto :eof
:assignKeyValue
if /i "%1" EQU "-Xmx" (
SET Xmx=%2
) else if /i "%1" EQU "-Xms" (
    SET Xms=%2
) 
goto :eof
SubString: SET var=%var:~10,5%
http://geekswithblogs.net/SoftwareDoneRight/archive/2010/01/30/useful-dos-batch-functions-substring-and-length.aspx
Save current path and restore it later
set currentPath=%cd%
cd %currentPath%

set MYPATH=%~dp0

Labels

ANT (6) Algorithm (69) Algorithm Series (35) Android (7) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) JSON (7) Java (186) JavaScript (27) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) adsense (5) bat (8) regex (5) xml (5)