Results 1 to 3 of 3

Thread: java

  1. #1
    jesroni is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Question java

    hi i am new to java..i know only basics...
    i wrote following chat program..i run the proram following error
    occured
    Exception in thread "main" java.lang.NoSuchMethodError: Client.<init>(Ljava/net/Socket;I)V
    at Server.main(Server.java:15)
    Server side
    import java.io.*;
    import java.net.*;
    import java.util.*;
    public class Server
    {
    public static void main(String[] args) throws Exception
    {
    final int PORT = 1444;
    ServerSocket server = new ServerSocket(PORT);
    System.out.println("Waiting for clients...");
    while (true)
    {
    Socket s = server.accept();
    System.out.println("Client connected from " + s.getLocalAddress().getHostName());
    Client chat1 = new Client(s,1);
    Client chat2 =new Client(s,2);
    Thread t1 = new Thread(chat1);
    Thread t2 =new Thread(chat2);
    t1.start();
    t2.start();
    }
    }
    }
    class Client implements Runnable
    {
    private Socket socket;
    int id;
    public Client(Socket s,int pid)
    {
    id=pid;
    socket = s;
    }
    public void run()
    {
    try
    {
    Scanner chat = new Scanner(System.in);
    Scanner in = new Scanner(socket.getInputStream());
    PrintWriter out = new PrintWriter(socket.getOutputStream());
    while (true)
    {
    if(id==1)
    {
    String input = chat.nextLine();
    out.println(input);
    out.flush();
    }
    if(id==2)
    {
    if(in.hasNext())
    System.out.println(in.nextLine());
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    client side
    import java.io.*;
    import java.net.*;
    import java.util.*;
    public class Client
    {
    private final static int PORT = 1444;
    private final static String HOST = "localhost";
    public static void main(String[] args) throws IOException
    {
    Socket s = new Socket(HOST, PORT);
    System.out.println("You connected to " + HOST);
    Clients client = new Clients(s,1);
    Clients clients =new Clients(s,2);
    Thread t1 = new Thread(client);
    Thread t2 =new Thread(clients);
    t1.start();
    t2.start();
    }
    }
    class Clients implements Runnable
    {
    private Socket socket;
    int id;
    public Clients(Socket s,int pid)
    {
    socket = s;
    id=pid;
    }
    public void run()
    {
    try
    {
    Scanner chat = new Scanner(System.in);
    Scanner in = new Scanner(socket.getInputStream());
    PrintWriter out = new PrintWriter(socket.getOutputStream());
    while (true)
    {
    if(id==1)
    {
    String input = chat.nextLine();
    out.println(input);
    out.flush();
    }
    if(id==2)
    {
    if(in.hasNext())
    System.out.println(in.nextLine());
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default Re: java

    Quote Originally Posted by jesroni View Post
    Exception in thread "main" java.lang.NoSuchMethodError: Client.<init>(Ljava/net/Socket;I)V
    at Server.main(Server.java:15)
    According to the JVM there is no constructor '<init>' for the Client class that takes a Socket and an int for parameters, but as far as I can see, the constructor is present; I guess something went wrong during compilation; recompile everything and try again. (don't ignore the compiler messages).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jesroni is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •