I'm calling code in a jar file using jpype in python. The java code prints a bunch of output to stdout, which I'd like to just redirect to /dev/null. How would I do this? I can't modify the java code (since it's an external project).
Here is my python code:
import jpype
import jpype.imports
jpype.startJVM(jpype.getDefaultJVMPath(), '-Djava.class.path=%s' % astral)
jpype.imports.registerDomain('phylonet')
from phylonet.coalescent import CommandLine
CommandLine.main(['-i', input_file, '-o', output_file])
jpype.shutdownJVM()
Fortunately I can redirect the output that I need to a file, but I still get a lot of undesirable output directly to stdout. I'm calling multiple instances in a process pool, so I end up with garbled output from multiple instances of the java code.
Stealing from here and then translating to Python/JPype
import jpype
import jpype.imports
from jpype.types import *
jpype.startJVM(convertStrings=False)
from java.lang import System
from java.io import PrintStream, File
original = System.out
System.out.println("Hello")
System.setOut(PrintStream(File("NUL"))) # NUL for windows, /dev/null for unix
System.out.println("Big")
System.setOut(original)
System.out.println("Boy")
If you need to be neutral to the OS, you would need to compile your own NullPrintStream concept as it is currently not possible to extend a class in JPype.