Call ant from batch file - java

I want to call ant from a batch file.
#echo off
cmd.exe /k cd /d E:\work\CRM
call ant
It's not working. Nothing is happening; neither showing any error nor nothing... How do I call ant?
I need to execute ant compile command from batch file from the current location E:\work\CRM.
Also I set the environment variables for ant:
ANT_HOME
JAVA_HOME

Get rid of cmd.exe /k since you're already in a batch file context
Add -d after ant to show debugging info

Related

Unable to run a .bat file in java program

Problem Statement: I just want to start the HUB and Node to perform some tests using Selenium Grid.
I have two Batch files START HUB.bat and START NODE.bat which run perfectly when i manually run them.
But i want them to run using a Java Program #BeforeMethod.
I looked for answers
Runtime.getRuntime().exec("cmd /C start \"./BatchFiles/START HUB.bat\"");
This Opens Up the CMD but goes to the path of my .git project but doesnt run the bat file.
I tried using Process Builder but that doesn't open the cmd.
ProcessBuilder pb = new ProcessBuilder("cmd", "/C"," start", "START HUB.bat");
File dir = new File("D:\\work\\GIT REPOSITORY\\project.selenium.maven.jenkinsCI\\BatchFiles");
pb.directory(dir);
Process p = pb.start();
Can someone please help me with this issue. Below are the commands in the batch file.
D:
cd work
java -jar selenium-server-standalone-3.4.0.jar -role hub
So you want to execute the command line:
cmd /C start "./BatchFiles/START HUB.bat"
With cmd at beginning of the command line a new command process is already started with executing %SystemRoot%\System32\cmd.exe. This command process should automatically close after running the command as explicitly requested with option /C which means close as it can be read on running in a command prompt window cmd /?.
The command to execute in this command process is:
start "./BatchFiles/START HUB.bat"
The internal command start of cmd.exe is for starting an executable or script in a new process. Its help can be read on running in a command prompt window start /?.
The first double quoted string is interpreted by start as title of the new command process window opened when a batch file or a console application should be executed in a new command process.
And this is the reason why the batch file is not executed because "./BatchFiles/START HUB.bat" is interpreted as window title string.
And on Windows the directory separator is \ and not / as on Unix. / is used as begin of an option as you can see on /C. But Windows handles also file paths with / often correct because of replacing each / internally by \ in directory/file names with an absolute or relative path on accessing the directory or file.
So the solution is using either
Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" \".\\BatchFiles\\START HUB.bat\"");
or using
Runtime.getRuntime().exec("cmd.exe /C \"BatchFiles\\START HUB.bat\"");
A path starting with a directory or file name is relative to current directory of the running process on Windows like using .\ at begin of a directory or file name string.
The first code starts a command process which executes the command start which starts one more command process with title start hub executing the batch file. The first command process started with cmd.exe immediately terminates after running start while the batch file is executed in second started command process. This means your Java application continues while the batch file is executed parallel.
The second code results in executing the batch file in the single command process started with cmd.exe and halting the execution of the Java application until entire batch file execution finished.
The usage of a batch file can be removed here by using:
Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" /D D:\\work java.exe -jar selenium-server-standalone-3.4.0.jar -role hub");
With /D D:\work the working directory is defined for the command process started for executing java.exe with its parameters.
Alternatively without using start command:
Runtime.getRuntime().exec("cmd.exe /C cd /D D:\\work && java.exe -jar selenium-server-standalone-3.4.0.jar -role hub");
Run in a command prompt window cd /? for help on cd /D D:\work and see Single line with multiple commands using Windows batch file for an explanation of operator && used here to specify two commands to execute on one line whereby java.exe is executed only if cd could successfully change the working directory to D:\work.
class RunFile
{
public static void main(String[] arg){
Runtime runtime = null;
try{
runtime.getRuntime.exec("cmd /C start \"D:\\work\\GIT REPOSITORY\\project.selenium.maven.jenkinsCI\\BatchFilesmyBatchFile.bat\\START HUB.bat\"");
}
catch(RuntimeException e){
e.printStackTrace();
}
}
}
Did you try to pass the absolute path to the exec function?
As well as quote the path since you're having a whitespace between START and HUB
The explanation by #Mofi really helped here understanding as to how cmd treats each and every "/".
Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" \".\\BatchFiles\\START HUB.bat\"");
Above is the solution that worked for me.

