Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
I've been playing around a little with JNativeHook Java library. "JNativeHook is a library to provide global keyboard and mouse listeners for Java. This will allow you to listen for global shortcuts or mouse motion that would otherwise be impossible using pure Java." What I really didn't like is it printed its licence details every time my code ran. I've dug into the JNI code and saw that it makes a callout to System.out to print this information. My solution was pretty simple, just disable the system output stream during initialisation.

This is the text that I was seeing every time my code ran...
 JNativeHook Licence Text
JNativeHook: Global keyboard and mouse hooking for Java.
Copyright (C) 2006-2015 Alexander Barker. All Rights Received.
https://github.com/kwhat/jnativehook/
JNativeHook is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JNativeHook is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.


Ok yeah I get it, but it doesn't have to be printed every single time. It's enough to include the licence text file.

So this is how I initialise...
 Init code
private static final Logger logger =
Logger.getLogger(GlobalScreen.class.getPackage().getName());
private PrintStream origOut;
...
private void init() throws NativeHookException
// disable System.out and turn off logging
origOut = System.out;
logger.setLevel(Level.OFF);
System.setOut(null);
// register the native hook and all mouse related listeners
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeMouseListener(this);
GlobalScreen.addNativeMouseMotionListener(this);
GlobalScreen.addNativeMouseWheelListener(this);
// restore System.out
System.setOut(origOut);
}


Now when my code starts I don't get any notices and no logging info printed from JNativeHook.



-i

Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...