1

Alright, here's what's confusing me:

I've been messing around with some simple coding using Netbeans to compile and run some java programs, now that I've gotten a hang of it, I tried building a jar file and opening it outside of Netbeans, but I was smacked in the face by this error:

Error: Could not find or load main class C:\Users\n\Desktop\DirectoryLi
st_1\DirectoryList_1\build\classes\directorylist\DirectoryList_1.jar

What I'm unsure of is how/why this is happening as I can run the program, which produces a textfile with some info in it, completely fine when I'm running it in Netbeans, what's being excluded? What went wrong?

Here's the log from compiling the jar:

Thanks for the help!

init:
deps-clean:
Updating property file: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\built-clean.properties
Deleting directory C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build
clean:
init:
deps-jar:
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build
Updating property file: C:\Users\Maxunknown\Desktop\DirectoryList_1\DirectoryList_1\build\built-jar.properties
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\classes
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\empty
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\generated-sources\ap-source-output
Compiling 1 source file to C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build\classes
compile:
Created dir: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\dist
Copying 1 file to C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\build
Nothing to copy.
Building jar: C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\dist\DirectoryList_1.jar
To run this application from the command line without Ant, try:
java -jar "C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\dist\DirectoryList_1.jar"
jar:
BUILD SUCCESSFUL (total time: 0 seconds)

The very messy and should probably be better organized code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package directorylist;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This program lists the files in a directory specified by the user. The user
 * is asked to type in a directory name. If the name entered by the user is not
 * a directory, a message is printed and the program ends.
 */
public class DirectoryList {

    public static void main(String[] args) {
        try {
//            String directoryName;             // Directory name entered by the user.
//            Scanner scanner;                  // scanner reads from standard input.
//            scanner = new Scanner(System.in); // scanner reads from standard input.
//
//            System.out.print("Enter a directory name: ");
//            directoryName = scanner.nextLine().trim();

            String indent = "";
            String userHome = System.getProperty("user.home");
            String fileSeparator = System.getProperty("file.separator");

            File log = new File(userHome + fileSeparator + "log.txt");
            FileOutputStream stream;
            stream = new FileOutputStream(log);
            PrintWriter out = new PrintWriter(stream);

//            System.out.println(userHome);
//            System.out.println(fileSeparator);

            for (int i = 0; i < userHome.length(); i++) {
                if (userHome.charAt(i) == fileSeparator.charAt(0)) {
                    userHome = userHome.substring(0, i + 1);
                }
            }

            System.out.println("Files in directory \"" + userHome + "\"");
            out.println("Files in directory \"" + userHome + "\"");
            directoryListV2(userHome, 1, out);
        } // end main()
        catch (FileNotFoundException ex) {
            Logger.getLogger(DirectoryList.class.getName()).log(Level.SEVERE, null, ex);
        }
    } // end main()

    public static void directoryListV2(String directoryName, int directoryLevel, PrintWriter out) {

        File directory;        // File object referring to the directory.
        String[] files;        // Array of file names in the directory.
        directory = new File(directoryName);
        String fileSeparator = System.getProperty("file.separator");
        String indent = "";

        for (int i = 0; i < directoryLevel; i++) {
            indent += "  ";
        }

        if (directory.isDirectory() == false) {
            if (directory.exists() == false) {
                System.out.println("There is no such directory!");
                out.println("There is no such directory!");
            } else {
                System.out.println("That file is not a directory.");
                out.println("That file is not a directory.");
            }
        } else {
            files = directory.list();
//            System.out.println("Files in directory \"" + directory + "\":");
//            out.println("Files in directory \"" + directory + "\":");

            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (isDirectory(directoryName + fileSeparator + files[i] + fileSeparator)) {
                        System.out.println(indent + directoryName + fileSeparator + files[i] + fileSeparator);
                        out.println(indent + directoryName + fileSeparator + files[i] + fileSeparator);

                        directoryListV2(directoryName + fileSeparator + files[i], directoryLevel + 1, out);
                    } else {
                        System.out.println(indent + files[i]);
                        out.println(indent + files[i]);
                    }
                }
            }
        }
        out.flush();
    }

    private static boolean isDirectory(String directoryName) {
        File directory;

        directory = new File(directoryName);
        if (directory.isDirectory() == true) {
            if (directory.exists() == true) {
                return true;
            }
        }
        return false;
    }
} // end class DirectoryList
Hobbyist
  • 885
  • 2
  • 10
  • 23
  • The main class declaration in the manifest, probably. – Dave Newton May 10 '12 at 16:45
  • Already asked here http://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class – Juliano May 10 '12 at 16:46
  • How did you try to run the jar? What does the MANIFEST.MF look like? – Thomas May 10 '12 at 16:46
  • That's even more confusing, the manifest is there, with the contents : Manifest-Version: 1.0 X-COMMENT: Main-Class will be added automatically by build – Hobbyist May 10 '12 at 16:49
  • I tried to run the jar file simply by clicking on it, then by typing the full path name of the jar file in the CMD, producing that error message – Hobbyist May 10 '12 at 16:50
  • So from what I understand, jar files cannot be used by none programming people? What's the correct way to build a program for their use then? – Hobbyist May 10 '12 at 17:03
  • Did you try java -jar "C:\Users\n\Desktop\DirectoryList_1\DirectoryList_1\dist\DirectoryList_1.jar" – CodeBlue May 10 '12 at 17:18
  • Yes, I tried that, the code runs in the CMD but no file is produced, I was trying to make a file that any normal person could use, are jar files the right way to go? – Hobbyist May 10 '12 at 17:31
  • Definitely, in fact if everything is set up correctly, you can double click the jar file to run it. – CodeBlue May 10 '12 at 17:34

1 Answers1

0

The problem is that the main class in you jar is not loaded.

For that you will have to make a menifest entry for the Main Class in the jar.

You can find best tutorial on creating executable jar here

Other resource can also found here which is Sun's main guideline for creating executable jars.

Hope this will work for you.

Enjoy !!

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
  • Ahhhh, what you said sounds logical but I'm still being plague by the problem! I've tried a few things: Adding Main-Class: directorylist.DirectoryList to the manifest Doing the same thing with the full path name instead Yet the error occurs! – Hobbyist May 11 '12 at 07:59
  • Then there may be problem in you coding. – Bhavik Ambani May 11 '12 at 08:03
  • But it runs as expected while in the IDE, is that possible? – Hobbyist May 11 '12 at 08:05
  • Then please copy and paste the menifest file. – Bhavik Ambani May 11 '12 at 08:11
  • Manifest-Version: 1.0 Ant-Version: Apache Ant 1.8.3 Created-By: 1.7.0_03-b05 (Oracle Corporation) Class-Path: X-COMMENT: Main-Class will be added automatically by build Main-Class: directorylist.DirectoryList – Hobbyist May 11 '12 at 08:24
  • where is main class definition ? – Bhavik Ambani May 11 '12 at 08:29
  • Is that not the part which says Main-Class: directorylist.DirectoryList? – Hobbyist May 11 '12 at 08:34
  • There is only one class in the package, the beginning of the java file looks like this: package directorylist; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.logging.Level; import java.util.logging.Logger; /** * This program lists the files in a directory specified by the user. The user * is asked to type in a directory name. If the name entered by the user is not * a directory, a message is printed and the program ends. */ public class DirectoryList { public static void main(String[] args) { – Hobbyist May 11 '12 at 08:39