Java redirect the sysout of a class in a jar
-
12-06-2021 - |
Question
I would like to redirect/hide the sysout generated by the following code :
Tools tool = new ToolsImpl();
HashCode hash = tool.computeHashCodes(dir);
The difficult part is : the method computeHashCodes is stored in a jar.
I've tried the following code :
PrintStream printStreamOriginal=System.out;
System.out.println("sysout deactivated");
System.setOut(new PrintStream(new OutputStream() {
public void write(int b) {}
}));
System.out.println("Text to delete");
Tools tool = new ToolsImpl();
HashCode hash = tool.computeHashCodes(dir);
System.setOut(printStreamOriginal);
System.out.println("sysout reactivated");
The "text to delete" is indeed deleted, but the sysout generated by the ".computeHashCodes" is not. Does someone know how to hide this sysout ?
Thx in advance, Mike
Solution 4
Thanks everyone, I finally managed to avoid displaying the sysout.
When Magodiez advised me to find how the output was done, I thought I couldn't do that because I didn't have access to the source code ; but then I realised I just had to decompile the code.
So I decompiled it with Java Decompiler, and then I saw how the output was done :
LOGGER.log(Level.INFO, str2);
Then I resolved my problem by using the following line :
java.util.logging.Logger.getLogger("log.tools").setLevel(Level.SEVERE);
It is actually what I really wanted, now only the SEVERE messages will be printed on the sysout.
Thanks again !
OTHER TIPS
The code may be writing to System.err instead.
Try the same exercise but with System.err instead of System.out.
your solution works fine when using System.out
, so I'm guessing that the code you want to "block" doesn't use System.out
for the output. Try to find how the output is done, so you can "block" it.
See here:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
public class CreateJarFile {
public static int BUFFER_SIZE = 10240;
protected void createJarArchive(File archiveFile, File[] tobeJared) {
try {
byte buffer[] = new byte[BUFFER_SIZE];
// Open archive file
FileOutputStream stream = new FileOutputStream(archiveFile);
JarOutputStream out = new JarOutputStream(stream, new Manifest());
for (int i = 0; i < tobeJared.length; i++) {
if (tobeJared[i] == null || !tobeJared[i].exists()
|| tobeJared[i].isDirectory())
continue; // Just in case...
System.out.println("Adding " + tobeJared[i].getName());
// Add archive entry
JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
jarAdd.setTime(tobeJared[i].lastModified());
out.putNextEntry(jarAdd);
// Write file to archive
FileInputStream in = new FileInputStream(tobeJared[i]);
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
out.write(buffer, 0, nRead);
}
in.close();
}
out.close();
stream.close();
System.out.println("Adding completed OK");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Error: " + ex.getMessage());
}
}
}