Windows Batch File popd not working as expected - java

I am using the following batch script to run a Java Command Line tool.
#echo off
pushd %~dp0
setLocal EnableDelayedExpansion
set CLASSPATH="
for /R ./libs %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!"
java -cp !CLASSPATH! com.example.CLIApplication %*
popd
I have added the tool's directory to the System Variables PATH so that I can run it from any directory via the command prompt. This is working but the problem I am seeing is:
The tool's dir is C:\tool\
The user is in C:\
After executing the batch file the user is left in C:\tool\ not C:\
popd is getting called but the console navigates back to C:\too\ instead of staying in C:\
How do I ensure they users directory is not changed after the script finishes?

The setlocal without endlocal causes this problem here.
You only need to add an endlocal just before calling popd.
In your code the popd returns to your first directory, but as setlocal stores all variables and all open setlocals are closed by implicit endlocals when the batch is exited, it will also restore the cd variable.

I may be off track, but why aren't you using:
pushd .

Related

BASH to BATCH java

There's this BASH file:
#!/bin/bash
set -eu
DIR=`dirname $0`
JAR=$DIR/myjar.jar
CLASSPATH=$JAR:./
HEAP_SIZE=-size
java $HEAP_SIZE -cp $CLASSPATH something.something2 "$#"
That I want to turn into a .bat file to run on windows
This is what I have so far:
#ECHO OFF
set DIR = %cd%
set JAR = DIR/myjar.jar
set CLASSPATH = %JAR%:./
set HEAP_SIZE = -size
java %HEAP_SIZE% -cp %CLASSPATH% something.something2
How would I complete it to have the same behavior as the bash file?
%CD% can work, but your bash script seems to be setting the directory based off the path of the script (i.e., argument in $0) and not the current directory. To use the same directory as the batch file, you use %~dp0.
You missed % around your DIR variable when you wanted to expand the value.
You shouldn't have spaces around equal signs in your set statements.
The equivalent of $# in batch is %*.
You should quote your %CLASSPATH%. On Windows you are much more likely to encounter paths with spaces in them.
#ECHO OFF
set "DIR=%~dp0"
set "JAR=%DIR%/myjar.jar"
set "CLASSPATH=%JAR%:./"
set "HEAP_SIZE=-size"
java %HEAP_SIZE% -cp "%CLASSPATH%" something.something2 %*

Use Windows Batch File to start a Java program

I am not familiar with batch script, but I want to create a Windows Batch File to start a Java program. The problem is that it has to specific the path where JRE is installed. When you install both JRE7 and JRE8, the name of that JRE8 folder would call something like jre1.8.0_20 or jre1.8.0_40 with the version number in the back. When you have only JRE8 installed, the folder would call jre8. Is there an easier way to find where the most updated JRE installed and then execute it? Thanks.
start ..\..\Java\jre7\bin\javaw.exe -Xms512M -Xmx1024M -Djna.library.path=.\lib -cp example.jar; com.example.main
You should be able to get the location of javaw.exe by executing where java. This can be set as a variable inside a batch file like this:
# sets a variable called 'java' to the location of javaw.exe
for /f "delims=" %a in ('where javaw') do #set java=%a
# execute you jar file
%java% -jar <app.jar>
Noticed that the above only seems to work when running directly from the command line. Here is another example that should work in a batch file:
# run.bat
#echo off
setlocal enabledelayedexpansion
for /f %%a in ('where javaw') do (
set java=%%a
)
!java! -jar %1
The above batch file should be called with the name of the jar file:
run.bat app.jar
I think it's best to just user JAVA_HOME and/or JRE_HOME and let the user / sysadmin worry what's installed.

Command line options to batch file (Java application)

