i need to sort a csv file by the first column, which is a timestamp. I've been trying to do this with the following code, but the inputstream of the process p is always just blank:
Process p = Runtime.getRuntime().exec("sort -k1,1 -t, Bucket_Stats.csv");
p.waitFor();
// read this file into InputStream
InputStream in = p.getInputStream();
OutputStream output = new FileOutputStream("Sorted_Bucket_Stats.csv");
System.out.println(IOUtils.copy(in,output));
output.flush();
output.close();
Instead of handling the output in Java, you can use the
-o or --output=FILE
flag of the sort command and pass a filename for the output.
If you pass the same filename as the input, it will be overwritten.
Related
I'm trying to write a output file of a python script using java exec, however I get no output.
I got the expected file however is empty.
I made a script do what I want.
#!/bin/bash/
cd /home/taste/work/AIR/air/
python configure -f .air_config
I have already tried to execute this script from the shell and I can get the output.
Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","sh
./Configuration_Data/Scripts/"+cF.folderName+"/testConfigure.sh >
configureOut.txt"});
This is the way I'm reading :
String line ="";
//Used to create output
StringBuilder text = new StringBuilder();
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
text.append(line+"\n");
System.out.println(line);
}
I would like to have output from my python file.
Thank you.
Since you direct the script's output with > configureOut.txt to the file, there's simply nothing left to read from the input stream. If you really want the output in both the file and the input stream, you could change the above redirection to | tee configureOut.txt.
my java method looks like this:
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
String line = "";
Process proc;
try {
proc = Runtime.getRuntime().exec(command);
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
If I send small commands like "ls -l" it executes the command and print the results. But I need this function to read the output from an application wich will run for days and it will print frequently output while running. So I can't wait until the application is "done", I need the output in realtime. Anyone knows how to print the output without delay? thanks for help..
The simplest way is to redirect the output of the command directly to a file:
ls -l > ./ls.output
In this case you don't need to wait the end of the command. It will be the operating system to handle the output redirection to a file.
If you need also to write the output for the error you need a command like the following:
ls -l > ./ls.output 2> ./ls.err
where ./ls.output is the normal output and ./ls.err is the output for errors
If I call a command line process like this:
Process proc = Runtime.getRuntime().exec("foo -bar");
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
How do I enter values in it if it's interactive?
Use proc.getOutputStream() to obtain an OutputStream to which you can write the shell script inputs.
I am calling jar in a java program. the inner jar returns some output. how should i read and display in following program ?
i am able to call the jar successfully but how to display the output ?
import java.io.InputStream;
public class call_xml_jar {
public static void main(String argv[]) {
try{
// Run a java app in a separate system process
Process proc = Runtime.getRuntime().exec("java -jar xml_validator.jar");
// Then retreive the process output
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
System.out.println("Completed...");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output: Completed...
I want to print jar output as well
With the lines
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
you are already on the way.
These streams give you access to the other application's standard output and standard error streams (respectively). By the way: You retrieve the other application's standard output stream by calling getInputStream(), as this is the view of your current application; you are inputting the other application's data.
Just to make it clear: The standard output and th standard error stream are accessed in an application by printing calls to System.out and System.err (respectively).
So, if you have - for example - System.out.println("Hello world") in the other application, you will retrieve the corresponding bytes (see below) in the input stream that you reference with the variable in of the above code snippet.
Normally, you are not interested in the bytes but you want to retrieve the String that you have placed into the output. So you must convert the bytes to a String. For this you normally must provide an encoding (the Java class for that is Charset). In fact, the platform's default encoding works in such cases.
The easiest way is to wrap the input stream in a buffered reader:
BufferedReader outReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
The above mntioned platform's default encoding is used, when not specifying any character set in the InputStreamReader's constructor.
A BufferedReader knows a method readLine(), which you must use to get all the other application's output.
while(outReader.ready())
System.out.println(outReader.readLine())
One word about flushing: If you write data to the standard output stream, this stream is flushed only, when a newline is also written. This is done by calls to System.out.println(...). And this is the reason, why you must read entire lines from the reader.
Are you now able to assemble some code that reads out the other application's output? If not, you maybe should post another question.
I solved it myself.. Here is my solution...
// Then retreive the process output
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
System.out.println("Completed...");
InputStreamReader is = new InputStreamReader(in);
StringBuilder sb=new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while(read != null) {
//System.out.println(read);
sb.append(read);
sb.append("\n");
read =br.readLine();
}
System.out.println(sb);
I am running a command line command from java:
ping localhost > output.txt
The command is send via a Java like this:
Process pr = rt.exec(command);
For some reason the file is not created, but when i run this command from the
command line itself, the file does create and the output is in that file.
Why doesn't the java command create the file?
Because you haven't directed it to a file.
On the command line, you've requested that it be redirected to a file. You have to do the same thing in Java, via the InputStream provided by the Process object (which corresponds to the output stream of the actual process).
Here's how you get the output from the process.
InputStream in = new BufferedInputStream( pr.getInputStream());
You can read from this until EOF, and write the output to a file. If you don't want this thread to block, read and write from another thread.
InputStream in = new BufferedInputStream( pr.getInputStream());
OutputStream out = new BufferedOutputStream( new FileOutputStream( "output.txt" ));
int cnt;
byte[] buffer = new byte[1024];
while ( (cnt = in.read(buffer)) != -1) {
out.write(buffer, 0, cnt );
}
1. After successfully executing the command from Java program, you need to read the output, and then divert the Output to the file.
Eg:
Process p = Runtime.getRuntime().exec("Your_Command");
InputStream i = p.getInputStream();
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
File f = new File("d:\\my.txt");
FileWriter fw = new FileWriter(f); // for appending use (f,true)
BufferedWriter bw = new BufferedWriter(fw);
while((br.readLine())!=null){
bw.write(br.readLine()); // You can also use append.
}
Complementing Andy's answer, I think you MUST read this article: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html.
It is very important for who needs to deal with external processes in Java.
I you want to keep it simple, and you are using Windows, try:
Process pr = rt.exec("cmd /c \"ping localhost > output.txt\"");