Java runtime not able to run command on mac - java

I installed ffmpeg on mac through brew install. When i run ffmpeg command from terminal, it is running fine. But when i create a program in java and execute command like
Runtime.getRuntime().exec("ffmpeg");
It is throwing IOException
Cannot run program "ffmpeg":error=2, No such file or directory.
Any idea how to resolve it?

Try using the below code
String[] command = new String[]{"/bin/bash","-c","ffmpeg -version"};
Process p = Runtime.getRuntime().exec(command);

Related

Calling Emacs from Java file

For vhdl beautifier, I am using Emacs and in Linux I am using the command in terminal:
emacs --batch --visit=FILENAME.vhd --funcall=vhdl-beautify-buffer --funcall=save-buffer
And this working fine, however when trying to call this command within a java file as below not working! :
String command = "emacs --batch --visit=FILENAME.vhd --funcall=vhdl-beautify-buffer --funcall=save-buffer";
Process p = Runtime.getRuntime().exec(command);
what can be causes for that?

Cannot run program "adb": error=2, No such file or directory while executing through eclipse

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();

Lauching Cygwin terminal from a JAVA program

I am working on Windows and I have downloaded cygwin.
I want to launch the cygwin terminal from a java program but I can't find the command so I can launch the terminal and then write line command into it.
When I go to the cygwin Terminal icon parameters, I can see this :
target: C:\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico -
But when I use this to launch the terminal on my program:
ProcessBuilder pb = new ProcessBuilder("C:\\cygwin64\\bin\\mintty.exe", "cd Documents");
I have a shell that opens wit this :
cd Documents: No such file or directory
I think that my problem is that I launch the wrong cygwin terminal but I can't find where is the right Cygwin terminal.
Does anyone knows where is the right cygwin terminal so I can write line commands in my java program and the temrinal will understand them ?

Java: execute command in a new cmd.exe as administrator

I want to execute a command from a Java-application using
Runtime.getRuntime.exec(command);
but the command need Admin-privileges. If I use
runas /user:Administrator "cmdName parameters"
nothing happens because I need to give user und pw as parameter to the command.
But I need to run a command to the cmd, so that a new cmd.exe starts as administrator and asks if I want to run cmd.exe as admin. After agree the command should be run in the admin-cmd. So like this:
String command = "popupNewCmdAsAdminAndRun "batWhichNeedsAdmin.bat" "
Runtime.getRuntime.exec(command);
Has anyone an Idea?
Thanks in advance!
you should do the following
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "command";
process.StartInfo.Verb = "runas";
process.Start();
process.WaitForExit(60000);
Start Command Prompt (CMD) from java as Administrator and Run command prompt as Administrator - or the silly way, try to pipe it:
pw | command
Research building projects and the executable jar file. Usually when you build a project, it gives you a commandline that can be used to execute the jar file in folder dist located in the projects folder. You can copy and paste the command line in cmd. But you could also use a bat file, or convert the executable jar file into an exe with the jar2exe converter. Not completely sure of the name because i am not posting on my computer.

how to run shell script in java using Cygwin

From a long time i am struggling for with this program. I am having a shell script which accepts parameters as version number and path for the files. then that script creates Zip file with the name of version number congaing all file files to the folder.
I have installed Cygwin on following path D:/cygwin. I am coping required files to same location where cygwin is installed D:\cygwin\bin
Command
D:/cygwin/bin/bash -c '/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files
Or Can any one please suggest how to run shell script in java using Cygwin.
Rewriting the Problem:-
When i am trying to run following Command in command prompt it gives error
sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
Error:-C:\Documents and Settings\sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
/usr/bin/app.sh: line 51: lib/lib.sh: No such file or directory
But if i run the same command at
D:cygwin\bin\Test>sh app.sh AK-RD 02.20 D:\cygwin\bin\Test_files
It works fine. Can any one suggest me how to avoid this kind of errors.
Runtime run = Runtime.getRuntime();
Process p = run.exec("D:/cygwin/bin/bash -c \'/bin/test/app.sh 04.10 D:\cygwin\bin\ Test_files");
p.waitFor();

Categories