In one commend , I'm trying to send data to System.out like this:
And in another command I'm trying to get this data from System.in.
It's strange because, it works once of many tries. I can try to run it 10 times and it's still inReader.ready() == false, and when I run it for example 11th time , it works.
Why ? How can I fix this? How to make it work everytime ?
Thanks, in advance !
You can't read your InputStream that way, since the data may not have been arrived at the second process yet. You can either read character by character, with something like:
InputStreamReader inReader = new InputStreamReader(System.in);
int data = inReader.read();
while (data != -1){
...
data = inReader.read();
}
or simple read the input line by line, using:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while ((String line = br.readLine()) != null) {
...
}
If your objective is to execute a shell command, don't use System.out but Runtime.getRuntime().exec(cmd) instead. Check out this question for more details.
Related
Hy.
I've created a routine that read .tgz files from a directory and unzip each one. I'm using
Process zip01 = Runtime.getRuntime().exec("LINE OF COMMAND");
and
exitVal = zip01.waitFor();
I,m using 7z.exe from its folder to decompress and compress files. The command line is working fine. Now, I what to read the percentage of the decompress and throw it into a textfield or a textarea. The graphics part are ok too, s well all the routine. The only dificult is to get the realtime percentage of the 7z. is there some way to read and show it?
Thanks!
You can get the output of your process like this:
Process zip01 = Runtime.getRuntime().exec("LINE OF COMMAND");
BufferedReader output = new BufferedReader(new InputStreamReader(zip01.getInputStream()));
String line;
while ((line = output.readLine()) != null) {
/* process lines */
}
I'm trying to use the Java Runtime.getRuntime().exec(String) command to run Festival, then use OutputStreamWriter to write some commands to the outpustream of the process.
This works great, and I'm able to do something like this:
Process p = Runtime.getRuntime().exec("festival");
Writer w = new OutputStreamWriter(p.getOutputStream());
w.append("(SayText \"Hello World\")");
w.flush();
Obviously the way I can tell this works is that it speaks the text through the speakers.
What I am having a real hard time doing is getting the text output from what I would see in the terminal. I'm trying to run some other commands (such as (voice.list)) which output text, presumably to stdout.
For example, I've tried using a BufferedReader in the following way:
BufferedReader reader = new BufferedReader (new InputStreamReader(p.getInputStream()));
w.append("(voice.list)");
w.flush();
String output = "";
String line = reader.readLine();
System.out.println(line);
while ((line = reader.readLine()) != null)
{
System.out.println("Reading: " + line);
output += line;
}
(The System.out.println's is just for debugging, I would do the entire thing in a cleaner way if I was able to get it to work.)
No matter what code I try, I'm never able to get any output from Festival. I can get output from other commands. E.G. I have tried this code as well http://en.allexperts.com/q/Java-1046/2008/2/Runtime-getRuntime-exec-cmd.htm and it works with many other commands (like ls) but not Festival.
Does anything have any idea how I would be able to get this to work?
Thanks.
Festival may output it's text on stderr instead of stdout. Try replacing
p.getInputStream()
with
p.getErrorStream()
i'm trying to execute a command with root privileges, i try this(only for example):
Process process = Runtime.getRuntime().exec(new String[]{"su","-c","whoami"});
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result += line;
Log.d(TAG,"RESULT:"+result);
}
But i need to call it in a cicle with hundred times, so this way is very slow and always is showing a dialog with the message of the root privileges are granted, so how i can create a sigle su process for write and read consecutively using input and output streams? i know to use both but i dont know how to write and the read the result of the command. Thanks for your help.
I want to know the owner of current process in Unix using Java. I want to find the current server's owner name. I tried with running "who am i" command in Runtime.getRuntime().exec(), but its not returning me any results.
String line = "";
Process p = Runtime.getRuntime().exec("who am i");
InputStream iStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(iStream);
BufferedReader bufReader = new BufferedReader(inputStreamReader);
while ((line = bufReader.readLine()) != null) {
System.out.println("Input "+line);
}
Is there anything wrong with this code or any idea how can I find the owner of current process using Java?
First thing, I think System.getProperty("user.name") should work for that
Second thing, the reason your code is not returning anything is because the command is whoami with NO SPACES so your exec line should be (assuming you are running on windows through cygwin or on a **nix based system)
Process p = Runtime.getRuntime().exec("whoami");
I am executing a simple java application in eclipse.
Sample code:
BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
String line;
while((line = input.readLine()) != null && line.length() != 0)
{
System.out.println("------"+line);
}
In the above code the readline() method hangs when reading the last line of my input.
I have gone through some threads and understood that it waits for the end of line.
I don't want to give any '\n' or '\r' at the end of my input in the console.
so how to handle this in the code.
How is the code to know when you are not going to type anything else unless you press enter?
There is no solution to this issue. You need to re-think your use-case.