Batch File Error When Setting CLASSPATH - java

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.

Related

Need help running my Java .jar file from Windows batch file

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
)

Expanding wildcard in batch

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.

Start a Java application using Bat file if not already started

I want to check using a bat file if a Java program is already running. If its not running then start it using start javaw.
I have tried WMIC and I am only successful so far to get the PID.
WMIC process where "CommandLine like '%MyServer.jar%' and Name like '%javaw%'" get ProcessId
But how to write an if else condition in the bat file to start?
I tried using tasklist and find
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
But for all the java applications myapp.exe will be java.exe.
Please help...
P.S: There are options for this to be done via the Java program itself, but my specific situation asks for a bat file solution.
You can use jcmd.exe to see the main class of all the java processes running. The IF-ELSE syntax of the batch file is explained in the following thread
How to use if - else structure in a batch file?
jcmd | find /I /N "sun.tools.jconsole.JConsole"
IF "%ERRORLEVEL%" GTR "0" (
jconsole.exe
)
IF %ERRORLEVEL% EQU 0 (
echo Programm is running
)
This link would help with the IF construct - http://ss64.com/nt/if.html

Running a batch file from java

I just downloaded MCP to see how things work behind the scenes in Minecraft.
Inside MCP there are a bunch of batch files that you use to do things like: decompile, recompile, startclient, etc.
What I would like to be able to do is run these batch files from a basic java gui.
I'm good with the gui part but I havent got a clue how to run those batch files.
Here is an example of one of the batch files:
The file is at:
C:\MCP\startclient.bat
startclient contains the following:
#echo off
:try_python
set PYTHON=python
%PYTHON% --version >NUL 2>NUL
if errorlevel 1 goto try_python_mcp
goto foundit
:try_python_mcp
set PYTHON=runtime\bin\python\python_mcp
%PYTHON% --version >NUL 2>NUL
if errorlevel 1 (
echo Unable to locate python.
pause
exit /b
)
:foundit
%PYTHON% runtime\startclient.py conf\mcp.cfg
pause
Can it be done?
You can easily run a batch file from java using Runtime:
Process p = Runtime.getRuntime().exec("cmd /c start " + yourbatchFileName);
you can also grab I/O of the process, using p.getOutputStream(), p.getInputStream() etc.
See more about the Process class here.
And I suggest you take a look at this article as well.

Batch and the for loop

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.

Categories