好的,我在书中找到了这个例子,所以我知道这段代码没有错误。下面的程序包含两个类,它们都是主类。一个用于客户端,一个用于服务器。
根据这本书,我应该像这样编译它们:
编译客户端和服务器,然后按如下方式启动服务器:
$ java GreetingServer 6066 在6066端口等待客户端......
按如下方式检查客户端程序:
$ java GreetingClient localhost 6066 在端口6066上连接到localhost 刚连接到localhost / 127.0.0.1:6066 服务器说谢谢你连接到/127.0.0.1:6066 再见!
我希望能够在eclipse上运行它们,但每次我这样做,都会给我这个错误: 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0 在GreetingServer.main(GreetingServer.java:47)。
我如何在ECLIPSE中运行此程序?感谢。
//文件名GreetingClient.java
import java.net.*;
import java.io.*;
public class GreetingClient
{
public static void main(String [] args)
{
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try
{
System.out.println("Connecting to " + serverName
+ " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out =
new DataOutputStream(outToServer);
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
// File Name GreetingServer.java
import java.net.*;
import java.io.*;
public class GreetingServer extends Thread
{
private ServerSocket serverSocket;
public GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}
public void run()
{
while(true)
{
try
{
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataInputStream in =
new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!");
server.close();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
int port = Integer.parseInt(args[0]);
try
{
Thread t = new GreetingServer(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
见这些行
public static void main(String [] args)
{
String serverName = args[0]; //<-- Expecting a value
int port = Integer.parseInt(args[1]); //<-- Expecting a value
args []是一个字符串数组,包含通过命令行传递的参数。当你试图直接从eclipse运行它时,你没有指定这些值导致args []成为一个空数组,因此args [0]给出一个ArrayIndexOutOfBoundsException
要解决此问题,请在eclipse中创建运行配置(参见屏幕截图)
并指定您希望eclipse在运行此类时传递的参数。你可以通过right-clicking the project, select run, then run-configurations --> double click on java_application
执行此操作并传递您想要的信息。在指定参数时,您可能需要指定主类名,以便eclipse可以识别将args传递给哪个主类。
或者您可以直接在类本身(用于测试)中对这些值进行硬编码
答案 1 :(得分:0)
您需要创建一个运行配置并在那里传递参数。在eclipse中运行程序。让它失败。这会自动创建运行配置。编辑配置以添加参数。或者创建一个新的运行配置。