Just download and run TOR Browser. It will open a socks proxy in your system. You can check the port in browser settings:
You just need to set a proxy for your java socket:
InetSocketAddress HiddenerProxyAddress = new InetSocketAddress("127.0.0.1", 9150);
Proxy HiddenProxy = new Proxy(Proxy.Type.SOCKS, HiddenerProxyAddress);
Socket underlying = new Socket(HiddenProxy);
SocketAddress sa = new InetSocketAddress("www.facebook.com", 80);
underlying.connect(sa);
If you want to connect to a hidden service (onion address), you should create an unresolved socket address:
InetSocketAddress sa = InetSocketAddress.createUnresolved("http://facebookcorewwwi"facebookcorewwwi.onion", 80);
This is it. Now you can connect using socket through TOR.