Can run jar from commandline but not in shell script - why?

I want t start a jar which should provide a Rest-WebService.
When I run following command from terminal the jar and the webservice starts successfully:
java -jar SchnittprofilService-1.0-fat.jar BH121 8888
If I run the same command in a shell script start.sh, the jar starts but the not the webservice.
The permission for start.sh is set to 777.
Any suggestions?
Oops:
You have to put your arguments in quotation marks:
java -jar SchnittprofilService-1.0-fat.jar "BH121" "8888"

loop through files and execute jar with each file passed as arguments

cd /D "%~dp0"
#echo off
for %%f in ("Z:\java_projects\IDS\trunk\deployment\config\client\%1\Documentation\MappingEngine_*.xml")
do(
echo %%~nxf
java -jar EngineUtil.jar 192.67.64.172 %%f
)
Please tell me what I'm doing wrong.
I want to create a batch file to loop through a set files from a directory and run a jar file with one file at a time passed as argument.
The directory is dependent on a command line parameter.

exit bat file after an execution in cmd

Im running a selenium TestNG script using ANT in eclipse. The execution completes but the cmd prompt stays there even after the execution when i try to run the bat file from java. But when i run the bat file by double clicking it, the cmd exit after execution. Here below are the code to invoke the bat file and content of bat file:
Runtime.getRuntime()
.exec("cmd /c start run.bat", null, new File("C:\\Users\\nvithushan\\Desktop\\HSBC\\gen\\seleniumwebdriver\\HSBC_Demo"));
This is the content of the batch file.
#echo off
echo exectution starting
start ant
exit
Any help
It should be sufficient in your exec to do cmd /c run.bat and in your batch file just do ant.bat or call ant.bat. By definition the start command starts a separate cmd process. Note also the documentation:
If [the command you run] is an internal cmd command or a batch file
then the command processor is run with the /K switch to cmd.exe. This
means that the window will remain after the command has been run.

Current directory is invalid when running batch file

This is my run.bat file:
#echo off
start compactau.exe
"c:\program files\java\jre7\bin\java" -Xms512m -Xmx768m -cp jio.jar;log4j-1.2.9.jar;auagent.jar Auagent auagent.conf
Pause
When I run this file, I get the error the current directory is invalid. I uninstalled and reinstalled Java, then changed my Java location to c:\java\bin\java and adjusted my batch file like this:
#echo off
start compactau.exe
"c:\java\bin\java" -Xms512m -Xmx768m -cp jio.jar;log4j-1.2.9.jar;auagent.jar Auagent auagent.conf
Pause
But I still get the the current directory is invalid. How is this possible when I created the destination and folder name?
#echo off
start /D DBServerSimulator ./DBServerSimulator.exe
start /D DetectCheatServer ./DetectCheatServer/DetectCheatServerR
start /D ServerGuarder ./ServerGuarder/ServerGuarder.exe
start /D ManagerServer ManagerServer/ManagerServerR
start /D GateServer ./GateServer/Gate
start /D LocalSaveServer LocalSaveServer/LocalSaveServerR
start /D ShareMemProc ./ShareMemProc/ShareMemProcR
start /D GameServer ./GameServer/Line.exe
Start /D Auagent run.bat!
is my start.bat the last thing to start is my run.bat it starts the whole server boots up but because java wont kick in i get errors which i cant post coz it's image file only and i cant post them due to being a new user lol
you didn't specify a title to start. Use quotes for an empty title
start "" compactau.exe
Looks it is missing the directory where compactau.exe is stored.
Can you just add a line as
cd <path where the compactau.exe is stored>
e.g. cd c:\personal\exe_dir
before "start compactau.exe" so it can use the directory?
However the question is not clear enough in which line you get the error. Can you please mention it or post the error log?
and what are you going to do with Auagent auagent.conf, are they parameters to main method() ?
why don't you try by setting the path first before you run your java command?
You need not provide the whole absoulute path for using java command.

Categories

Resources