[SOLVED]Redirect command prompt output to log file.
Hi all,
I am running a big build.xml through ant script. What I want to do is to have the console output printed to console as well as another log file which I can use for my reference.
I tried doing ant build.xml >>xyz.log in this case the output gets printed into the log file only, but not on the console. I want to see the output on console as well as I want to store it in xyz.log file.
Any suggestions what I need to try out?
Thanks much,
Goldest
Re: Redirect command prompt output to log file.
1 Attachment(s)
Re: Redirect command prompt output to log file.
Well,
There is no code written by me as such. I was just trying to install jBPM with Tomcat through the sample build.xml provided by the vendor jBoss itself.
Command which I ran is ant demo.setup.tomcat, everything works fine. Product gets installed. The thing is that I want to see the output on command prompt console plus I want to save it to some other log file as well. So that I can look at it later on and get the details of installation.
I hope that helps. Attaching the build.xml file renamed as build.xml.txt
Thanks much,
Goldest
Re: Redirect command prompt output to log file.
This is more of an OS problem than a java problem. I presume given you are using the output symbol you are on linux/unix? Try using tee, something like
ant myscript | tee log.file
Re: Redirect command prompt output to log file.
Not Unix/Linux, I am on Windows
It's Windows Vista Business to be more specific. And I am talking about running the ant script through Windows command prompt.
Goldest
Re: Redirect command prompt output to log file.
As far as I'm aware, this isn't possible without having some "intermediate program" to handle the streams. You're probably better off asking this question on a Windows forum as this doesn't really concern Java specifically (unless you want this intermediate program to be written in Java, in which case this would be a good place to post your question).
So your batch file might look something like this:
Code batch:
some_program > java log_stream "output_log.log"
log_stream could be some program written in Java which would take 1 command-line argument (the name of the log file), and then simply open up a file stream and write out the contents it receives to both the standard out and the file.
Re: Redirect command prompt output to log file.
Here you go helloworld922
Code Java:
import java.io.*;
import java.util.logging.*;
public class RunAnt {
public static void main(String args[]) {
try {
String command = "ant.bat test";
File buildFileDir = new File("D:/dell");
Runtime rt = Runtime.getRuntime();
// Below process execution is as good as doing following:
// Start -> Run -> cmd -> cd D:/dell -> ant test
Process pr = rt.exec(command, null, buildFileDir);
BufferedReader input = new BufferedReader(new InputStreamReader(pr
.getInputStream()));
String line = null;
FileHandler fh = new FileHandler(args[0], false);
Logger logger = Logger.getLogger("global");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
while ((line = input.readLine()) != null) {
logger.log(Level.INFO, line);
}
System.out.println("Logging Completed...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
We can run it like,
And that log file will get generated at the root location from where we are running the program.
Thanks helloworld922 for suggestion of doing this through java code. That helped and worked. :cool:
Interesting Observation:-
If I change the String variable command from "ant.bat test" to "ant test", it doesn't work, and the compiler gives exception like java.io.IOException: CreateProcess: ant test error=2
I have the ANT_HOME set to the root directory and even the classpath contains the ANT_HOME\bin of the ANT installation directory. Environment variables are set correctly.
Well thats just weird, but the main issue is resolved. But still, if someone knows why this happens, let me know. That would be appreciated.
Cheers,
Goldest
Re: Redirect command prompt output to log file.
Is there an ant.exe in the same directory? I believe windows will search for these files first before searching for batch files.
Re: Redirect command prompt output to log file.
Yeah, seems like the same thing. There is actually one ant file but it doesn't have any extension and there is this ant.bat file as well. And may be the ant.bat file is the one which is running on the build.xml script when we say "ant" in any particular directory.
So the conclusion is when it comes to ant, there isn't any ant.exe, but there is ant.bat.
Goldest
[SOLVED]Re: Redirect command prompt output to log file.
Marking it as solved.
Thanks for your support everyone!
Goldest