I have a Java jar file located in:
C:\Users\myusername\bin\MyDir\MyApp.jar
I also have some required properties files (needed as input arguments to the .jar file) located in the same directory as the .jar file.
I created a runme.bat file here:
C:\Users\myusername\Desktop\runme.bat
In the runme.bat file, this is what I have:
setlocal
set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_161\bin\"
set PATH=C:\Users\myusername\bin\MyDir\
start %JAVA_HOME%javaw -jar %PATH%MyApp.jar %PATH%propertiesfile.properties
However, whenever I try to run the .bat file, I get the error:
Windows cannot find '-jar' Make sure you typed the name correctly, and then try again.
On the command line I see Windows trying to do this:
> "C:\Program Files\Java\jdk1.8.0_161\bin\"javaw -jar C:\Users\myusername\bin\MyDir\MyApp.jar ...
I get this error when running from the command line. If I simply double-click the .bat file, a cmd window comes up and quickly disappears.
So, what am I doing wrong?
Thanks!
Use Double quotes around the set command, not inside the variables.
Also, I see no reason to use the START command unless you want to do more in your batch file in the original command prompt after starting your Java in a second command prompt. Possible but seems unlikely.
Generally, you will just type in the executable or use CALL so that the executable runs and control returns to the batch after reaching conclusion.
Additionally, you changed your system path variable to be just the path of your java files which will make the session pretty screwy. Thankfully this should only persist in your open command windows and those spawned by the original window, so close them all and then use a different variable name for your path.
So I will put this both ways, using Call, and using start.
Here is your code using call:
#(
setlocal
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
TITLE "%_Title%"
CD /D "%JAVA_HOME%"
CALL "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%\propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)
Here is your code using the start command:
#(
SETLOCAL
ECHO ON
)
set "_Title=Runnning My Java"
set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_161\bin"
set "_MyJarPath=C:\Users\%UserName%\bin\MyDir"
start "%_Title%" /D "%JAVA_HOME%" "%JAVA_HOME%\javaw.exe" -jar "%_MyJarPath%\MyApp.jar" "%_MyJarPath%propertiesfile.properties"
(
ENDLOCAL
EXIT /B 0
)
Related
I have written a Java program for linux and windows.
To start the program, I use the following code in linux:
abc.sh:
#!/bin/bash
java -jar /opt/AudiobookConverter/AudiobookConverter.jar "$#"
The "$#" will expand any wildcard I may throw at it.
So, if I want my program to process all .mp3-files in a certain directory, I just call abc.sh *.mp3 and it does its magic.
Under Windows, I have the following code, which is supposed to do the same:
abc.bat:
java -jar C:\AudiobookConverter\AudiobookConverter.jar "%*"
But when I call abc.bat *.mp3, it will pass *.mp3 to the java program, instead of a list of the files ending in .mp3.
What am I doing wrong, and how to fix this?
Greetings,
AHahn94
#echo off
setlocal enabledelayedexpansion
set "files="
for /f "delims=" %%a in ('dir /b /a-d "%*" ') do set "files=!files! %%a"
java -jar C:\AudiobookConverter\AudiobookConverter.jar "%files%"
The setlocal command opens a local environment with delayed expansion invoked. The dir command lists files (names only - no directorynames), the for assigns the entire list-line to %%a, and each name is appended to the environment variable files using the delayed expansion syntax to access the run-time value.
Once the files variable has been established, conventional syntax may be used to pass it to the java command.
I have a run-tests.bat file as in the following (provided to us). This bat file is in a directory along with a lot of other files and is zipped. It is a standalone testng directory. In addition to this file, in the directory are subdirectorys lib/ and drivers/ and lib/ does have testng:
#ECHO OFF#ECHO OFF
SET javacmd=
SET javacp=-classpath ".;lib/*"
SET jvmparams=-Dwebdriver.ie.driver="drivers/IEDriverServer.exe" - Dwebdriver.chrome.driver="drivers/chromedriver.exe"
IF "%JAVA_HOME%"=="" (
SET javacmd=java
) ELSE (
SET javacmd="%JAVA_HOME%\bin\java"
)
for /f tokens^=2-5^ delims^=.-_^" %%j in ('%javacmd% -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"
IF %jver% LSS 17000 (
ECHO Java version is not supported. Please install Java 1.7 or greater.
GOTO RETURN
)
%javacmd% %javacp% %jvmparams% org.testng.TestNG testng.xml
:RETURN
IF -%1-==-- (
PAUSE
)
When I run this on my machine it works fine. I needed to copy it to a shared directory so some other people could run it. I copied it to a shared directory and tests it and it ran fine. I navigated with the file explorer, and double clicked.
When I had the tester do it from his machine, the cmd window would just come up and disappear. I put a bunch of pauses in it to see where it was failing. The %jver% is 17045 so it made it past that line. It failed at the %javacmd% command and said it could not find (or load) org.testng.TESTNG. It found it fine when I ran it from my machine but not from his.
My only thought is that when I double click on the .bat file it somehow CD's me to the correct directory, but leaves him in his directory so that SET
javacp=-classpath ".;lib/"* sets lib/. in his own directory rather than in the directory that is unzipped. I tried to figure out when you click on a .bat file whether it CDs you to a directory or not but could not find it out.
So is my theory for why it is not working correct, or is there some other reason I am not seeing? I verified my dos version and his are the same, 6.1.7601, and we are both using Windows 7 and he has Java 7.
Double clicking a .bat or .cmd script file causes that script runs usually
either in %HOMEDRIVE%%HOMEPATH% or in %SystemRoot%\system32 starting directory. I'd try next
#ECHO OFF
SETLOCAL EnableExtensions
echo "%~f0" script started in "%CD%"
pushd "%~dp0"
echo "%~0" script now run in "%CD%"
pause
rem continue here with your code:
SET javacmd=
…
Explanation:
SETLOCAL EnableExtensions: although Command Extensions are enabled by default, this can be disabled or switched off, see CMD shell or cmd /?. Right ensure…
"%~f0", "%~dp0" Parameter Extensions: see Command Line Arguments or call /?.
pushd "%~dp0": change the current directory/folder including disk drive, see also pushd /?.
Im trying to run a simple RMI application via batch file.
I have been given these lines by my instructor to write in notepad and save it as .bat :
#cd %cd%\src
#for /r %%a in (*.java) do #javac %%a
#start rmiregistry 3000
#java Server
#pause
When I run the batch files it shows that 'javac is not recognized as an internal or external command operable program or batch file', and for the RMI it shows 'The system cannot find the file rmiregistry'.
I searched for solutions for 'javac' problem and it appears I have to set a path for it in the 'Environment Variables' which I did via adding a new USER variable in the name of JAVA_HOME and the path : 'C:\Program Files\Java\jdk1.8.0_40\bin'
The problem is consistent and I don't know where to start to solve it
You should append this $JAVA_HOME environment variable in the PATH environment variable. This will solve your problem.
OR Alternatively,
you should directly add this directory location to the end of the PATH environment variable.
PATH=........;C:\Program Files\Java\jdk1.8.0_40\bin
// here ....... denotes previous entry done already in PATH
Or, SET Path by typing the following in the CMD(console/command prompt) as :-
set PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_40\bin
I want to run a simple batch file that navigates to a folder location and runs a java command. What I have now won't run the command.
#echo on
set /p DIR="C:\Application\dir_to_run_from"
dir %DIR%
java -cp file.jar com.myCompany.db.collector.Collector
Right now I'm just getting the path printed when I run the command.
set /p does not set the path. p stands for prompt. Usually, you'll use it like this:
set /p ANIMAL=Enter your favorite animal:
Instead, you probably just want to replace the first line with this:
cd "C:\Application\dir_to_run_from"
This will cd (change directory) to the directory you want to use. (You should change the second line to just dir if you take this route.)
Alternatively, you could replace the last line with this:
java -cp %DIR%\file.jar com.myCompany.db.collector.Collector
Having read the post at .bat files, nonblocking run/launch,
I still cannot achieve what I need: to have the .BAT file close once the start command is executed. My problem is that when the JVM starts the application launches a window so I end up with 2 windows being opened, when in fact one of them (the .BAT command) is just a startup process and doesn't do anything meaningful to the user.
I paste the .BAT code here:
#echo off
setlocal
rem Starts the application
rem Check for Java Home and use that if available
if not "[%JAVA_HOME%]"=="[]" goto start_app
echo. JAVA_HOME not set. Application will not run!
goto end
:start_app
echo. Using java in %JAVA_HOME%
start "Application" "%JAVA_HOME%/bin/java.exe" -jar lib/pathToMyJarFile
goto end
:end
I'd like the .BAT process to terminate (or at least the window to close) once the JVM starts.
Try javaw.exe instead of java.exe.
Use start /b, see this:
http://zeroflag.wordpress.com/2007/05/12/start-command/