I am using the following code to execute an SH file in Linux
Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", file.getPath() });
This code works however I can't use the screen command in the SH file when executing it from Java, I get this error
Must be connected to a terminal.
So is it possible to "connect" java to a terminal? I would like to be able to also view this screen when I connect via SSH so I think it has to be connected to the terminal that is shown when you SSH into the server.
I found that if I use screen -dm it will work. Thanks!
Related
I wanted to start appium server programatically using java in Mac.
I am able to open terminal with following code:
String cmd= "osascript -e \"tell app \\\"Terminal\\\" to activate\"";
List<String> command1=new ArrayList<String>();
command1.add("/bin/sh");
command1.add("-c");
command1.add(cmd);
ProcessBuilder pb=new ProcessBuilder(command1);
pb.start();
But I am not able to send any command in terminal.
Please suggest any solution.
Thanks in advance. :)
It seems you are trying to open Terminal app, which is not required rather you can try this:
There are many ways to do start appium programatically:
1. Create a text file and write command: export PATH=$PATH:/usr/local/bin; /usr/local/bin/appium & -g /tmp/app.log --command-timeout 90 and save this file with .sh extension say test.sh and give executable permission to this file like chmod +x test.sh
2. Now you can execute this command using your java code like: Runtime.getRuntime().exec("<PATH_TO_FILE>/test.sh");
Thats all. You can check if appium is running by browsing localhost:4723,
if you are writing further tests, you need to wait until appium server is started in your code.
I am trying to automate android devices using appium in Mac machine(Yosemite OS).
I downloaded and set all the required PATHS like sdk,build-tools,tools,paltform-tools,platforms and able to run the adb commands through terminal sucessfully.
But I written sample below java code
**Process p = Runtime.getRuntime().exec("adb devices");**
Getting output:
Cannot run program "adb": error=2, No such file or directory**
I am unable to figure out the exact problem, why it is working through terminal and why i am getting error through eclipse even I set path for everything.
Could you please any one suggest me what exactly the issue.Please do the needful.
could you please try the following line:
Process p = Runtime.getRuntime().exec(new String[]{"bash", "-l", "-c", "adb devices"});
My answer is based on another link in stackoverflow which resolved my problem and it sounds very similar to yours:
https://stackoverflow.com/a/54923150/3439297
I faced this issue with IntelliJ community edition+ Mac combo. But the reason seems the same, try to invoke your IDE (Eclipse) using the command prompt (Via Terminal) so that it can use the system paths, and in turn recognize adb, you mentioned that adb works from terminal so once the IDE launches from terminal again the paths would be honored.
You can use following code on Android:
To enable the WIFI:
String ADB=System.getenv("ANDROID_HOME");
String cmd = "/platform-tools/adb shell am broadcast -a io.appium.settings.wifi --es setstatus enable";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(ADB+cmd);
pr.waitFor();
To Disable the WIFI use:
String ADB=System.getenv("ANDROID_HOME");
String cmd = "/platform-tools/adb shell am broadcast -a io.appium.settings.wifi --es setstatus disable";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(ADB+cmd);
pr.waitFor();
It is possible to execute external commands in java at run time.
However, what I am looking for is to actually open cmd or terminal and show that executing external command on screen.
I tried to execute a jar console application as java -jar jartool arguments, it works both on Windows and Ubuntu. The only problem is that, this does not open cmd or terminal separately. it runs the command in background.
We can use either ProcessBuilder and the start process or simply Runtime.getRuntime().exec.
I need to forcefully open the cmd or terminal. Java is configured to show console on windows but still no luck.
Any idea why this is happening or what should I do to force it to open cmd or terminal independent of current cmd or terminal.
I am currently working on a Java Applet which is supposed to start a telnet session via command line. My current approach is to run:
String connectionString = "cmd /c start cmd.exe /k \"telnet\"";
Runtime.getRuntime().exec(connectionString);
When I execute this, cmd.exe opens but displays "telnet is not recognized as an internal or external command, operable program or batch file".
I do have telnet set up, there is no problem opening a cmd window and executing telnet there. I also tried to run the above snippet with other programs (rasdial, jarsigner) and it perfectly works.
Why would the cmd.exe not recognize telnet when started from java? Any help highly appreciated!
If the JVM is 32-bit on a Windows 7 system, then according to this post
...on a 64-bit Windows 7 system, telnet only works when launched from a 64-bit application....
You can try Apache Commons Net API which support telnet protocol. You can refer this sample example. You can also refer this reference guide.
I have a simple server application, which I would like to run in the background. The following line works for me:
Runtime.getRuntime().exec("cmd /c start java -jar ..\\server\\server.jar -Dlog4j.configuration=file:src\\test\\resources\\log4j.properties -filename src\\test\\resources\\server.properties");
But it displays the cmd window and I am unable to destroy it. So I would like to use
Runtime.getRuntime().exec("java -jar ..\\server\\server.jar -Dlog4j.configuration=file:src\\test\\resources\\log4j.properties -filename src\\test\\resources\\scIntegration.properties");
But it simply doesn't connect to the server. So why is that?
A related question. How do I end the process? It is a server that "doesn't end". So I have to kill it and I would assume, that running the java only command would be capable to be destroyed, but with the cmd I have no luck there.
You should split your command into an array in which first argument is the actual command to run and all the rest are command like arguments:
Runtime.getRuntime().exec(new String[] {"/usr/bin/java", "-jar", "..\\server\\server.jar" ...});
Try using an absolute path to the java programm.
For destroying: exec() returns a java.lang.Process, which you should be able to destroy. If not, you have to implement some type of callback to shut your server down, e.g. listening on a specific prot for a shutdown command.
The server is outputing something to stdout and in the shortened command version it didn't have a place to output, so it got stuck while trying to output some data. The solution is to pipe the stdout to eg some file.