yuk
yuk

Reputation: 19870

Groovy: redirect stdout to a file does not work

I'm just learning groovy. I have a very simple script that output some text to STDERR and STDOUT with println:

err = System.err    
resultStr = "test to print"
err.println resultStr
println resultStr

If I then run the script without redirection I get the resultStr string in the output two times.

If I redirect the output to a file with test > test.txt, the result string appears one time in the output (OK), test.txt is created (OK), but it's empty. I've tried to do System.out.println instead of println with the same result. What am I doing wrong?

If it's important, I'm on Windows 7. Just in case I've tried to redirect output of standard windows programs, like dir, and it works.

Groovy Version: 1.8.4 JVM: 1.7.0_02

UPDATE: i have also tried to create a simple "Hello World" class in java (the same JVM). Redirection does work. So it should be something in Groovy.

UPDATE 2: After the comment from @socha23 that he can redirect successfully from the same script under Windows 7, I thought that the problem is not in the script but in Groovy installation, version conflicts or OS. So I've asked similar question on SuperUser (sorry for cross-posting, but I really thought it's a borderline question).

Then I found out that redirection does not work properly if I run the script directly as test.groovy > test.txt. But if I run groovy test.groovy > test.txt it works. I'd still like to understand this behavior and correct if possible. Please visit the SU question. As for today 12/24, it's still unanswered.

I think it's clear now that it's not a programming question. Please recommend if I should delete it from here. Both questions are referring each other.

Upvotes: 2

Views: 3648

Answers (2)

Markus
Markus

Reputation: 514

Are you running Windows 7 64-bit? Try if this hotfix from MS solves your problem: http://support.microsoft.com/kb/971163/en-us This may apply if output redirection does not work with a certain program, but output on console does. (For some reason, the hotfix does not install on my machine.)

As far as I understand the issue, if you call groovy directly there is no API call to CreateProcessWithTokenW involved, whereas if you use the shell, that is "test.groovy > file.txt", it calls this API function, leading to the bug.

UPDATE:

I found out in the meantime that this Hotfix is included in Windows 7 SP1, this is why it will not install on your and on my machine. The only other related bug I found is this one: http://support.microsoft.com/kb/321788, but it was supposedly fixed during Windows XP times.

I strongly suspect the output redirection code in Windows is still containing bugs, since I had the same problem as you with a different program and could fix it by changing the source code, but in a way that does not make sense.

Upvotes: 2

Bob Kuhar
Bob Kuhar

Reputation: 11100

Your results make sense to me, the standard ">" redirect only works on standard out. In both bash and, I'm pretty sure, DOS, you need to do "2>" to get the standard error output redirected to a file. The following script proves this out, on bash at least. The DOS command line worked pretty much the same for me.

System.out.println "Hello Standard Out"
System.err.println "Hello Standard Err"

println "Hello plain-jane println"

Simple redirect test has stdout.txt showing "Hello Standard Out" and "Hello plain-jane println" but NOT "Hello Standard Err", it comes out on the console.

bobk-mbp:hello bobk$ groovy StderrAndOut.groovy > stdout.txt
Hello Standard Err
bobk-mbp:hello bobk$

My next test explicitly redirects standard out and standard error resulting in stdout2.txt showing "Hello Standard Out" and "Hello plain-jane println" and stderr2.txt showing "Hello Standard Err".

bobk-mbp:hello bobk$ groovy StderrAndOut.groovy > stdout2.txt 2>stderr2.txt
bobk-mbp:hello bobk$

I am surprised that your results are any different in standard java as what you are trying to do has little to do with the VM and a lot to do with the shell.

Upvotes: 0

Related Questions