Am calling a p = Runtime.getRuntime().exec(command) on Java 1.8.
The command calls a bash script as follows:
#!/bin/bash
args=("$#")
cat /home/user/Downloads/bigtext.txt | grep ${args[0]}
The above bash script works fine when called with the command line. When called within my Java application using exec() I get no CAT output back. it does not seem to get past p.waitFor(); Where am I going wrong?
If I run a command directly such as "ls -alF" every thing works as expected.
$ java -version java version "1.8.0_31" Java(TM) SE Runtime Environment (build 1.8.0_31-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
String tmp = "/home/user/search "+input;
System.out.println(tmp); // /home/user/search sometextarg
String woa = executeCommand(tmp);
private String executeCommand(String command) {
StringBuilder output = new StringBuilder();
BufferedReader reader = null;
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
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();
}
Your Java process and your script are deadlocked waiting on each other: you are waiting for the process to exit, the process is waiting for you to read the output.
It happens to work for ls -alF and other commands with small outputs because they all fit in the pipe's buffer (64kib on my system).
Just move the p.waitFor() below the while loop, and you'll have more luck:
private String executeCommand(String command) {
StringBuilder output = new StringBuilder();
BufferedReader reader = null;
Process p;
try {
p = Runtime.getRuntime().exec(command);
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
If you additionally want this to work correctly with values containing spaces and certain other characters, you have to:
Quote "${args[0]}" in your script.
Rewrite your executeCommand to use Runtime.exec(String[]) instead of Runtime.exec(String).
Related
I am sending some greek text ("αλεξ") to my python program and I expect to get a popup window showing "αλεξ" but instead I get "αλεξ". Then I print the text parsed in and get it back to my java program, the result is "αλεξ" again! I am assuming the problem is the charset on my python program but I read somewhere that python3 uses by default utf-8.
Here is my Java Program :
public void run() {
try {
Runtime rt = Runtime.getRuntime();
String commands = "python -u C:\\Users\\Alex\\Desktop\\PythonProjects\\inputOutput.py";
Process proc = rt.exec(commands);
// read from terminal of the program
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),Charset.forName("UTF-8")));
// write on the terminal of the program
BufferedWriter out = new BufferedWriter(s=new OutputStreamWriter(proc.getOutputStream(), "UTF-8"));
System.out.println(s.getEncoding());
boolean telegramStarted = false;
String s;
while ((s=in.readLine())!=null) {
System.out.println(s);
if (telegramStarted |s.equals("started")) {
out.write("αλεξ");
out.newLine();
out.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Here is the Python Script :
import ctypes
print('started')
while True :
name = input('Enter your name: ')
ctypes.windll.user32.MessageBoxW(0, name, "Greetings", 1)
print('Hello', name, '!')
I am running my Java program from terminal and I am trying to count the number of files in a certain directory using a linux command in my code; I have managed to get output for all other commands but this one.
My command is: ls somePath/*.xml | wc -l
When I run my command in my code, it appears that it has nothing to output, yet when I run the same exact command in terminal it works just fine and actually outputs the number of xml files in that directory.
Here is my code:
private String executeTerminalCommand(String command) {
String s, lastOutput = "";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
System.out.println("Executing command: " + command);
while ((s = br.readLine()) != null){//it appears that it never enters this loop since I never see anything outputted
System.out.println(s);
lastOutput = s;
}
p.waitFor();
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
return lastOutput;//returns empty string ""
}
Updated code w/ output
private String executeTerminalCommand(String command) {
String s, lastOutput = "";
try {
Process p = new ProcessBuilder().command("/bin/bash", "-c", command).inheritIO().start();
//Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
System.out.println("Executing command: " + command);
while ((s = br.readLine()) != null){
System.out.println("OUTPUT: " + s);
lastOutput = s;
}
System.out.println("Done with command------------------------");
p.waitFor();
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("LAST OUTPUT IS: " + lastOutput);
return lastOutput;
}
output:
Executing command: find my/path -empty -type f | wc -l
Done with command------------------------
1
LAST OUTPUT IS:
To execute a pipeline, you have to invoke a shell, and then run your commands inside that shell.
Process p = new ProcessBuilder().command("bash", "-c", command).start();
bash invokes a shell to execute your command and -c means commands are read from string. So, you don't have to send the command as an array in ProcessBuilder.
But if you want to use Runtime then
String[] cmd = {"bash" , "-c" , command};
Process p = Runtime.getRuntime().exec(cmd);
Note: You can check advantages of ProcessBuilder here and features here over Runtime
I am building a javaFx application I am running a command using java process builder class.
ProcessBuilder builder = new ProcessBuilder(runCommand);
builder.redirectErrorStream(true);
Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
I am trying to get the output from the process I have tried two different ways but I am not able to get it.
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream ();
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
String line1=null;
while ((line1 = reader.readLine()) != null) {
System.out.println(line1);
}
BufferedReader errorred = new BufferedReader (new InputStreamReader(stderr));
while ((line1 = errorred.readLine()) != null) {
System.out.println(line1);
}
I tried also this:
ProcessBuilder builder = new ProcessBuilder(runCommand);
//String outputLogs = new VerboseProcess(new ProcessBuilder(""));
builder.redirectErrorStream(true);
builder.redirectOutput(Redirect.INHERIT);
builder.redirectError(Redirect.INHERIT);
builder.redirectInput(Redirect.INHERIT);
Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
I believe the error is somewhere else. Following snippet is based on your posted code
ProcessBuilder builder = new ProcessBuilder("java.exe", "-version");
builder.redirectErrorStream(true);
Process process = builder.start();
process.waitFor();
int exitValue = process.exitValue();
System.out.println("exitValue = " + exitValue);
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
String line1 = null;
while ((line1 = reader.readLine()) != null) {
System.out.println(line1);
}
BufferedReader errorred = new BufferedReader(new InputStreamReader(stderr));
while ((line1 = errorred.readLine()) != null) {
System.out.println(line1);
}
and produces the following output on the console
exitValue = 0
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
If the command specified in new ProcessBuilder(command...) does not exists or is not in the PATH an IOException is thrown
Cannot run program "...": CreateProcess error=2, The system \
cannot find the file specified
If you don't get any output. Following could be the reason:
the process has not printed anything to stdout/stderr
your runcommand start the process in a background process, e.g. on Linux/OSX command & or on Windows start command.exe
Hello i have some question about java.
here is my code:
public static void main(String[] args) throws Exception {
Process pr = Runtime.getRuntime().exec("java -version");
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
pr.waitFor();
System.out.println("ok!");
in.close();
System.exit(0);
}
in that code i'am trying to get a java version command execute is ok, but i can't read the output it just return null. Why?
Use getErrorStream().
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
EDIT:
You can use ProcessBuilder (and also read the documentation)
ProcessBuilder ps=new ProcessBuilder("java.exe","-version");
//From the DOC: Initially, this property is false, meaning that the
//standard output and error output of a subprocess are sent to two
//separate streams
ps.redirectErrorStream(true);
Process pr = ps.start();
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
pr.waitFor();
System.out.println("ok!");
in.close();
System.exit(0);
Note that we're reading the process output line by line into our StringBuilder. Due to the try-with-resources statement we don't need to close the stream manually. The ProcessBuilder class let's us submit the program name and the number of arguments to its constructor.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProcessOutputExample
{
public static void main(String[] arguments) throws IOException,
InterruptedException
{
System.out.println(getProcessOutput());
}
public static String getProcessOutput() throws IOException, InterruptedException
{
ProcessBuilder processBuilder = new ProcessBuilder("java",
"-version");
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
StringBuilder processOutput = new StringBuilder();
try (BufferedReader processOutputReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));)
{
String readLine;
while ((readLine = processOutputReader.readLine()) != null)
{
processOutput.append(readLine + System.lineSeparator());
}
process.waitFor();
}
return processOutput.toString().trim();
}
}
Prints:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
You already have the process-object (name pr). You can get the Input-, Output- and Errorstream. In your case you want pr.getInputStream(). Read from that, that is connected to the output of the process.
try this
public static final Pair<Integer,Integer> javaVersion(File file) throws IOException {
final ProcessBuilder pb = new ProcessBuilder("java", "-version");
pb.directory(new File(file.getCanonicalPath() + File.separator + "bin"));
pb.redirectErrorStream(true);
// Call the test target
final Process process = pb.start();
final InputStream in = process.getInputStream();
final InputStream err = process.getErrorStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String s = bufferedReader.readLine();
int start = s.indexOf('\"');
int end = s.lastIndexOf('\"');
String substring = s.substring(start + 1, end);
String[] split = substring.split("\\.");
return new Pair<>(Integer.parseInt(split[0]),Integer.parseInt(split[1]));
}
I also suffered this issue because I didn't set $JAVA_HOME correctly. (I forgot Contents/Home).
After I edit $JAVA_HOME, update Gradle JVM, and remove .idea directory to re-build with gradle, It works well.
In Java, I want to be able to execute a Windows command.
The command in question is netsh. This will enable me to set/reset my IP address.
Note that I do not want to execute a batch file.
Instead of using a batch file, I want to execute such commands directly. Is this possible?
Here is my implemented Solution for Future Reference:
public class JavaRunCommand {
private static final String CMD =
"netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0";
public static void main(String args[]) {
try {
// Run "netsh" Windows command
Process process = Runtime.getRuntime().exec(CMD);
// Get input streams
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// Read command standard output
String s;
System.out.println("Standard output: ");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read command errors
System.out.println("Standard error: ");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
Runtime.getRuntime().exec("netsh");
See Runtime Javadoc.
EDIT: A later answer by leet suggests that this process is now deprecated. However, as per the comment by DJViking, this appears not to be the case: Java 8 documentation. The method is not deprecated.
Use ProcessBuilder
ProcessBuilder pb=new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process process=pb.start();
BufferedReader inStreamReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while(inStreamReader.readLine() != null){
//do something with commandline output.
}
You can run the command with Runtime.getRuntime().exec("<command>") (eg. Runtime.getRuntime().exec("tree")). But, this will only run executables found in path, not commands like echo, del, ... But only stuff like tree.com, netstat.com, ... To run regular commands, you will have to put cmd /c before the command (eg Runtime.getRuntime().exec("cmd /c echo echo"))
public static void main(String[] args) {
String command="netstat";
try {
Process process = Runtime.getRuntime().exec(command);
System.out.println("the output stream is "+process.getOutputStream());
BufferedReader reader=new BufferedReader( new InputStreamReader(process.getInputStream()));
String s;
while ((s = reader.readLine()) != null){
System.out.println("The inout stream is " + s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
This works.
Runtime#exec().