I am trying to run an executable .sh file from Katalon using the below code, but my mac terminal is not opening and running the file, any idea how to perform this operation?
Runtime.getRuntime().exec("./Automation.sh");
Test gets passed but terminal will not open and run the script !!
process = Runtime.getRuntime()
.exec(String.format("sh -c ls %s", homeDirectory));
Please find that this sample executes sh command (possibily bash executable) which will be found in the $PATH environment variable of OS, so it will successfully run.
In you code you passed a String "./Automation.sh" which will not be found in the $PATH, and it is not executable, so your code will never run.
The âTerminal app of Macâ has nothing to do with your case.
When a Java program calls Runtime.getRuntime().exec(..), it speaks to the OS on which the program is running. It does not speak to the âTerminalâ app at all.
Let us not confuse people.
Although you are right, the exec() will speak to the OS (and not call terminal which is yet another app), when we speak about sh scripts we have to first talk about shells.
$PATH it is just ⊠a path where the OS may look for various executableâs (binaries) but not a binding for a specific one.
But, in the case of Linux at least, the sh binding it is not made in the $PATH, but in the environment in use, which may vary from user to user ($PATH is just a variable inside this guy).
It is an alias which can point to various executables (bash, zsh, csh and so on).
See:
Most probably same it is on Mac, since it is also an *unix OS despite whatever fans may think.
So, knowing your OS in depth may help you to avoid headache.
But yeah, @kazurayam 's comment are somehow valid ⊠pay more attention when using code snippets found here and there âŠ
LE: and of-course, all from above can be enforced to a specific shell <> terminal for a specific script through the use of a shebang but i will let you read more about this by using various searches âŠ
@surajigure09 this is because the command gave to you:
.exec(String.format("sh -c ls %s", homeDirectory));
will run a shell but not a terminal session (read my above notes and the insights from @kazurayam again )
If you really need to open terminal, consult your OS docâs on how to run Terminal from command line and pass a script as an argument.
Although, why do you need that? Does your script wait for user input?
Do you want to open a window of Terminal.app on Mac by your Java/Groovy program?
I do not know how to write a Groovy/Java program that opens a window of Terminal app on Mac. To be precise, I do not know the absolute path of the executable âTermianl appâ, and I do not know if it is possible to launch it in the command line.
I am not interested in it, as I can open a window of Terminal.app with my finger by clicking this icon on the desktop of Mac. Itâs enough for me.
@kazurayam - Yep, thatâs correct, I want to open the terminal and run the .sh shell script file by using the java/groovy program.
I did the same on a windows machine with a .bat file and works fine over there, I am trying to do the same on mac.
I have all data like token, path and other variables set in a properties file, so when i run the .sh file, it will pick the info from there and execute it. Manually running from terminal works fine.
@anon46315158 - I understand your book knowledge and what u r trying to say, but you are not giving me a solution please give a solution or say if it can be achieved or notâŠ
@anon46315158 u want to see a video of what I am doing in windows, without knowing what I am doing how can u say the defective behaviour of the Windows command interpreter.
give me a solution on how to open a terminal using groovy or java program, or rather say u donât know and keep quiet plz.
i allready gave u solutions.
and you still didnt answered to the most relevant question.
why do you exactly need such?
for the rest just keep dreaming that how a script behaves on windows platform is the sane wayâŠ
In the screenshot below you can see the test case I am trying to run, I need help with a proper command/program to run the shell script from katalon. Even if the terminal is opening or not, at least if it can display inside the console if the sh script is even executing. The .bat file I am referring in screenshot is to show you that it works in windows with that line of code. Looking something similar on mac.
In the Katalon Studio projectsâ folder, I made a shell script <projectDir>/Automation.sh:
#!/bin/sh
# call this function to write into stderr
echo_stderr()
{
echo "$@" >&2
}
# write to stdout
echo this is stdout
# write to stderr
echo_stderr this is stderr
I made a Test Case script Test Cases/TC1:
import java.nio.file.Path
import java.nio.file.Paths
/**
* https://www.baeldung.com/java-process-api
* 8. Handling Process Stream
*/
Path projectDir = Paths.get(System.getProperty("user.dir"))
Path shellFile = projectDir.resolve("Automation.sh")
Process process = new ProcessBuilder("sh", shellFile.toString()).start()
BufferedReader stderr =
new BufferedReader(
new InputStreamReader(process.getErrorStream()));
BufferedReader stdout =
new BufferedReader(
new InputStreamReader(process.getInputStream()));
String errString = stderr.readLine()
String outString = stdout.readLine()
System.out.println("stderr: " + errString)
System.out.println("stdout: " + outString)
When I ran TC1, I saw the following output in the Console of Katalon Studio GUI:
2022-03-05 09:20:02.850 DEBUG testcase.TC1 - 8: out.println("stderr: " + errString)
stderr: this is stderr
2022-03-05 09:20:02.878 DEBUG testcase.TC1 - 9: out.println("stdout: " + outString)
stdout: this is stdout
Here you can see the output from the subprocess (the shell script) is consumed by the parent process (Test Case).
I do not need to open a window of âTerminal.appâ at all. This code will work just the same on Mac, Linux and Windows.
By default, the created subprocess does not have its terminal or console. All its standard I/O (i.e., stdin, stdout, stderr) operations will be sent to the parent process. Thereby the parent process can use these streams to feed input to and get output from the subprocess.
Consequently, this gives us a huge amount of flexibility as it gives us control over the input/output of our sub-process.
I have developed a library that wraps java.lang.ProcessBuilder, which implements various helper methods; especially useful when you want to startup & shutdown HTTP server process in the command line from Java app.
Shellâs redirection operator > does not work in a Java/Groovy program.
I wrote a Test Case of single line:
process = Runtime.getRuntime().exec("sh -c ls . > out.txt")
I ran it. It passed. But the file out.txt was not created.
Why?
I would tell you why.
When Runtime.getRuntime().exec("sh -c ls .") ran, it creates a subprocess. The output stream from the subprocess is implicitly redirected to the parent process (the Test Case in Katalon Studio). The above single line script does not consume the stream, therefore the content of the stream is eventually thrown away.
The Runtime.getRuntime().exec( ... )does not understand the > symbol at all. The > is a special symbol understood only by Shell command line interpreter. The Runtime.getRuntime().exec( ... ) does NOT behave the same as a Shell command line interpreter.
I wrote a variation.
process = Runtime.getRuntime().exec("sh -c ls .")
BufferedReader stdout =
new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line
while ((line = stdout.readLine()) != null) {
println line
}
When I ran this, I saw the following output in the Console
2022-03-06 09:46:20.102 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/TC1
Checkpoints
Data Files
Drivers
Include
Keywords
Libs
Object Repository
Plugins
Profiles
Reports
Scripts
Test Cases
Test Listeners
Test Suites
VisualInspectionOfExcelAndPDF.prj
bin
build
build.gradle
console.properties
settings
store
2022-03-06 09:46:20.816 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/TC1
The lines after BufferedReader ... do the same processing as the > in the Shell interpreter.
Cumbersome? ⊠Yes, it is really cumbersome.
This demo reminds us how nicely the âUNIX Shell interpreterâ was designed by Ken Thompson in 1970s. It is really amazing.