I wanted to compile a Java file through a batch file, then I realized that I wouldn’t see any errors, I decided to try creating logs using the same batch file, but nothing happens. I tried to put file overwriting through '>', however, the result is the same.
The code itself .bat:

 echo Date:% date%, time:% time% >> "C: \ app \ logs \ log.txt"
 cd "C: \ app \ xyz \ some \ Main"
 "C: \ Program Files \ Java \ jdk1.8.0_144 \ bin \ javac.exe" Main.java >> "C: \ app \ logs \ log.txt"
 echo ===================================== >> "C: \ app \ logs \ log. txt "
  • it is better to master assembly systems - this will greatly facilitate many things (and logging too) - Mikhail Vaysman
  • @MikhailVaysman, I don’t argue, but I thought it would be better to start with something complicated - Andrei
  • why start with the difficult? we must begin with a simple one. - Mikhail Vaysman
  • @MikhailVaysman, not so expressed. I want to study this too, in other words - Andrew

1 answer 1

Firstly, if javac is in the environment variables, then you can omit the full path (except when you have a lot of them and you want to use a particular one).

Secondly, they usually turn off the output of commands to the screen, indicating at the beginning of the *.bat . @echo off file @echo off .

Thirdly, logging is conveniently done in this way, combining several commands at once, so as not to write to each line:

 @echo off >output.txt 2>&1( command1 command2 ... commandN ) 

Fourthly, if you want to add to the file use >> instead of > , the latter overwrites the file from the beginning.

Use 2>&1 to redirect stderr to stdout to output errors too.

  • Thank you, especially for the third. Everything works, errors are displayed. But I can not understand the structure of the expression> output.txt 2> & 1 () - Andrey
  • @Andrey about 2> & 1 is written at the end of the answer =)> output.txt - this redirects stdout to the file output.txt - Lex Hobbit