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.
Related
I wrote an application in java that needs five players and a server.
I need to write a script that executes the jar of the server and of every single player in different terminal windows. How can I do?
I tried a script and worked but the jar opened in the same terminal window than I tried with xterm or konsole with flag --noclose but does not work (warning command: konsole not found)
#! /bin/sh
xterm --hold -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/server_jar/adrenalina.jar
for X in $(seq 5)
do
konsole --noclose -e java -jar /Users/Marco\ 1/Documents/ing-sw-2019-Lentini-Marazzi-Marini/out/artifacts/client_jar/adrenalina.jar gui
done
exit;
To run a process in the background from bash, you'll need to add an & to the end of your command, e.g.
java -jar /path/to/jar/my.jar &
Otherwise bash will wait until the command execution terminates.
I am trying to execute a powershell script from my java console app, I was able to get this working with the below command:
Process p = Runtime.getRuntime().exec("cmd.exe /c start cd "+dir+" & start cmd.exe /k \"Powershell C:\\runscript.ps1 args\"",null,dir);
p.waitFor();
Inside my powershell script, i have the below snippet which gets called in a loop,
Start-Process -FilePath $RunLocation -ArgumentList $args -wait -NoNewWindow -RedirectStandardOutput $OutputFile
$output = Get-Content $OutputFile| out-string
if(!($output.toLower().contains("failed"){
Remove-Item $outLogFile
}
If I open command prompt, and copy exactly what I have in my exec(...) command, it runs great, however, when I run it in my Java application, it seems like the -wait in my powershell script is being ignored, and the next line (which is checking and removing logs) is run, I've even gone to the length of adding a sleep for a few seconds in my powershell just after the Start-Process, this works but I'm hoping there is a better way.
The error i am getting is in the powershell script is below (this only happens when ive run it from my Java app, the -wait waits foor the start process to finsih before continuing when run directly from command prompt..):
Remove-Item : Cannot remove item D:\adhoc\logs\2016-06\output38844448.out: The process cannot access the file
'D:\adhoc\logs\2016-06\output38844448.out' because it is being used by another process.
At C:\runscript.ps1:91 char:3
+ Remove-Item $OutputFile
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (D:\adhoc\log...4448.out:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Why is the -wait in my powershell script not working when I run it from my Java app using runtime().exec?
I am running this command with apache commons exec
C:\\Windows\\System32\\cmd.exe /C start ".\\test" /D ".\\test1" .\\test1\\ldecod.exe -p InputFile=\"test.h264\" -p OutputFile="test.yuv"
the command runs normally through command prompt i.e. starts ldecod.exe and decodes test.h264, but when running the same command through apache commons exec it just opens the folder "test1"
What's going on here?
edit: using the following code
String cmd = "C:\\Windows\\System32\\cmd.exe /C start \".\\test\" /D \".\\test1\" .\\test1\\ldecod.exe -p InputFile=\"test.h264\" -p OutputFile=\"test.yuv\"";
CommandLine commandline = CommandLine.parse(cmd);
DefaultExecutor exec = new DefaultExecutor();
exec.execute(commandline);
You need to add the /b argument to the windows start command. This will run the your command without starting a new command prompt.
From the docs:
/b - Starts an application without opening a new Command Prompt window. CTRL+C handling is ignored unless the application enables CTRL+C processing. Use CTRL+BREAK to interrupt the application.
The way you are running it, start is spawning a new process. I suspsect that ldecod.exe is running but the start command (and your program) does not terminate till you close the command prompt.
I need a multi command to bring the CMD to the root of the disc it runs on.
This is how the structure looks on the USB
\data
\data\commands
\Java
\Java\bin
App.jar
App.bat
This is how the "event" looks:
String command = "cmd /c cd\\data\\commands && wscript \"invisible.vbs\" \"Ready2Go.bat\"";
try {
Process p = Runtime.getRuntime().exec(command);
} catch (Exception e) {
e.printStackTrace();
}
This works perfekt when running App.jar file and "using" the java thats installd on the computer it self.
But when i try to run it with the java that i installd on the usb it will not work. (guide to install java on usb
app.bat file:
set Path=\Java\bin;%%Path%%
java -jar app.jar
So i need the a command to Leave the \java\bin dir. (for it is from there, I think the program is now running) and then run my command.
i have tried:
"cmd /c cd\\ && cd\\data\\commands && wscript \"invisible.vbs\" \"Ready2Go.bat\"
But without much luck.
I really hope you understand what I mean
write a startup batch start.bat like so
#echo off
rem change to this batch's folder
pushd "%~dp0"
set Path="%~dp0\Java\bin";%%Path%%
set JAVA_HOME=%~dp0\Java
java -jar App.jar
popd
and execute it as \data\commands\start.bat
Setting the current path of a process from java is done using ProcessBuilder and setting the directory-property. See a sample here
How to set working directory with ProcessBuilder
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.