title: How to config socks proxy in Java NIO
body: <p>I'm developing a tool which includes forcing all network traffic of application to go across a socks proxy in Java. For old Socket API, I can just set system properties "-DsocksProxyHost=my-host -DsocksProxyPort=my-port", but it doesn't work with NIO.</p>
  
  <p>I tried a solution:</p>
  
  <p>I wrote an NIO SocketChannel, called "ProxySocketChannel" which extends SocketChannel. It contains socks connection and other socks proxy logic. But when I run it, I got an "IllegalSelectorException" in this line of code in "SelectorImpl.register":</p>
  
  <pre><code>if (!(ch instanceof SelChImpl))
          throw new IllegalSelectorException();
  </code></pre>
  
  <p>sun.nio.ch.SelChImpl is package visible so I can't access it. I tried a tricky solution: I put my "ProxySocketChannel" in package "sun.nio.ch". The compilation passed, but I got an error when running:</p>
  
  <pre><code>java.lang.IllegalAccessError: class sun.nio.ch.ProxySocketChannel cannot access its superinterface sun.nio.ch.SelChImpl
  </code></pre>
  
  <p>I don't know why the class sun.nio.ch.ProxySocketChannel with package sun.nio.ch still can not access sun.nio.ch.SelChImpl. I think there is some protection for JDK built-in classes. Is there a way to access it?</p>
  
  <p>My JDK Version is 1.6.0_65.</p>
  
  <p>Otherwise, is there a way to setup socks proxy for NIO without change existing code?</p>