21

I am trying to build a Scala project with sbt, so I run a command:

sbt clean test > log.log

Which means that any messages that the sbt tool writes to the Windows console should be written to the "log.log" file. But sometimes I get stacktrace written to the console and not into the file:

C:\path>sbt clean test > log.log
java.lang.ExceptionInInitializerError
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
        at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.ClassCastException: Class org.infinispan.configuration.parsing.Parser60 does not implement org.infinispan.configuration.parsing.ConfigurationParser

Why does ">" command not redirect all messages to a file?

1 Answer 1

35

What you have pasted is not command standard output (STDOUT), but error output of the command (STDERR).

When you add "> output_file" to command, you are only redirecting STDOUT to that file, not STDERR.

If you want to output errors, to the same file as standard output you need to use

sbt clean test > log.log 2>&1

what "2>&1" does, is, it says to output error to same place as standard output results.

You can also do something like this:

sbt clean test > log.log 2>error.log

It will output STDOUT to log.log, and STDERR to second file called error.log, if you want to separate them.

See this about command redirector operators

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

3
  • 3
    What's the difference between 2>&1 from your answer, and 1<&2 from the link? I've always seen it your way, and the other way makes sense also (just redirecting "input" instead of "output" but otherwise appearing the same), but it's interesting to see the second choice.
    – Joe
    Oct 27, 2014 at 17:03
  • 6
    semantics... 2>&1 is saying that the output from STDERR should be redirected to the same output as STDOUT. 1<&2 is saying that the output from STDERR should be used as an input to STDOUT. Both produce the same result, and are simply a matter of preference
    – SeanC
    Oct 27, 2014 at 19:14
  • Note that, you must put 2>&1 after the > log.log. Jun 6, 2018 at 2:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .