The transformation of a coordinate can actually be done in only a few lines of code:
Coordinate coordinate = new Coordinate(x, y);
MathTransform transform = CRS.findMathTransform(CRS.decode("EPSG:4326"), CRS.decode("EPSG:3857"), false);
JTS.transform(coordinate, coordinate, transform);
This will transform a longitude/latitude coordinate (EPSG:4326) into Web Mercator projection (EPSG:3857) coordinate.
You just need to depend on the following two GeoTools libraries in your build tool (e.g. maven):
osgeo
Open Source Geospatial Foundation Repository
http://download.osgeo.org/webdav/geotools/
org.geotools
gt-api
${geotools.version}
org.geotools
gt-epsg-hsql
${geotools.version}
This answer builds on a question/reply on gis.stackexchange.com. Posted my reply because the current answers here seem to be quite verbose.