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 /?.
Related
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
)
Issue Environment : Windows Server 2008 R2 Build 7601
I have a batch file SomeBatchFile.bat which uses %~dp0 to get the script location. But when it's executed on Windows Server 2008 R2 Build 7601 from a java program, its showing a buggy behavior.
Here is the behavior,
1) When executed like this
Process proc= Runtime.getRuntime().exec("c:\\full\\path\\SomeBatchFile.bat");
Keeping the SomeBatchFile.bat file in C:\full\path (essentially giving the actual full path), its returning the expected result c:\full\path\
2) But when executed like this
Process proc= Runtime.getRuntime().exec("SomeBatchFile.bat");
Keeping the SomeBatchFile.bat file in C:\Windows (essentially a location that is a part of environment variable PATH).This returns a wrong value instead of the BAT script location, its returning the java program location from where this script is called.
This is the script file I am using,
REM Just prints the script location to a file
set MY_HOME=%~dp0
echo %MY_HOME% >> test_out.txt
REM And some other business logic here ...
On Windows Server 2003, this is working absolutely fine.
Any idea why this happens like this ? Is this Java/Windows Bug ? And how to resolve this ?
If you are trying to launch script in a relative path, you should also give a try for these syntaxes:
Process proc= Runtime.getRuntime().exec(".\\SomeBatchFile.bat");
or
Process proc= Runtime.getRuntime().exec(".\SomeBatchFile.bat");
or
Process proc= Runtime.getRuntime().exec("./SomeBatchFile.bat");
One of the three should work.
I've tried to replicate your problem, and the only way I get to do it is if the batch file is executed quoted, that is, if when the java program invokes the cmd instance to handle the batch file execution, the batch file name is "SomeBatchFile.bat". You can try to execute your batch file from command line calling it with quotes to see the problem.
That is, a simple batch file as
#echo %~dp0
placed somewhere in the path (by example, c:\windows\test.bat), when called from command line, from any folder, without including the path to it, without quotes (just type test.bat), you will retrieve the correct folder. But, if the same test is done calling the file as "test.bat" you will retrieve the current folder
D:\>type c:\windows\test.bat
#echo %~dp0
D:\>
D:\>test.bat
C:\Windows\
D:\>"test.bat"
D:\
D:\>cd temp
D:\temp>test.bat
C:\Windows\
D:\temp>"test.bat"
D:\temp\
I don't have access to neither 2003 not 2008 to check, but "probably" the configuration on batch file invocation was changed.
Here (in this case the problem raised from c# and the reference is to the full batch file name, but it is the same problem) you will find why this happens.
If the batch file name does not contain spaces or special characters you can simply use something like
Runtime.getRuntime().exec("cmd.exe /c SomeBatchFile.bat")
Or, if you can not avoid the quotes in the file name, you can change the batch file to use a subroutine to obtain the required information
#echo off
setlocal enableextensions disabledelayedexpansion
call :getBatchFolder MY_HOME
echo %MY_HOME%
goto :eof
:getBatchFolder returnVar
set "%~1=%~dp0"
goto :eof
I am trying to launch a .bat file but I get the error
System cannot find the file C:\ProgramData\Oracle\Java\javapath\java.exe
I understand that this is probably a path variable, but I have not been able to understand how to fix it, if that is the problem.
Thanks in advance.
First of all, the path that you provided i.e. C:\ProgramData\Oracle\Java\javapath\java.exe doesn't appear to be correct. It should point to either JRE or JDK folder that you (probably) have in here:
C:\Program Files (x86)\Java\jdk1.8.0_25\bin
In there you should at least see the following files (and hence, the addition of this bin directory to your PATH variable in the env vars):
java.exe
javac.exe
javaw.exe
javap.exe
java.h.exe
java-rmi.exe
javadoc.exe
jarsigner.exe
jar.exe
If you don't see the files above, something is definitely wrong! Also, even if you were using JRE (not JDK), you should still have this bin folder present, where java.exe resides.
I had the same problem. Here's what worked for me:
Go into your path variable and delete: C:\ProgramData\Oracle\Java\javapath\.
Then add a new one called: %JAVA_HOME\bin
This post helped: java-path-is-not-pointing-to-sdk-path-in-windows
Edit: Sorry, I just noticed that this question was 3 years old.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#ECHO OFF
:: Export java settings from registry to a temporary file
START /W REGEDIT /E %Temp%\java.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft"
if not exist "%Temp%\java.reg" (
START /W REGEDIT /E %Temp%\java.reg "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft"
)
if not exist "%Temp%\java.reg" (
echo java not installed
exit /b 1
)
:: Find java location
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%\java.reg ^| FIND "INSTALLDIR"') DO SET "JAVA_HOME=%%B"
SET "JAVA_HOME=%JAVA_HOME:"=%"
SET "JAVA_HOME=%JAVA_HOME:\\=\%"
::SET JAVA_HOME
set "java=%java_home%\bin\java.exe"
"%java%" -version
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Try this to see if you can automatically can detect the java location.And if it's work you can put this at the beginning of your script and use %java% in your script (though it will decrease the performance).
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/
I have a stanalone application with a main class which used to run a windows BAT file ,the BAT file which invoke another java class(B),the class B refer so many JARs and configuration files which i configured through "build Path"
Now I want to refer the JARs and configuration files in BAT file.How I write the BAT file.
If you can make sure the .bat file and all needed jars are located in the same directory, this is quite easy:
rem sets the basedir to the directory where this batch file is locaed
set basedir=%~dp0
rem build the classpath for the Java command
set cp=%basedir%\jar_one.jar
set cp=%cp%;%basedir%\jar_two.jar
set cp=%cp%;%basedir%\jar_three.jar
(and so on...)
rem start your second class
java -cp %cp% your.package.ClassB
Is the program specific to one machine or does it need to be generic?
The issue would be, I think, would be how to get a jar file to return something outside of java. The obvious answer would be, use the batch file to run the java. Write the java so that it writes its 'return value' to a file. Write the batch file so that it watches for the file/dir that the java writes to, and looks for the changed/new data and then use it.
REM if all jar files contains in ./lib dir:
setlocal enabledelayedexpansion
for /r "./lib" %%a in (*.jar) do (set "CP=%%a;!CP!")
REM start your second class
java -cp %CP% your.package.ClassB