I have a java application launched by a .cmd file. I want to set the classpath of the application through this batch, all the needed jars are into a lib folder.
Here is what I tried :
set _classpath=.
for %%i in (%1/lib/*.*) do ( set _classpath=%_classpath%;%%i )
Surprisingly, it seems that it does not act as expected. Let's say there is 3 jar in the lib folder :
pikachu.jar
sonic.jar
mario.jar
Here is what happens :
set _classpath=.
set _classpath=.;pikachu.jar
set _classpath=.;sonic.jar
set _classpath=.;mario.jar
Obviously, what I am looking to get is
set
_classpath=.;pikachu.jar;sonic.jar;mario.jar
Any idea ?
Thanks and regards,
Place this at the top of your batch file:
setlocal enabledelayedexpansion
Then inside the for loop, replace %_classpath% with !_classpath!
Without delayed expansion enabled, %_classpath% gets expanded once, at the beginning of your for loop.
[Edit] In response to a comment, here is a full code-listing
#echo off
setlocal enabledelayedexpansion
set _classpath=.
for %%i in (%1/lib/*.*) do (
set _classpath=!_classpath!;%%i
)
echo %_classpath%
pause
CMD.EXE is expanding the %...% before running the loop.
You need delayed variable expansion, this is explained in set /? from a command prompt.
Related
I have problems to start eclipse with a proper configured JVM from windows batch file.
I like to do it this way, because I have different workspaces, different eclipse versions, different java versions and different configurations.
I guess the problem is the way I pass the parameters. I have tried several combinations of setting '"', but none worked. Below is just one example.
Anyway, can someone help?
Thanks :-)
Afterwards added:
Sorry, I forgot - the problem is the parameters for the JVM are neglected
#echo off
ver
echo.
set thePathToJDK=C:\Program Files\AdoptOpenJDK\jdk-11.0.10x9-hotspot
rem to be checked - is JAVA_HOME needed by anyone ?
rem eclipse states that is neglecting JAVA_HOME
set JAVA_HOME=%thePathToJDK%
rem set path to vm ( shall be in harmony with path to JDK )
set thePathToJVM=%thePathToJDK%\bin\javaw
set thePathToEclipse=C:\eclipse\ ...
set thePathToEclipseWorkSpace=C:\ ...
rem set minimal path (for ECLIPSE/JAVA)
set theNewPath=%SystemRoot%\system32
set theNewPath=%theNewPath%;%SystemRoot%
set theNewPath=%theNewPath%;%SystemRoot%\System32\Wbem
set theNewPath=%theNewPath%;%SystemRoot%\System32\WindowsPowerShell\v1.0\
set path=%thePathToJDK%\bin;%theNewPath%
echo.
echo path used for ECLIPSE
echo %path%
set theCommand=%thePathToEclipse%\eclipse.exe -data "%thePathToEclipseWorkSpace%" -vm "%thePathToJVM%" -vmargs -ea -Xmx4096m -Xss256m
echo.
echo %theCommand%
rem start eclipse with preconfigured Java
cmd /c %theCommand%
pause
On Windows, you need to include the .exe part of the VM's file name when you're using -vm with a file rather than a folder.
As #greg-449 pointed out, "the parameters" only change the Java that Eclipse itself uses.
What I wanted cannot be done the way I wanted it to be done
:-(
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
)
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 /?.
I have created a batch for my java swing application.After running batch the UI vanishes in seconds.
How to put my swing UI on hold until and unless we close it manually.
My batch file as below:
#echo on
set PATH=.;%PATH%
set CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
echo "Installing"
"%JAVA_HOME%\bin\java" -Xms512M -Xmx1G -classpath ./Installer/bin/plexus-classworlds-2.5.2.jar -Dclassworlds.conf=installer.conf %CLASSWORLDS_LAUNCHER%
pause
You can try the below and see:
Set the classpath to include the dependent jars if any and the location of the class files(bin folder)
set java home
execute the Main Class(with the main() method, com/main being the package name,change it according to your package structure)
Sample batch file:
#echo on
For /F "delims=" %%i in ('cd') Do set currentdir=%%i
SET CLASSPATH=%currentdir%\bin;%currentdir%\lib\poi-3.9.jar;
set JAVA_HOME=C:\Program Files\Java\jre6
SET path=%JAVA_HOME%\bin;%path%;
java com/main/MainRunner
pause
Check if this helps.
I have a number of weka datasets I need to run at once so I was creating a batch file to do that.
#ECHO OFF
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
SET CLASSPATH=C:\Program Files (x86)\Weka-3-6\weka.jar
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
When I run the SET CLASSPATH command and the java command in a regular command line they work fine, and they also work on their own in a batch file but as soon as I nested them in a for loop, I started getting "\Weka-2-6\weka.jar was unexpected at this time" errors.
I'm not an expert in Java or in Batch file programming so forgive me if the fix is really simple but I've been up and down the internet and I haven't found any solutions to this problem. What am I doing wrong here?
Thanks for your help.
#ECHO OFF
setlocal
SET "CLASSPATH="C:\Program Files (x86)\Weka-3-6\weka.jar""
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
endlcoal
set class path can be expanded after the for loop is finished.So better defined it outside the loop.