Hi all,

I am pretty much new to Java (I've had experience in C++) and have been working on a "simple" FTP client. I pretty much ended up with the code below. Problem is I get a lot of errors mostly related to the IOException in each method. The errors are "not a statement", "; expected", and "illegal start of expression". I've looked up the errors to find a resolution, but I haven't found anything concrete, so I've come here as a last resort. Appreciate the help. Thanks.

import java.net.*;
import java.io.*;
import java.lang.*; 
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.SocketClient;
import org.apache.commons.net.ftp.FTPClient;

public class UIAgent {
    public static void main(String[] args) {

   // Connect to FTP Server

   public void connect(String hostname) throws SocketException, IOException
       {
            connect(hostname, _defaultPort_);
        }

   public int user(String username) throws IOException
        {
            return sendCommand(FTPCommand.USER, username);
        }

   public int pass(String password) throws IOException
       {
           return sendCommand(FTPCommand.PASS, password);
      }

   public int syst() throws IOException
        {
            return sendCommand(FTPCommand.SYST);
        }

   public int type(int fileType) throws IOException
        {
            return sendCommand(FTPCommand.TYPE,
                               __modes.substring(fileType, fileType + 1));
        }


   // Get file from the FTP Server
      public int retr(String pathname) throws IOException
       {
           return sendCommand(FTPCommand.RETR, pathname);
        }


  // Quit the FTP Server
     public int quit() throws IOException
       {
            return sendCommand(FTPCommand.QUIT);
       }

}
}

You are declaring your methods inside another method (the public static void main method) which isn't acceptable in Java. I'd recommend reading the "sticky" thread at the top of this forum for getting started with Java.

Thanks for responding. I'll look into it and come back with a response later.

Thanks

Ok, I think I have taken care of the earlier problem. I am now getting problems associated with the package org.apache.commons.net. For example:

package org.apache.commons.net does not exist
import org.apache.commons.net.SocketClient;

and
^
package org.apache.commons.net.ftp does not exist
import org.apache.commons.net.ftp.FTPClient;

and
^
csci4311\ftp\UIAgent.java:16: cannot find symbol
symbol : variable _defaultPort_
location: class csci4311.ftp.UIAgent
connect(hostname, _defaultPort_);

I have read how packages work, but I am required to use my own package to insert my classes and stuff. How do I use the package org.apache.commons.net.ftp?

If you are using the command line to compile your classes, you need to specify the location where the java compiler would be able to find the apache commons classes i.e. the classpath. Look more into the documentation of the javac command, especially the -cp option. In case you are using an IDE, you need to refer to the documentation of that IDE, esp the section which deals with setting the build path.

Also, post your latest code in case you still face problems.

You've been a big help man. I have set the classpath to where the jar file I need is located and that is taken care of. However, the "can't find symbol" errors remain, mostly related to the FTPCommand(s) in the code below. From what I've read, I must declare it and I have tried multiple times on my own to declare it, but i get even more errors as a result. Also, I keep getting an error with _defaultPort_. I declared it as an int, which resolved the problem, but I get an error related to the row saying "connect(java.lang.String) in csci4311.ftp.UIAgent cannot be applied to (java.lang.String,int)"

import java.net.*;
import java.io.*;
import java.lang.*; 
import java.io.IOException;
import org.apache.commons.net.SocketClient;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTP;


public class UIAgent {

     // Connect to FTP Server

   public void connect(String hostname) throws SocketException, IOException
       { 
           
            connect(hostname, _defaultPort_);
        }

   public int user(String username) throws IOException
        {

            return sendCommand(FTPCommand.USER, username);
        }

   public int pass(String password) throws IOException
       {

           return sendCommand(FTPCommand.PASS, password);
      }

   public int syst() throws IOException
        {

            return sendCommand(FTPCommand.SYST);
        }

   public int type(int fileType) throws IOException
        {

            return sendCommand(FTPCommand.TYPE,
                               __modes.substring(fileType, fileType + 1));
        }


   // Get file from the FTP Server
      public int retr(String pathname) throws IOException
       {

           return sendCommand(FTPCommand.RETR, pathname);
        }


  // Quit the FTP Server
     public int quit() throws IOException
       {

            return sendCommand(FTPCommand.QUIT);
       }

}

I'll continue to work on it on my own. I know it might be an easy resolution, but I am a novice, so I appreciate the help.

> However, the "can't find symbol" errors remain, mostly related to the
> FTPCommand(s) in the code below

What exactly is this FTPCommand class here? Is it your class or does it belong to the commons library? If it's your class, is it placed in the same package as that of the UIAgent class? If it's not your class, I don't see any import there...

> "connect(java.lang.String) in csci4311.ftp.UIAgent cannot be applied
> to (java.lang.String,int)"

This is because the compiler infers that you are making a call to the 'connect' method of your UIAgent class which doesn't accept any second int parameter. You should ideally make a call to the *actual* Ftp client's connect method in your connect method, which is like delegating the connection responsibility to the actual class which does all the heavy lifting. Something like this:

public class WrapperClient {

  private FtpClient ftpClient;

  public Status connect(String host, int port) {
    ftpClient = new FtpClient();
    ftpClient.connect(host, port);
  }

}

i can' speak english very well, but i try to do for resolving your problem;
1/first solution
try with ( try and catch) ;take the body of your methode and round it with the block try and catch

method xxx
try
 {
body of the methode
}
} catch (Exception e) {
            System.out.println("erreur run : " + e.getMessage());
            e.printStackTrace();
}

2/second solution
with thows IOEXception { body of methode}

also paramethers of method connect not valid; your method connect has just one parameters


public void connect(String hostname) throws SocketException, IOException

{

connect(hostname, _defaultPort_);/*****appel with 02 parametrs you will define the second parameters

}

Ok, from reading you responses, I have taken care of the "connect" problem, but the FTPCommand "cannot find symbol" still exists.

What exactly is this FTPCommand class here? Is it your class or does it belong to the commons library? If it's your class, is it placed in the same package as that of the UIAgent class? If it's not your class, I don't see any import there...

FTPCommand is apart of the commons library. I added the "import org.apache.commons.net.ftp.FTPCommand;" after reading your response and got ditto. I set the classpath to where the jar was located, but still got the same errors. Everything is solved, except for these errors. I'll work on it tonight and hopefully I'll respond back saying I've taken care of it. :icon_smile:

Update: Actually adding the "import org.apache.commons.net.ftp.FTPCommand;" solved the "cannot find symbol" errors for the FTPCommand. Now I am getting the same errors for the sendCommand methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.