0

您好,我对下面的代码有一些问题。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);
    }
}
}
4

2 回答 2

5

不确定这是否是警告的原因,但您的 multicatch 中有语法错误:您只需要使用单个管道。

catch (IOException | InterruptedException | ExecutionException ex)
于 2012-05-30T14:32:23.723 回答
0

在 Java>Compiler 下的 Preferences 中验证您是否已为 Java 1.7 配置了工作区:“Compiler compliance level”。

如果这没有帮助,请检查 Java Compiler 下的项目属性,它应该没有特定于项目的设置,或者配置了 1.7。

于 2012-05-30T14:34:43.707 回答