Board index » jbuilder » Re: How do you compile when each class refers to other class

Re: How do you compile when each class refers to other class


2005-02-23 05:44:10 AM
jbuilder15
Joe Vasher wrote:
Quote
I'm trying to compile an example that someone wrote.
Creating a simple Chat Client/Server Solution (I included link to and
the source code involved below.)


The two classes in server example 4 refer to each other. When I try to
build in in JBUILDERX it keeps saying:

"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 6, column 12
"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 6, column 45
"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 48, column 10
"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 63, column 37

pirate.shu.edu/~wach{*word*233}/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html



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

public class ChatServer implements Runnable
Probably because you aren't using a package statement. Always put your
classes in packages.
java.sun.com/developer/onlineTraining/Programming/BasicJava2/jar.html
--
Regards,
Lori Olson [TeamB]
------------
Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.
Google Advanced Newsgroup Search
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache/1.html
 
 

Re:Re: How do you compile when each class refers to other class

I'm trying to compile an example that someone wrote.
Creating a simple Chat Client/Server Solution (I included link to and
the source code involved below.)
The two classes in server example 4 refer to each other. When I try to
build in in JBUILDERX it keeps saying:
"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 6, column 12
"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 6, column 45
"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 48, column 10
"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 63, column 37
pirate.shu.edu/~wach{*word*233}/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html
import java.net.*;
import java.io.*;
public class ChatServer implements Runnable
{ private ChatServerThread clients[] = new ChatServerThread[50];
private ServerSocket server = null;
private Thread thread = null;
private int clientCount = 0;
public ChatServer(int port)
{ try
{ System.out.println("Binding to port " + port + ", please wait
...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
start(); }
catch(IOException ioe)
{ System.out.println("Can not bind to port " + port + ": " +
ioe.getMessage()); }
}
public void run()
{ while (thread != null)
{ try
{ System.out.println("Waiting for a client ...");
addThread(server.accept()); }
catch(IOException ioe)
{ System.out.println("Server accept error: " + ioe); stop(); }
}
}
public void start() { /* as before */ }
public void stop() { /* as before */ }
private int findClient(int ID)
{ for (int i = 0; i < clientCount; i++)
if (clients[i].getID() == ID)
return i;
return -1;
}
public synchronized void handle(int ID, String input)
{ if (input.equals(".bye"))
{ clients[findClient(ID)].send(".bye");
remove(ID); }
else
for (int i = 0; i < clientCount; i++)
clients[i].send(ID + ": " + input);
}
public synchronized void remove(int ID)
{ int pos = findClient(ID);
if (pos>= 0)
{ ChatServerThread toTerminate = clients[pos];
System.out.println("Removing client thread " + ID + " at " + pos);
if (pos < clientCount-1)
for (int i = pos+1; i < clientCount; i++)
clients[i-1] = clients[i];
clientCount--;
try
{ toTerminate.close(); }
catch(IOException ioe)
{ System.out.println("Error closing thread: " + ioe); }
toTerminate.stop(); }
}
private void addThread(Socket socket)
{ if (clientCount < clients.length)
{ System.out.println("Client accepted: " + socket);
clients[clientCount] = new ChatServerThread(this, socket);
try
{ clients[clientCount].open();
clients[clientCount].start();
clientCount++; }
catch(IOException ioe)
{ System.out.println("Error opening thread: " + ioe); } }
else
System.out.println("Client refused: maximum " + clients.length
+ " reached.");
}
public static void main(String args[]) { /* as before */ }
}
import java.net.*;
import java.io.*;
public class ChatServerThread extends Thread
{ private ChatServer server = null;
private Socket socket = null;
private int ID = -1;
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
public ChatServerThread(ChatServer _server, Socket _socket)
{ super();
server = _server;
socket = _socket;
ID = socket.getPort();
}
public void send(String msg)
{ try
{ streamOut.writeUTF(msg);
streamOut.flush();
}
catch(IOException ioe)
{ System.out.println(ID + " ERROR sending: " + ioe.getMessage());
server.remove(ID);
stop();
}
}
public int getID()
{ return ID;
}
public void run()
{ System.out.println("Server Thread " + ID + " running.");
while (true)
{ try
{ server.handle(ID, streamIn.readUTF());
}
catch(IOException ioe)
{ System.out.println(ID + " ERROR reading: " + ioe.getMessage());
server.remove(ID);
stop();
}
}
}
public void open() throws IOException
{ streamIn = new DataInputStream(new
BufferedInputStream(socket.getInputStream()));
streamOut = new DataOutputStream(new
BufferedOutputStream(socket.getOutputStream()));
}
public void close() throws IOException
{ if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
if (streamOut != null) streamOut.close();
}
}
 

Re:Re: How do you compile when each class refers to other class

On 2/23/2005 at 12:54:41 AM, Joe Vasher wrote:
Quote
The two classes in server example 4 refer to each other. When I try to
build in in JBUILDERX it keeps saying:

"ChatServer.java": cannot resolve symbol: class ChatServerThread in
class ChatServer at line 6, column 12
import java.net.*;
import java.io.*;

public class ChatServer implements Runnable
:
}

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

public class ChatServerThread extends Thread
:
}
Did you put this code in two separate files?
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

{smallsort}

Re:Re: How do you compile when each class refers to other class

On 2/23/2005 at 10:39:33 PM, Joe Vasher wrote:
Quote
>Did you put this code in two separate files?
>
yes
See Lori's message.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Re: How do you compile when each class refers to other class

John McGrath [TeamB] wrote:
Quote
On 2/23/2005 at 12:54:41 AM, Joe Vasher wrote:

>import java.net.*;
>import java.io.*;
>
>public class ChatServer implements Runnable

>
>import java.net.*;
>import java.io.*;
>
>public class ChatServerThread extends Thread

:
Did you put this code in two separate files?

yes