通过修改源文件中包名和手工编译源文件的方法,说明使用Java引用的方法是: JAR文件实际上是zip格式压缩包,假设包结构为dir_a/dir_b/c.class(dir_a目录下是dir_b目录,dir_b目录下是c.class文件),将该JAR包的绝对路径加入到classpath中,然后在源文件里用import a.b.c的方法使用c类。下面分别用手工编译、运行不同包名源文件的方法证明以上论述。 方案1:类文件中不写包名: DOS命令为: cd e:\ex\leechau set CLASSPATH=e:\MyDoc\JEx\MinaServer\lib\slf4j-jdk14-1.3.0.jar;e:\MyDoc\JEx\MinaServer\lib\slf4j-api-1.3.0.jar;e:\MyDoc\JEx\MinaServer\lib\mina-core-2.0.0-RC1.jar;. javac SamplMinaServerHandler.java javac MyMinaServer.java java MyMinaServer SamplMinaServerHandler.java文件内容为: // package ex.leechau; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IoSession; public class SamplMinaServerHandler extends IoHandlerAdapter{ private int count=0; //当一个客端端连结进入时 @Override public void sessionOpened(IoSession session) throws Exception { System.out.println("incomming client : "+session.getRemoteAddress()); } //当一个客户端关闭时 @Override public void sessionClosed(IoSession session) { System.out.println("one Clinet Disconnect !"); } @Override public void messageReceived(IoSession session, Object message) throws Exception { //我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为String: String s=(String)message; // Write the received data back to remote peer System.out.println("收到客户机发来的消息: "+s); //测试将消息回送给客户端 session.write(s+count); count++; } } MyMinaServer.java文件内容为: // package ex.leechau; import java.io.IOException; import java.net.InetSocketAddress; import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.SocketAcceptor; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; public class MyMinaServer { public static void main(String[] args) throws IOException { //创建一个非阻塞的Server端Socket,用NIO SocketAcceptor acceptor = new NioSocketAcceptor(); //创建接收数据的过滤器 DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); //设定这个过滤器将一行一行(/r/n)的读取数据 chain.addLast("myChin", new ProtocolCodecFilter(new TextLineCodecFactory())); //设定服务器端的消息处理器:一个SamplMinaServerHandler对象, acceptor.setHandler(new SamplMinaServerHandler()); // 服务器端绑定的端口 int bindPort=9988; //绑定端口,启动服务器 acceptor.bind(new InetSocketAddress(bindPort)); System.out.println("Mina server is listening on port: " + bindPort); } } 方案2:类文件中写包名,在源文件目录下编译: cd e:\ex\leechau set CLASSPATH=e:\MyDoc\JEx\MinaServer\lib\slf4j-jdk14-1.3.0.jar;e:\MyDoc\JEx\MinaServer\lib\slf4j-api-1.3.0.jar;e:\MyDoc\JEx\MinaServer\lib\mina-core-2.0.0-RC1.jar;e:\ javac SamplMinaServerHandler.java javac MyMinaServer.java java MyMinaServer SamplMinaServerHandler.java和MyMinaServer.java文件取消方案1中包名上的注释。 方案3:类文件中写包名,在项目根目录下编译: cd e:\ set CLASSPATH=e:\MyDoc\JEx\MinaServer\lib\slf4j-jdk14-1.3.0.jar;e:\MyDoc\JEx\MinaServer\lib\slf4j-api-1.3.0.jar;e:\MyDoc\JEx\MinaServer\lib\mina-core-2.0.0-RC1.jar;. javac ex/leechau/SamplMinaServerHandler.java javac ex/leechau/MyMinaServer.java java ex/leechau/MyMinaServer SamplMinaServerHandler.java和MyMinaServer.java文件与方案2一样。 以上3中方案都可以是Mina Server正常运行,但如果在有包名的情况下使用方案1的方法定义classpath,则编译MyMinaServer.java时会出现“无法识别SamplMinaServerHandler”错误。