I've updated the title, please see update below for the reason.

I have asked this question on Stackoverflow, but still didn't receive an answer.

Groovy: redirect stdout to a file does not work.

I've decided to ask here since I think it's may be not a programming issue but rather some problem with Groovy installation, version conflict, etc.

Here are some details.

I wrote a very simple script to output a string to stdout with System.out.println. It works unless I want to redirect the output to a file. Interestingly, the file is created but it's empty.

I also tried a simple "Hello, World" script with Java (same JDK), and it works well.

Someone confirmed in comments that my Groovy script does redirect properly on Windows and OS X.

The Groovy installation was fresh with the latest version. I followed all the installation instructions. I tried to run some existing scripts and everything works except this redirection.

I have no clue where to look now. Please help.

Groovy Version: 1.8.4 JVM: 1.7.0_02`
OS: Windows 7 64-bit Pro
GROOVY_HOME: C:\Program Files (x86)\Groovy\Groovy-1.8.4\
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.7.0_02


OK, I've just tried one more thing that come to mind. I have .groovy extension associated with groovy executable, so I can run a script without typing groovy. So I did test.groovy > test.txt and it was not working.

Now I've tried groovy test.groovy > test.txt and it works!

So the question is now simpler. Why redirection does not work if I don't use groovy in the command line?

Please also recommend what i should do with this question? Delete the SO version?

UPDATE 2: Here is the related registry record (HKCR\Groovy\shell\open\command):

"C:\Program Files (x86)\Groovy\Groovy-1.8.4\bin\groovy.exe" "%1" %*
  • When you simply run test.groovy, does anything appear in the console? It could be possible that running via the extension prevents standard output. – John Dec 22 '11 at 21:46
  • @John: Yes, without redirection everything works well. I do see the output string in the console. Thanks – yuk Dec 23 '11 at 15:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.