I've a java application which I run using a batch file- start.bat
Batch file:
#echo off
rem (
set JAVA_HOME=.\jre7
rem echo %JAVA_HOME%
java.exe -jar app-0.0.1-SNAPSHOT.jar
rem )
Manifest file has the name of class which has public static main(String[] args) method, and that's the start point of application.
I run the application from command prompt by typing start.bat
Now I need to give command line arguments to this bat file that can be received by main method.
so the task is to pass command line parameters to batch file and then send those parameter to java class main method.
I was exploring apache cli library but not sure if it can help me.
Basically this should be the input:
start.bat -a :
if -a is there then do task A in java application
start.bat -b :
if -b is there then do task B in java application
start.bat -a -b :
do task A and B
Help appreciated.
Thanks !
As user882813 points out above, the easiest solution is, if you want your .bat file to accept the same switches as your .jar file, just append %* (the batch variable containing all arguments) to the Java command like this:
java.exe jar app-0.0.1-SNAPSHOT.jar %*
If your .jar file can't accept the same switches for some reason, here's a way you can script a translation within your batch script. If you check for the existence of -a and -b within %*, you can construct your java launch command appropriately. This should work even if the switches are in backward order (like start.bat -b -a).
#echo off
setlocal
set "JAVA_HOME=.\jre7"
set a=& set b=& set "args=%*"
if "%*" neq "" (
if "%args%" neq "%args:-a=%" set "a=1"
if "%args%" neq "%args:-b=%" set "b=1"
)
if defined a (
if defined b (
rem :: Do both A and B
set "cmdArgs=/ABswitch"
) else (
rem :: Do work A
set "cmdArgs=/Aswitch"
)
) else if defined b (
rem :: Do work B
set "cmdArgs=/Bswitch"
) else (
rem :: Do no work
set cmdArgs=
)
java.exe -jar app-0.0.1-SNAPSHOT.jar %cmdArgs%
A quick note about the if "%args%" neq "%args:-a=%" set "a=1" lines: this works by variable substring substitution. Basically, %args:-a=% is %args% with -a replaced with nothing. So if %args% does not equal the same thing with -a removed, it must contain -a. Therefore, set a=1. See this page for more info on batch variable string manipulation.
Try something like the following in your batch script (after you have checked if those command line params are actually passed):
IF "%1" == "-a" GOTO DOWORKA
IF "%2" == "-b" GOTO DOWORKB
//as a third meassure , check for both -a and -b and invoke :DOWORKAB label
:DOWORKA
//invoke java program with param a
:DOWORKB
//invoke java program with param b
:DOWORKAB
//invoke java program with param a and b

Input filename into jar from windows batch file

I want to input a file into my jar after it is executed from batch file.
I wrote the following batch code.
#echo off
set path=%PATH%;C:\Program Files\Java\jre7\bin
java -jar E:\ER\er_v.3.3.17.jar
The above code is working fine. However, after the jar file is executed I need to feed another file for it to run successfully.
I want something like this
#echo off
set path=%PATH%;C:\Program Files\Java\jre7\bin
java -jar E:\ER\er_v.3.3.17.jar
echo E:\Run.xml
Can somebody help me with this?
Here you go. I tested this and it works.
#echo off
setlocal
:: if java.exe is not in %path%
for %%I in (java.exe) do if "%%~$PATH:I" equ "" (
set "PATH=%PATH%;C:\Program Files\Java\jre7\bin;C:\Program Files (x86)\Java\jre7\bin"
)
echo E:\Run.xml| java -jar E:\ER\er_v.3.3.17.jar
This passes "E:\Run.xml" to the stdin of java -jar etc.

how to run the batch file from any folder

cd ../../jobs
set CLASSPATH=.;../xyz.jar;../mysql-connector-java-5.1.6-bin.jar
java folser.folder1 ../Files/MySQL.xml
cd ..
I need to run the batch file from any directory. I have set the paths for java. Can anybody help me?
Under *nix (e.g. Linux):
cd "`dirname \"$0\"`"
# your current directoy is now the script's directory
cd ../../jobs
set CLASSPATH=.:../xyz.jar:../mysql-connector-java-5.1.6-bin.jar
java folder.folder1 ../Files/MySQL.xml
cd ..
# when the script terminates, you are automatically
# back to the original directory
Under Windows NT/XP/etc.:
SETLOCAL
PUSHD .
REM current directory has been saved and environment is protected
CD /d %~dp0
REM your current directoy is now the script's directory
CD ..\..\jobs
SET CLASSPATH=.;..\xyz.jar;..\mysql-connector-java-5.1.6-bin.jar
java folder.folder1 ..\Files\MySQL.xml
CD ..
REM before the script terminates, you must explicitly
REM return back to the original directory
POPD
ENDLOCAL
Although I can't comment on Vlad's answer (comments require more points than answers?!) I would always be wary of relying on:
CD /d %~dp0
because Windows can't CD to UNC paths and has a nasty habit of dropping you into %windir% instead with potentially catastrophic results.
Instead, although it is more long-winded, you are usually better off referring to %~dp0 (or a variable containing that value) each time you need a full path.
BAD:
cd /d %~dp0
rd temp
GOOD:
rd %~dp0\temp
You message was a bit garbled, I'm assuming you're saying that java is on the path but you can't properly run your application from a batch file. It looks like you are missing the classpath option (-cp) for java. Try this:
cd ../../jobs
set CLASSPATH=.;../xyz.jar;../mysql-connector-java-5.1.6-bin.jar
java -cp %CLASSPATH% folser.folder1 ../Files/MySQL.xml
cd ..
Use %cd% to get the current directory (i.e. the one that the batch file lives in)
e.g.
set JAVA_HOME=%cd%\jdk1.x.x
set PATH=%JAVA_HOME%\bin;%PATH%
set CLASSPATH=%JAVA_HOME%\lib\tools.jar;%cd%\lib\myjar.jar;etc,etc

Categories