Reputation: 2926
I have a simple Java application, that is built and executed by an Ant script.
When I say System.out.println("x")
in code, there will be the following output:
...
run:
[java] x
...
What I want is simply:
...
run:
x
...
Without the prefix [java]
.
Below the fold:
I know there is the switch -emacs
(stupid name, BTW). This however removes all the other [...]
prefixes as well - in tasks like clean
or compile
. I want to keep those, and I want the change to be in Ant script, not an Ant switch.
The relevant Ant code is:
<target name="run" depends="jar">
<java fork="true" jar="MyCoolProgram.jar"/>
</target>
Upvotes: 3
Views: 657
Reputation: 5258
ANT redirects standard output / standard error to its own output code using java.lang.System.setOut/setErr. However, you can get back the original standard output/error by using java.io.FileDescriptor.out/err fields. You will need to open those fields using FileOutputStream, and then in turn open a java.io.PrintStream on those:
import java.io.*;
...
PrintStream out = new PrintStream(new FileOutputStream(FileDescriptor.out));
PrintStream err = new PrintStream(new FileOutputStream(FileDescriptor.err));
This will then work for any custom ANT tasks, and for any Java code you launch with
<java fork="false">
. So you can avoid the prefixing for this code, while keeping it for any other task.
However, when you launch with<java fork="true">
, then the standard output and standard error passed to your sub-process are controlled by ANT, and the above technique will not work. If you really must run your code in a separate JVM, writing your own ANT task to launch this code could be an option.
Another technique that works is to use System.console() for output instead of System.out(). However, that only works if your ANT script is being executed interactively, not if it is being called by e.g. a Hudson build, and again this does not work with a forked JVM.
Upvotes: 3