Netty 3.5.8.Final Exception in thread [closed]-Collection of common programming errors

Several times a minute there is an error

channelConnected
Exception in thread "pool-632-thread-1" java.lang.NoClassDefFoundError: Could not initialize class org.jboss.netty.util.internal.DetectionUtil
    at org.jboss.netty.util.HashedWheelTimer$Worker.waitForNextTick(HashedWheelTimer.java:456)
    at org.jboss.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:373)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at java.lang.Thread.run(Unknown Source)

Netty 3.5.8.Final. How to fix it? I’m using Netty 3.5.3 and the error disappears, but there is a memory leak. Here is the code that I use. The error occurs when channelConnected:

public class Netty {
    private ClientBootstrap Cbootstrap = new ClientBootstrap(new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    private ServerBootstrap Sbootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));    
    public Netty(int port) {
        final Handler Handler = new Handler();
        ChannelPipelineFactory CPF = new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                return Channels.pipeline(new ReadTimeoutHandler(new HashedWheelTimer(), 30), Handler);
            }
    };
    Cbootstrap.setPipelineFactory(CPF);
        Sbootstrap.setPipelineFactory(CPF);
        Cbootstrap.setOption("connectTimeoutMillis", 5000);
        Sbootstrap.setOption("connectTimeoutMillis", 5000);
    Cbootstrap.setOption("tcpNoDelay", true);
    Cbootstrap.setOption("keepAlive", true);
        Sbootstrap.setOption("child.tcpNoDelay", true);
        Sbootstrap.setOption("child.keepAlive", true);
        Sbootstrap.bind(new InetSocketAddress(port));   
    }
    public void connect(InetAddress addr, int port) {
        Cbootstrap.connect(new InetSocketAddress(addr, port));
    }
}
public class Handler extends SimpleChannelHandler {
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        ChannelBuffer buf = (ChannelBuffer) e.getMessage();
        System.out.println(buf.toString("windows-1251"));
        Channel ch = e.getChannel();
        ChannelBuffer buff = ChannelBuffers.dynamicBuffer();
    buff.writeBytes(("TEST1").getBytes());
        ch.write(buff);
    }
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
        ChannelBuffer buf = (ChannelBuffer) e.getMessage();
        System.out.println(buf.toString("windows-1251"));
        Channel ch = e.getChannel();
        ChannelBuffer buff = ChannelBuffers.dynamicBuffer();
    buff.writeBytes(("TEST2").getBytes());
        ch.write(buff);
    }
}
  1. That’s strange.. Is it possible that you have two versions of Netty on the classpath ? Like the old 3.5.3.FInal and the new 3.5.8.Final ?