Run CMD equivalent in OSX? - java

I'm using this code to make my Java program open a (visible) CMD window:
try {
String line;
Process p = Runtime.getRuntime().exec("cmd /C start \"Render\" \"" + myPath + "\\punchRender.cmd\"");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
jLabel7.setText(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
and I've been trying to do the same thing with the OSX terminal, this is where I'm at right now:
try {
String line;
Process p = Runtime.getRuntime().exec("sh " + myPath + "/punchRender.sh");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
jLabel7.setText(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
So far, no luck :( Any suggestions? The .sh file isn't even running...

I would just make sure your shell script has the execute bits on and just pass in the shell script file name.
Process p = Runtime.getRuntime().exec(myPath + "/punchRender.sh")
Edit:
I don't know Java specifically if there is anyway to set file permissions for Unix/Linux with it to set the eXecute bit or how to escape quotes. But It would be something like this:
Process chmod = Runtime.getRuntime().exec("chmod u+x \"" + myPath + "/punchRenderer.sh\"")

This should work. Not only running the script, but opening a terminal also:
Process p = Runtime.getRuntime().exec("open -a /Applications/Utilities/Terminal.app \"" + myPath + " /punchRender.sh\"");

If you want a new visible Terminal window, you can't run the shell directly. You need to start Terminal and then run a .command file, not a shell script. I'm not sure how hard it would be to connect the stdout of that command to your Java process. You might have to figure out some other way of getting the output into the terminal.
By the way, I tried your code in a class on my own Mac at home, and it ran a .sh file just fine. I was running the java class from the command line. Maybe sh just isn't in your PATH.

I assume you've checked that the .sh file is executable, haven't you?

Can I suggest you capture the standard error as well as the standard output, and dump that. That should give you some idea as to what's going on (it's good practise generally).
You may need to gather standard output and standard error in different threads to avoid blocking issues. See here for a StreamGobbler

Related

Why does the output of my shell script doesn't print in console?

I am trying to print the output of a shell script on the console using java. When I manually run the script, I get
C:/Users/user1/Desktop/shell.sh: line 78: /usr/ucb/ps: No such file or directory
<STATUS>: Probe [ devicename ] is not running!
But, when I try to run it on my Java program, the output is not being printed on the console.
My code is:
ProcessBuilder processBuilder = new ProcessBuilder("C:/Program Files/Git/git-bash.exe","C:/Users/user1/Desktop/shell.sh");
try {
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
System.out.println(line);
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
//abnormal...
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
The only output I am getting is "Success". When I debugged my code, I found that the code never enters the condition
while ((line = reader.readLine()) != null)
even though, in the bash terminal, there are lines of output. Why is this happening?
I am stuck at this point and I couldn't find any other explanations for this problem. Kindly help.
You are running git-bash.exe which opens as a windows application. Although Java has access to the stdout/stderr streams of git-bash.exe, these are not necessarily the same as the stdout/err of the internal launch of your shell script within git-bash.exe.
One way to see the stdout/err of your command would be to make a java friendly version of the .sh script which launches your original sh and redirects output to a specific files which you can then access within java afterwards.
ProcessBuilder pb = new ProcessBuilder("C:/Program Files/Git/GIT-BASH.EXE","/c/Users/blah/somescript.sh");
somescript.sh:
#!/bin/sh
runtheoriginalcommand > ~/somepath.out 2> ~/somepath.err
You could also add extra args to wrapper to pass the out/err files to be used so there is no contention with any other launches or hardcoded output files.

java execute external program and read its output

I want to read output from ADB logcat (https://developer.android.com/studio/command-line/adb). I have such code (test):
try {
Process p = Runtime.getRuntime().exec("sh -c adb logcat");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
while(true) {
String s = bufferedReader.readLine();
if(s == null) {
continue;
}
System.out.println("From process: " + s);
}
} catch (IOException e) {
e.printStackTrace();
}
But this prints nothing. If I execute this:
Process p = Runtime.getRuntime().exec("sh -c ls");
It works.
Okay, I too fast asked the question on SO.
I read error stream instead of inputstream and I find out that cannot find program ADB. Need to provide full path:
ProcessBuilder processBuilder = new ProcessBuilder("/home/UserName/Soft/Android/Sdk/platform-tools/adb", "logcat");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
But on another computer path is different. Also you can on every computer make symbolic link and in code use as path simply "adb":
sudo ln -s /home/UserName/Soft/Android/Sdk/platform-tools/adb /bin/adb
ProcessBuilder processBuilder = new ProcessBuilder("adb", "logcat");
This is not the best solution, partially resolves problem. Would be great without making symbolic link, using simply "adb" in instantiating ProcessBuilder object. Because in every disk location on my computer I can use ADB in terminal, it is visible but in this code no. In Windows I know that this is in System Environments > PATH. In linux I wrote command "printenv" but I don't see anything with android & adb.

Run shell script with Angular CLI commands from Java

I'm trying to run a shell script from Java (using Runtime.getRuntime().exec(cmd)). All commands in the script file seem to be running normally except the angular-cli (ng) commands.
My Java File:
System.out.println("Executing Script...");
final String[] cmd = new String[]{"/bin/bash", "test.sh"};
final Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println("Script output: " + s);
}
process.destroy();
System.out.println("Script Executed.");
test.sh:
#!/bin/bash
cd ~/ &&
ng new newAngularProject &&
Outout:
Executing Script...
Script Executed.
No errors are thrown. All other commands work but for some reason, I'm unable to run ng commands. Also, I've tested the file w/o running it from Java - When I run the same script directly on the console, it works perfectly and all commands (including ng commands) work neatly. I'm running on MacOS in case you wanted to know.
Also print the error stream. You will get the error message, if it is there.
final BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((s = errorReader.readLine()) != null) {
System.out.println("error: " + s);
}
Also you can try to use absolute path of ng in your test.sh e.g. /home/my/install/node-vxxx/ng, since the process spawn by java to run your command might not get the environment variable you set in your .bashrc /.bash_aliases

Calling sccmap using Java

I am trying to call the sccmap command from GraphViz using Java.
String command = "/usr/bin/sccmap -S /home/paperclip/Desktop/graph.dot > /home/paperclip/Desktop/scc.dot";
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(command);
System.out.println("Process exited with code = " + p.waitFor());
java.io.InputStream is = p.getInputStream();
java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
String s = null;
while ((s = reader.readLine()) != null) {
System.out.println(s);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
The output for this snippet of code is:
Process exited with code = 0
The sccmap function is supposed to output a dot file in the location that I have specified. However, it doesn't work like I expected. Even though the exitValue was given as 0, the dot file was not created.
I tried using the command manually in Terminal and it works perfectly. I also tried using other commands such as "ls" and it works too. Why does this code not work for "sccmap"?
I also searched for some Java APIs for GraphViz such as the jGraphViz (http://jgraphviz.sourceforge.net/) and graphviz-java-api at (http://www.loria.fr/~szathmar/off/projects/java/GraphVizAPI/index.php), but they don't seem to work for me.
FYI I am on Ubuntu 11.10 and GraphViz is already installed.
Thanks!
> is interpreted by the shell (it's a stream redirect), it's not an argument to the application. There is no shell in this situation.
Try /usr/bin/sccmap -S -o /home/paperclip/Desktop/scc.dot /home/paperclip/Desktop/graph.dot instead.

Show the CMD window with Java

I'm using this code to launch a .cmd file:
try {
String line;
Process p = Runtime.getRuntime().exec(myPath + "\\punchRender.cmd");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
It works fine, but I want to actually see the cmd.exe window running. How can I make it show? Any help would be greatly appreciated!
Instead of running your path, try actually running cmd.exe but using the build in start command to launch a new command window. You can see the full set of command line arguments by entering the following at a command prompt:
cmd/?
start/?
in your case, you probably want to execute something like the command:
cmd /c start c:\path\to\punchRender.cmd

Categories

Resources