For Java libraries that solve the more general problem of reducing the size of JavaScript or HTML, try googling "java minify". There are several solutions out there. Personally I've never used any of them, so can't really recommend one over another. But as usual, Google is our friend.
And minification is a bit more than just removing whitespaces. First, only the whitespace that don't effect the functionality of a program is removed -- as removing the whitesppace in a string to be printed does affect the program. Second, comments must also be removed -- as removing carriage returns will chain the comments otherwise. This can get complex if you take into account comments within comments (for commenting out code), and commented out code with quotes with whitespaces. etc. etc. etc.
Basically the best way would be to read the entire program flow into a memory model, then re-output that model leaving out what you don't want. Note that I said best, not easiest