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());
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 am writing a Java application in IntelliJ IDE. The application used Rserve package to connect to R and perform some functions. When I want to run my code for the first time, I have to launch R in the command line and start the Rserve as a daemon, which looks something like this:
R
library(Rserve)
Rserve()
After doing this, I can easily access all the function in R without any errors. However, since this Java code would be bundled as an executable file, so is there a way that Rserve() is invoked automatically as soon as the code is run so that I have to skip this manual step of starting Rserve using the command line?
Here is the code for the Class I wrote to get Rserve working from Java
public class InvokeRserve {
public static void invoke() {
String s;
try {
// run the Unix ""R CMD RServe --vanilla"" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("R CMD RServe --vanilla");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
// System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
I know this question has been asked a long back . I think You have the answer. But the below answer may help others. That's why I am posting my answer.
answer:- Instead of going again and again to the R console to start Rserve. One thing you can do is you can write a java program to start Rserve.
Below code you can use in a java program to start Rserve.
https://www.sitepoint.com/community/t/call-linux-command-from-java-application/3751. This is the link where you will get the code to run a linux command from java.I have changed the command only and posting below.
package javaapplication13;
import java.io.*;
public class linux_java {
public static void main(String[] args) {
try {
String command ="R CMD Rserve";
BufferedWriter out = new BufferedWriter(new FileWriter(
new File(
"/home/jayshree/Desktop/testqavhourly.tab"), true));
final Process process = Runtime.getRuntime().exec(command);
BufferedReader buf = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line;
while ((line = buf.readLine()) != null) {
out.write(line);
out.newLine();
}
buf.close();
out.close();
int returnCode = process.waitFor();
System.out.println("Return code = " + returnCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I have a computer algebra program (called Reduce) that works in the shell in an interactive manner: launch Reduce in the shell, then you can define variables, compute this and that, and what not. Reduce prints the output into the shell. My idea is that I want to build a frontend for this text-based program that evaluates its output and converts it into a nice LaTeX style formula. For this I want to use Java.
I can start Reduce via exec(). But how can I emulate text input to the opened shell, and how can I read back what Reduce writes into the shell?
Thanks
Jens
Edit 1: Current Code
// get the shell
Runtime rt = Runtime.getRuntime();
// execute reduce
String[] commands = {"D:/Programme/Reduce/reduce.com", "", ""};
Process proc = null;
try {
proc = rt.exec(commands);
} catch (Exception e) {
System.out.println("Error!\n");
}
// get the associated input / output / error streams
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
try {
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
try {
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
}
You need to get the streams associated with the process including the InputStream, OutputStream, and ErrorStream. You then can send messages to the process via the OutputStream and then read info from the process via the InputStream and the ErrorStream.
From some code of mine:
final ProcessBuilder pBuilder = new ProcessBuilder(TEST_PROCESS_ARRAY);
final Process proc = pBuilder.start();
procInputStream = proc.getInputStream();
errorStream = proc.getErrorStream();
errorSBuffer = new StringBuffer();
streamGobblerSb = new StreamGobblerSb(errorStream, "Autoit Error", errorSBuffer);
new Thread(streamGobblerSb).start();
final Scanner scan = new Scanner(procInputStream);
You may want to look into using the Process class.
http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
I believe you may be able to start the process, and then use getOutputStream() to feed commands into the process.
While this is not strictly an answer, I discovered that it is more convenient for me to stick with PHP's function proc_open(). That way I can include the output directly in the frontend and do not need to worry about the communication between my Java program and the html frontend.
For everybody who wants to stick to the Java method: the article http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html is a good reference.
I'm writing a Java program with multiple threads. One of the threads is responsible for reading lines from the standard input and parsing them.
Everything works well when running normally, but when the program is run in the background (in Linux) using:
$ java -jar my_jar_file &
my program hangs (at least until brought to foreground).
When running in the background I don't really need the standard input, but I also don't want my program to hang.
I searched for a way to programatically determine if the process is running in the background but could not find it.
Here's the code that reads from standard input:
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = null;
try {
while ((line = br.readLine()) != null) {
parseInputLine(line, br);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
As far as I can tell, the program hangs when it reaches the br.readLine() command.
I would guess the reader is blocking waiting for input you might be able to pipe in your input from the command line
readLine is a blocking call. So it is obviously waiting for input. If you want it to run in the background, then pass to it input from a file like this
java TakeInput < test.txt &
This where the class is as below and test.txt also shown below:
public class TakeInput
{
public static void main(String[] args)
{
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = null;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
write a text file as test.txt which has say
test1
test2
test3
I don't believe a Java program can determine if it's running in foreground or background.
I tried System.console(), with the following results:
Linux - returns null if stdin is redirected from a file, independent of foreground/background status
Windows - same thing;
Cygwin - seems to always return null
I suspect you will need to invoke the program from the command line as
java -cp ... ClassName < /dev/null
Try using .ready() function.
try {
if (stdError.ready())
{
while((line= stdError.readLine()) != null){
logger.error(line);
}
}
}
Do the same for the stdout.
To understand the situation, I try to describe what I'm trying to achieve:
I would like to create a java program that starts another java program (a minecraft server), monitors it's output, and sends commands to it's input accordingly.
I was able to start the jar program from my code, and I was also able to write an output listener for it, that currently only prints the jar program's output to the console.
The jar program I'm running from my code has a gui with a text output panel, where it shows messages, and an input field (textarea) where I can give commands. My problem is that when I start my code and it starts the program everything works fine until I want to write directly into the program's input field. If I type any letter into the input field, the gui freezes.
What do I wrong?
Here's my code:
public static void main(String[] args) {
try {
System.out.println("Starting.");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("java -jar \"D:\\Program Files\\mc\\minecraft_server.jar\"");
// output both stdout and stderr data from proc to stdout of this process
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());
errorGobbler.start();
outputGobbler.start();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
The output reader:
public class StreamGobbler extends Thread {
InputStream is;
// reads everything from is until empty.
StreamGobbler(InputStream is) {
this.is = is;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Thanks in advance! :)
First of all, instead of using the stream gobbler, just inherit the process's io:
final ProcessBuilder p1 = new ProcessBuilder(command).inheritIO();
p1.start();
If you don't care about stdin/out, use this to direct everything to NUL (windows version of /dev/null):
p1.redirectInput(Redirect.from(new File("NUL"))).redirectOutput(Redirect.to(new File("NUL")))
.redirectError(Redirect.to(new File("NUL")));
Does the program still crash?