您好我的代码有些问题如下。 try和catch块在我的IDE中的try行中有一个红色X,告诉我“遇到了”(但是遇到了“{”。我错过了什么?可能是我的问题?
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/** * * @author Apress */
public class FutureForm
{
public static void main(String[] args)
{
final int DEFAULT_PORT = 5555;
final String IP = "127.0.0.1";
// v-This parenthesis should be a bracket it says??
try( AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open())
{
if (asynchronousServerSocketChannel.isOpen())
{
asynchronousServerSocketChannel.bind(new InetSocketAddress(IP, DEFAULT_PORT));
System.out.println("Waiting for connections ...");
while (true)
{
Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel.accept();
try (AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get())
{
System.out.println("Incoming connection from: " + asynchronousSocketChannel.getRemoteAddress());
final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (asynchronousSocketChannel.read(buffer).get() != -1)
{
buffer.flip();
asynchronousSocketChannel.write(buffer).get();
if (buffer.hasRemaining())
buffer.compact();
else
buffer.clear();
}
System.out.println(asynchronousSocketChannel.getRemoteAddress() + " was successfully served!");
}
catch (IOException || InterruptedException || ExecutionException ex)
{
System.err.println(ex);
}
}
}
else
{
System.out.println("The asynchronous server-socket channel cannot be opened!");
}
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
答案 0 :(得分:5)
不确定这是否是警告的原因,但您的多重修补程序中存在语法错误:您只需要使用单个管道。
catch (IOException | InterruptedException | ExecutionException ex)
答案 1 :(得分:0)
验证您是否已在Java&gt;编译器:“编译器合规性级别”的首选项中为Java 1.7配置了工作区。
如果这没有帮助,请检查Java Compiler下的项目属性,该属性应该没有项目特定的设置,或者已配置1.7。