Does main method have to be static? Is there a way to go around it?

I am trying to write a class that will recursively walk through directory listing of a given ftp adress and store it in a text file in this format:
<path> , DIR // if directory
<path> , file // if file
Since I am not yet experienced to do my own socket programming(tried and did not work out) I used Secure iNet Factory's library. And since I will be running multiple instances of this class I concluded that my fields and methods should not be static. Here is the class I wrote:
import com.jscape.inet.ftp.Ftp;
import com.jscape.inet.ftp.FtpException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
public class test {
     private String path = "/";
     private String name;
     private String adres;
     // create new Ftp instance and connect.
     public test(String Name, String Adres) throws IOException {
          name = Name;
          adres = Adres;
          System.out.println("Test class: " + name);
          try {
               Ftp ftp = new Ftp(adres,"anonymous","anonymous");
               ftp.connect();
               Enumeration e = ftp.getDirListing();
               iterate(path, ftp);
               ftp.disconnect();
          catch (FtpException ex) {
               Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
     void iterate(String aStartingDir, Ftp ftp) throws FtpException, IOException {
          //Set the path.
          path = path + aStartingDir + "/";
          // Change directory.
          ftp.setDir(aStartingDir);
          // Iterate through directory list.
          Enumeration e = ftp.getDirListing();
          while(e.hasMoreElements()){
               Object f = e.nextElement();
               // Recursive call if item is directory.
               if(f.toString().startsWith("drwxrwxrwx") &&
                    // Ignore "." and ".."
                    !f.toString().substring(55).trim().equalsIgnoreCase(".") &&
                    !f.toString().substring(55).trim().equalsIgnoreCase("..")) {
                    // Write to index file.
                    URL dirUrl = FtpCheck.class.getResource("./index/"); // get the directory.
                    URL fileUrl = new URL(dirUrl, name + ".txt"); // construct the file path.
                    String filePath = fileUrl.getPath().replaceAll("%20", " "); // fix spaces.
                    BufferedWriter file = new BufferedWriter(new FileWriter(filePath,true));
                    file.write((path + f.toString().substring(55).trim()).substring(3)
                         + ",DIR\n");
                    file.close();
                    iterate(f.toString().substring(55).trim(),ftp);
               // Skip entries "." and ".."
               else if (f.toString().substring(55).trim().equalsIgnoreCase(".") ||
                         f.toString().substring(55).trim().equalsIgnoreCase("..")) {
                         continue;
               // Add files to the index.
               else {
                    URL dirUrl = FtpCheck.class.getResource("./index/"); // get the directory.
                    URL fileUrl = new URL(dirUrl, "index.txt"); // construct the file path.
                    String filePath = fileUrl.getPath().replaceAll("%20", " "); // fix spaces.
                    BufferedWriter file = new BufferedWriter(new FileWriter(filePath,true));
                    file.write((path + f.toString().substring(55).trim()).substring(3)
                         + ",file\n");
                    file.close();
          // End of listing reached, go one directory up.
          ftp.setDirUp();
          // And remove the last directory name from path.
          path = path.substring(0,path.lastIndexOf("/"));
          path = path.substring(0,path.lastIndexOf("/")) + "/";
}which compiles fine.
Here is the class that invokes it:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
* @author Sentinel
public class NewClass {
     public NewClass() {
     public static void main(String args[]) throws MalformedURLException, FileNotFoundException, IOException {
          index t = new index("El Naga","elnaga.sytes.net");
          t.start();
          index t1 = new index("Afacan","afacan.myftp.org");
          t1.start();
     public class index extends Thread {
          String name;
          String adres;
        public index(String Name,String Adres) {
            name = Name;
            adres = Adres;
        @Override
        public void run() {
            try {
                test checker = new test(name,adres);
            catch (IOException ex) {
                 Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}when I hit run build fails:
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Sentinel\My Documents\NetBeansProjects\ftpCheck\build\classes
C:\Documents and Settings\Sentinel\My Documents\NetBeansProjects\ftpCheck\src\NewClass.java:19: non-static variable this cannot be referenced from a static context
                index t = new index("El Naga","elnaga.sytes.net");
                          ^
C:\Documents and Settings\Sentinel\My Documents\NetBeansProjects\ftpCheck\src\NewClass.java:20: non-static variable this cannot be referenced from a static context
                index t1 = new index("Afacan","afacan.myftp.org");
                           ^
2 errors
BUILD FAILED (total time: 0 seconds)I (now)know that main method has to be static. I'm stuck and in need of help to overcome this obstacle. Is there a way to do this?

a stab in the dark, but what if you make index a non-internal class. make it a stand-alone class in it's own file. Either that or make it a static inner class (almost the same thing).
Your problem is that an inner class needs an instance of the outer class to start on, and you're not doing this. Another solution would be to do this kludge:
index t = new NewClass().new index("El Naga","elnaga.sytes.net");  // I think this is how to call itBut again better is to make index a stand-alone class.
Edited by: Encephalopathic on Oct 28, 2008 8:06 AM

Similar Messages

Maybe you are looking for

  • Need help, unable to find my Serial Number

    I recently moved to a new house, with all the ensuing hassles. Shortly after moving I decided to upgrade my computer to Windows 8. And in order to try and get rid of some old problems I only kept the private files and did a clean update. After (succe

  • Links in blog archive not working

    Can anybody help me get more reliable performance from my iWeb archives page? http://web.mac.com/truthmaker/Site/Blog/Archive.html I am getting very frustrated with the Archives page for the blog I've created with iWeb. Often -- very, very often, the

  • Mail in Maverick crashes when I try to attach a file.

    Each time I try to attach a file to an email in Maverick Mail crashes...I have fixed permissions and all the other recommended steps...nothing works???

  • Why does my iMovie shut down when trying to import videos

    When I am trying to import video to my library my iMovie closes and I get a "iMovie quit unexpectedly". How can I fix this?

  • SO FUSTERATED PLEASE HELP!!!!

    I keep getting a message to contact ITUNES supoport under payment type when I enter my credit card which is a visa card!  I go to the web site but I do not understAND WHAT TO LOOK FOR!  pLEASE HELP