编译时无法访问类时出错

时间:2011-11-23 22:58:26

标签: java

试图编译这个AuctionClientMain.java,这是我得到的错误,无法说明:

  

AuctionClientMain.java:16:无法访问AuctionClient       坏类文件:。\ AuctionClient.class       类文件包含错误的类:Assignment.AuctionClient       请删除或确保它出现在类路径的正确子目录中。                       AuctionClient a =新的AuctionClient(args [0],args [1],port);

我已经包含了AuctionClientMain.java

import Auction.*;
import java.io.*;

public class AuctionClientMain
{
    //Create the client
    public static void main (String args[]) throws IOException
    {
        if(args.length!=3)
        {
            throw new RuntimeException ("Syntax: java AuctionClient <name> <serverhost> <port>");
        }

        //Convert port taken in as string to an integer
        int port = Integer.parseInt(args[2]);
        AuctionClient a = new AuctionClient(args[0],args[1],port);

    }

}

拍卖客户

package Auction;

import java.io.*;
import java.net.*;

public class AuctionClient
{

    public AuctionGui gui;

    private Socket socket;
    private DataInputStream dataIn;
    private DataOutputStream dataOut;

    //Auction Client constructor  String name used as identifier for each client to allow server to pick the winning bidder
    public AuctionClient(String name,String server, int port)
    {
        //Create a new gui
        gui = new AuctionGui("Bidomatic 5000");
        //Add the key listener to the input field
        gui.input.addKeyListener (new EnterListener(this,gui));
        //Add the exit listener to the window
        gui.addWindowListener(new ExitListener(this));

        try
        {
            //Create a new socket with server name and port number provided
            socket = new Socket(server, port);
            //Create new data input stream
            dataIn = new DataInputStream(socket.getInputStream());
            //Create new data outpit stream
            dataOut = new DataOutputStream(socket.getOutputStream());

            dataOut.writeUTF(name);
              while (true) 
              {
                gui.output.append("\n"+dataIn.readUTF());
            }
      } 
      catch (Exception e)   
      {
         e.printStackTrace();
      }

    }



    //Send bid to output stream
    public void sentBid(String bid)
    {
        try
        {
            //Write bid out
            dataOut.writeUTF(bid);
        }

        catch(IOException e)
        {
            e.printStackTrace();
        }

    }

    public void disconnect()
    {
        try
        {
            socket.close();
        }

        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您的AuctionClientMain类似乎在默认包中。 AuctionClient类位于包Auction中。 AuctionClient的.class文件必须位于名为Auction的子目录中,相对于AuctionClientMain

或者,将AuctionClient放入默认包中,或将AuctionClientMain放入包Auction

另外,Java约定是包名都是小写的。最好使用package auction;代替package Auction;

答案 1 :(得分:-1)

由于Auction Client位于Auction包中,因此编译器希望在名为Auction的directroy中找到Java源文件。