Reputation: 1391
I have a small java program which asks the user to input two dates. I want to package this whole program into a jar file. when I try to execute the jar file via command line and redirecting the output to a log file( like this: java -jar Sample.jar >> Sample.log
), all the System.out.println
statements and log statements are printed to the external file, not on console. I want to see the System.out.println
statements printed on the console and log4j log statements in the external file even if the user redirects the output to log file. Please note that I have set the log4j appender to 'Console'. Any idea on how to do that?
The sample program is below:
public class Sample {
public static void main(String[] arg) {
logger.debug("start of the main method");
Scanner in = new Scanner(System.in);
System.out.println("Please enter start date in the format YYYY-MM-DD");
String startDate = in.nextLine();
logger.debug("The entered start date is " + startDate);
System.out.println("Please enter end date in the format YYYY-MM-DD");
logger.debug("The entered end date is " + endDate);
}
}
Upvotes: 1
Views: 1160
Reputation: 2458
Instead of the >>
you can use the approach described in this answer. So instead of:
java -jar Sample.jar >> Sample.log
... you could do:
java -jar Sample.jar | tee -a Sample.log
For Windows you can try to use something from: this question or this one
tee
can be accessed using using Windows PowerShell. This can be opened from start menu.
This question really helped me in finding a way to access tee
command
Upvotes: 1