How to change content of batch file using Java code?
I worked with parsing XML using Java program. It worked fine. But can I do same for the batch file using Java?
I am able to run batch file using below code.
String command = "cmd /c start " + batFile;
Runtime rt = Runtime.getRuntime();
rt.exec(command);
The content of my batch file is:
#echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C "C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml"
exit
But before doing this I want to change location
C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml
..to user given XML location. How can I achieve this in my Java application?
Maybe use an environment variable instead. This way you don't need to edit the batch file, just set the variable before running it.
Here or here see how to set an environment variable from java and here's how to use them in a batch file.
How about passing command line args to the bat file like so:
#echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C %1
exit
Then modify your java code to pass in the XML file name after the bat file name
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
)
I created a simple batch file to run jar server file. It is two lines of code:
cd /D %~dp0
java -jar server-0.17.0.jar
The problem is the server file is updated every while and hence script either fails to run the correct version or fail to run.
My request is, is there anyway to replace server jar file with latest released by number or date dynamically?
I'm only aware that I can retrieve available jar files using:
dir *.jar
But have no idea how to concatenate the command with the katest version. Thanks
you can try a script like this:
#echo off
for /f "delims=" %%x in ('dir /od /b server*.jar') do set latestjar=%%x
java -jar %latestjar%
Second line in this script will set the latestjar to the last created file whose name compliant with server*.jar template.
I'm trying to run a script from an Amazon Linux machine. The script invokes checkstyle like this (in a script called eval.sh):
CHECKSTYLE="java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar"
CHECKSTYLE_RULES="/home/ec2-user/grader/config/checks.xml"
CHECKSTYLE_OUT="quality.log"
"${CHECKSTYLE}" -c "${CHECKSTYLE_RULES}" -f xml -o "${CHECKSTYLE_OUT}" $(find "${_toCheck}" -name "*.java") 2>"quality.err"
When I run this, I get the following error in quality.err:
./grader/eval.sh: line 10: java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar: No such file or directory
I have tried to run the same command directly in the terminal and it is working. Both checkstyle-6.15-all.jar and checks.xml are where they should be.
What could cause this problem?
Change "${CHECKSTYLE}" to ${CHECKSTYLE} (without the quotes).
You are passing the entire value of the CHECKSTYLE variable as a single word (that's what the quotes do), so the shell is looking for a relative directory named java -jar, and is trying to find a file under that (nonexistent) directory with the path home/ec2-user/grader/ext/checkstyle-6.15-all.jar.
When you envoke "${CHECKSTYLE}" the shell thinks that is the command you are running. There is no such file name with the spaces and options have you have included there. If you envoke it simply as ${CHECKSTYLE} (drop the quotes) the shell will process it for whitespace as normal and split it into the appropriate pieces for creating the process.
In a former version of our application, we used the Eclipse launcher on Windows for startup. We hand the launcher an .ini file with java properties via the --launcher.ini property.
Now, we cannot use the launcher anymore and start the application from a .bat file using a java -jar <file> command. Therefore, the properties in the .ini file have to be added to this java command individually.
My idea is to have the .bat script read the properties from the .ini file and store them in a variable as list. The content of this variable is then used as part of the java command. How to do that in a robust way?
If you start it via a *.bat script I would first check if the file exists using:
if not exist myfile.ini (
# print some error
)
and if it exist I would iterate parse each line by:
FOR /F %i IN (myfile.ini) DO #echo %i
After that I would fill an array with the values and pass it to the java invocation.
I have batch file, named run.bat which inlcudes the following code:
#echo off
REM bat windows script
set CXF_HOME=.\lib\apache-cxf-2.2.7
java -Djava.util.logging.config.file=%CXF_HOME%\logging.properties -jar archiveServer-0.1.jar
When I execute this file on a command line it works perfectly. However when I try to execute within a java file with the following statement:
File path = new File("C:/Documents and Settings/Zatko/My Documents/Project-workspace/IUG/external/application/archive");
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", "start", "run.bat"}, new String[]{}, path);
I get the following error in the terminal window:
'java' is not recognized as internal or external command, operable program or batch file.
Where the error may be?
Java.exe is not found in your PATH.
If you can assume that the JAVA_HOME variable is defined, you can modify your batch file:
%JAVA_HOME%\bin\java -Djava.util.logging.config.file=%CXF_HOME%\logging.properties -jar archiveServer-0.1.jar
A better way to do it would be, as staker suggested, to set the PATH environment variable to contain %JDK_HOME%\bin
File workingDirectory = new File("C:/Documents and Settings/Zatko/My Documents/Project-workspace/IUG/external/application/archive");
String javaProgram = System.getProperty("java.home") + "\bin";
String[] command = {"cmd.exe", "/C", "start", "run.bat"};
String[] environment = {"PATH=" + javaProgram};
Process process = Runtime.getRuntime().exec(command, environment, workingDirectory);
As a third option, you can also avoid to have the batch file by invoking the main-class of the jar directly. You archiveServer would run in the same process, however. Maybe that's not want you want.
I suppose you didn't add JAVA_HOME/bin to your PATH environment variable.