I will make it as short and precise as possible, but this is a difficult problem. I write in Java on the Linux platform, no matter what it costs.
Short version of the goal: I want to have an abstract class called Client , which acts as a common container for client connections. Client must add thread to each of its connections. I also have some semi-professional code that plays an analogy with similar code. Client essay should be implemented into something more tangible and instance capability. In my case, I have a class called FileClientGui that extends Client and overrides all abstract Client methods using the basic method of retrieving the contents of a file from the server and displaying it. This is further complicated by the fact that the abstract Client itself is an extension of java.lang.Thread .
So, here is my file structure in general terms:
/class/path/lib/client/Client.java
/class/path/com/fileclient/FileClientGui.java
There are several other custom classes that both of these files reference, but I don't get any errors from them. If I need to publish code for these elements, let me know and I will publish them.
So, I run this long javac command on the terminal, setting up the path directory and build directory, as well as all the relevant files that also need to be compiled. The only error I get for any of this code is this:
com/fileclient/FileClientGui.java:26: com.fileclient.FileClientGui is not abstract and does not override abstract method cleanClients() in lib.client.Client
My code (see below) explicitly implements the method and all other abstract methods defined in Client.java. I was browsing the Internet, and it seems that most of the people who encounter this error are trying to do something like an ActionListener implementation and get confused with this implementation, and many times this is just a spelling or capitalization problem. I switched to my code to make sure that this is not a simple "oops" problem. I suspect that this is actually some kind of collision between the name of my class and the name of another class that somehow ended up in my path to classes or in my native Java environment / libraries, but I can not find anything obvious.
Anyway, here is my code.
Client.java:
package lib.client; import lib.clientservercore.Connection; import lib.simplefileaccess.Logger; import java.io.IOException; import java.net.Socket; import java.util.ArrayList; import java.lang.Thread; public abstract class Client extends Thread { ArrayList<Connection> connections; boolean isRunning; Logger log; public Client (String logFile) { log = new Logger(logFile); log.write("Initializing client..."); connections = new ArrayList<Connection>(50); log.write("Client initialized."); } public void logOut(String contents) { log.write(contents); } public Logger getLogger() { return this.log; } public ArrayList<Connection> getConnections() { return connections; } public void addConnection(Connection c) { connections.add(c); } public void removeConnection(Connection c) { connections.remove(c); } public boolean getIsRunning() { return isRunning; } public void setIsRunning(boolean r) { isRunning = r; } public Connection connect(String host, int port) { log.write("Creating new connection..."); Socket s; Connection c = null;
FileClientGui.java:
package com.fileclient; import lib.client.Client; import lib.clientservercore.Connection; import lib.clientservercore.Connection.ConnectionStatus; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.util.Iterator; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; import java.lang.Thread; public class FileClientGui extends Client { JFrame frmMain; JPanel pnlMain; JPanel pnlConnect; JTabbedPane tabConnections; JLabel lblHost; JLabel lblPort; JTextField txtHost; JTextField txtPort; JButton btnConnect; public FileClientGui(String logFile) { super(logFile); logOut("Initializing client controller..."); frmMain = new JFrame("Client"); pnlMain = new JPanel(new BorderLayout()); pnlConnect = new JPanel(new FlowLayout()); tabConnections = new JTabbedPane(); lblHost = new JLabel("Host:"); lblPort = new JLabel("Port:"); txtHost = new JTextField("localhost", 10); txtPort = new JTextField("12321", 5); btnConnect = new JButton("Connect"); frmMain.setSize(450, 600); frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmMain.add(pnlMain); pnlMain.add(pnlConnect, BorderLayout.NORTH); pnlMain.add(tabConnections, BorderLayout.CENTER); pnlConnect.add(lblHost); pnlConnect.add(txtHost); pnlConnect.add(lblPort); pnlConnect.add(txtPort); pnlConnect.add(btnConnect); btnConnect.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String host = txtHost.getText(); int port = Integer.parseInt(txtPort.getText()); try { Socket sock = new Socket(host, port); FileClientConnectionGui c = (FileClientConnectionGui)(connectClient(sock)); tabConnections.addTab(c.getInetAddress().toString(), c.getMainPanel()); } catch (UnknownHostException ex) { logOut("Can't find host: " + host + ". Exception: " + ex.getMessage()); ex.printStackTrace(); } catch (IOException ex) { logOut("Exception: " + ex.getMessage()); ex.printStackTrace(); } } } ); frmMain.setVisible(true); logOut("Client controller initialized."); } public void removeConnection(FileClientConnectionGui c) { logOut("Removing connection: " + c.getInetAddress().toString()); tabConnections.remove(c.getMainPanel()); logOut("Removed connection."); } Connection connectClient(Socket sock) { logOut("Client controller is creating a new connection..."); FileClientConnectionGui c = new FileClientConnectionGui(sock, getLogger(), this); addConnection(c); c.start(); logOut("Client controller created a new connection."); return c; } void runClient() { setIsRunning(true); logOut("Client controller is running."); while (getIsRunning()) { cleanClients(); try { sleep(500); } catch (InterruptedException ex) { logOut("Sleep interrupted. Exception: " + ex.getMessage()); ex.printStackTrace(); } } logOut("Client controller stopped running."); } void cleanClients() { Iterator i = getConnections().iterator(); try { while (i.hasNext()) { FileClientConnectionGui c = (FileClientConnectionGui)(i.next()); if (c.getStatus() == ConnectionStatus.CLOSED) { logOut("Removing dead client at " + c.getInetAddress().toString()); tabConnections.remove(c.getMainPanel()); removeConnection(c); } } } catch (Exception ex) { logOut("cleanClients Exception: " + ex.getMessage()); } } }
I will take any help I can get and thank you in advance for any suggestions you provide. I am completely confused by this.
Perhaps what most worries about this question (and maybe this gives the key to the problem?) Is that I can comment on other implementations of abstract methods (like runClient or connectClient), and I have no additional problems, just that same. Also, if I add the @Override directive to one of the following:
@Override Connection connectClient(Socket sock) { logOut("Client controller is creating a new connection..."); FileClientConnectionGui c = new FileClientConnectionGui(sock, getLogger(), this); addConnection(c); c.start(); logOut("Client controller created a new connection."); return c; }
I get an additional error:
com/fileclient/FileClientGui.java:96: method does not override or implement a method from a supertype
He is clearly overrides a method from its supertype (which is the client). I tried replacing the βClientβ with the full class path (lib.client.Client), and none of the errors changed at all.
Is there something I am missing? Did I try something?