Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Redirect command prompt output to log file.

  1. #1
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Unhappy [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
    Last edited by goldest; November 24th, 2010 at 05:30 AM. Reason: Marking as solved.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Redirect command prompt output to log file.

    Can we see your code?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Default 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
    Attached Files Attached Files

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  5. #5
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Default 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

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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.
    Last edited by helloworld922; November 19th, 2010 at 02:45 AM.

  7. #7
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Talking Re: Redirect command prompt output to log file.

    Here you go helloworld922

    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,
    java RunAnt MyLog.log
    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.


    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

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  9. #9
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Talking 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

  10. #10
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Talking [SOLVED]Re: Redirect command prompt output to log file.


    Marking it as solved.

    Thanks for your support everyone!

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

Similar Threads

  1. [SOLVED] can't run javac directly from command prompt
    By epezhman in forum Java IDEs
    Replies: 7
    Last Post: January 12th, 2011, 07:38 PM
  2. Reading ant output when exec the command
    By cipm66 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 23rd, 2010, 01:48 PM
  3. How To Make JCreator Output in a Command Window
    By Kimimaru in forum Java Theory & Questions
    Replies: 4
    Last Post: October 4th, 2010, 09:36 PM
  4. Strange launch problem using command prompt
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 15th, 2010, 07:52 AM
  5. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM