I am in the process of making a GUI in Javafx for a Java based command line tool the one of my predecessors had made. The command line tool is a 2 step tool, first you run the program using a specific set of configurations after which the command line input is required to quit out of the program or for additional analyses. I am using the following block of code to run the first half of the command-line tool:
button = new Button("Run Proteinarium");
button.setOnAction( e -> {System.out.println("Running Proteinarium");
String command = "java -jar Proteinarium-master\\example\\Proteinarium.jar config=Proteinarium-master\\example\\config_SIM.txt";
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
);
However, after click on the button while the first half of the command-line tool does run, the GUI stops responding. I believe this is likely due to the second-half of the command-line tool requiring additional input in the command Terminal. Any ideas for how to fix this?
I am running this on Windows 10.
Related
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.
I need to retrieve a list of currently open programs using java. The following code gives me a list of all the programs that are active including any background processes however I need only a list of active programs.
try {
String line;
Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
I am not going to be aware what programs are currently open and so will not be able to find it by searching for a series of names, as I have seen some people recommend this method.
By active program I am meaning any program that is available to the user to interact with through a window. The task manager window already splits the programs(in detailed view) into apps and background processes and I would like to be able to retrieve any programs that would be sorted under the apps section.
add this command line
String command="powershell -command \"get-Process cmd | format-table mainwindowtitle\"";
and use it here
Process p = Runtime.getRuntime().exec(command);
You can use the windows cmd like this :
try {
String process;
// getRuntime: Returns the runtime object associated with the current Java application.
// exec: Executes the specified string command in a separate process.
Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe /fo csv /nh");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((process = input.readLine()) != null) {
System.out.println(process); // <-- Print all Process here line
// by line
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
If I start my java program in an OS native terminal (cmd.exe or xterm), is it possible to:
Keep the current terminal for reading/writing as System.in and System.out
Open another terminal for reading/writing as NewTerm.in and NewTerm.out
As it stands, all I can seem to do is open the second terminal. I cannot write to it (I've tried with BufferedWriter) and the only way it displays commands is if those commands were issued with its opening (i.e. Runtime.getRuntime("xterm ls") or ProcessBuilder(command).start(); where command is a String[]).
I would like to keep the two terminals open so that I can compare their outputs. The sequence would be as follows:
Run program in first terminal
Spawn second terminal
Issue command in first terminal
Issue command in second terminal
Read first terminal output
Read second terminal ouput
Is this possible?
Here is how I have opened a new terminal but cant write to it after opening:
public class InterFace {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("xterm");
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
w.write("ls");
w.flush();
w.close();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
while ((s = r.readLine()) != null) {
System.out.println(s);
}
}
catch (IOException io) {
io.printStackTrace();
}
}
}
You need the line separator after the command.
w.write("ls" + System.lineSeparator());
So writing a test storm topology with minimal Java experience, so I'm figuring things out in a brute force way. My experience writing storm topologies is also minimal.
I have three supervisor nodes on my cluster and want each of them to run ls in the terminal, funnel the output to a file and then return it to the nimbus node.
Firstly, how would i code an individual computer to run ls in the terminal? Funneling the output to a file is simple enough for me to figure out. I just don't know how to write programs that execute terminal commands.
Secondly, how do i tell each of my supervisor nodes to run ls individually?
You can use below snippet to run a command in shell. So use this same method to invoke the specific ls command using ssh in all supervisor nodes (from external node like nimbus).
public String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
Hope this helped.
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