Results 1 to 6 of 6
  1. #1
    ruud00000 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default runtime error 'could not find main class' ?

    I want to write a simple console application showing the content of two object instances. Following code compiles without error however gives the following runtime error : 'could not find the main class: ConsoleApplication.KlantType. Program will exit.'

    If I 'clean an build project' (Netbeans) then I get
    Created dir: D:\Java\NetBeansProjects\ConsoleApplication1\dist
    Not copying the libraries.
    Building jar: D:\Java\NetBeansProjects\ConsoleApplication1\dist\ ConsoleApplication1.jar
    To run this application from the command line without Ant, try:
    java -jar "D:\Java\NetBeansProjects\ConsoleApplication1\dist \ConsoleApplication1.jar"
    jar:
    BUILD SUCCESSFUL (total time: 1 second)

    If I do that I get following error message on the dos prompt:
    Exception in thread "main" java.lang.NoClassDefFoundError: consoleapplication1/M
    ain
    Caused by: java.lang.ClassNotFoundException: consoleapplication1.Main
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: consoleapplication1.Main. Program will exit.

    What is wrong? How to solve?

    Here's the code:

    package ConsoleApplication1;

    public class KlantType {

    private String Naam;

    public KlantType(String naam) {
    Naam = naam;
    }

    public void setNaam(String value) {
    Naam = value;
    }

    public String getNaam() {
    return Naam;
    }


    private static String klantNaam;

    public static void main(String[] args) {
    KlantType klantObjectInstance1 = new ConsoleApplication1.KlantType("Janssen");
    KlantType klantObjectInstance2 = klantObjectInstance1;
    klantObjectInstance2.setNaam("Pietersen");

    System.out.println("Waarden na toekenning waarde Pietersen aan eigenschap Naam van instance klant2 van Object Klant");
    klantNaam = klantObjectInstance1.getNaam();
    System.out.println("Klant 1 : " + klantNaam);
    klantNaam = klantObjectInstance2.getNaam();
    System.out.println("Klant 2 : " + klantNaam);
    }

    }
    Last edited by ruud00000; 12-31-2010 at 12:14 AM.

  2. #2
    tecno40 is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Add "public static void main(String [ ] args){}"

    You need a main class for the applet to run.

  3. #3
    ruud00000 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by tecno40 View Post
    Add "public static void main(String [ ] args){}"

    You need a main class for the applet to run.
    That's already there ?

    See line just above
    KlantType klantObjectInstance1 = new ConsoleApplication1.KlantType("Janssen");
    KlantType klantObjectInstance2 = klantObjectInstance1;
    klantObjectInstance2.setNaam("Pietersen");

    System.out.println("Waarden na toekenning waarde Pietersen aan eigenschap Naam van instance klant2 van Object Klant");
    klantNaam = klantObjectInstance1.getNaam();
    System.out.println("Klant 1 : " + klantNaam);
    klantNaam = klantObjectInstance2.getNaam();
    System.out.println("Klant 2 : " + klantNaam);

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    You are trying to run a class namned “consoleapplication1.Main”, you
    should run “ConsoleApplication1.KlantType”.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #5
    ruud00000 is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Well actually I just do what NetBeans suggests: at the command prompt I enter
    java -jar "D:\Java\NetBeansProjects\ConsoleApplication1\ dist \ConsoleApplication1.jar"

    On the other hand if I go to the file KlantType.java in the NetBeans navigator, right click and chose to run the file directly it works well.

    Can I also run this KlantType.java file directly from the command prompt (Windows). How exactly?

    If I open the file in JavaEditor instead of NetBeans and run it from there I get the following error message in the DOS window. Note that now it DOES try to open the correct (public) class 'ConsoleApplication1.KlantType', however it fails to run:

    Exception in thread "main" java.lang.NoClassDefFoundError: ConsoleApplication1/K
    lantType
    Caused by: java.lang.ClassNotFoundException: ConsoleApplication1.KlantType
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    Could not find the main class: ConsoleApplication1.KlantType. Program will exit

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Quote Originally Posted by ruud00000 View Post
    Can I also run this KlantType.java file directly from the command prompt (Windows). How exactly?
    Java Code:
    cd YOUR_CLASSPATH
    java -cp . ConsoleApplication1.KlantType
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2011, 10:48 PM
  2. Replies: 3
    Last Post: 11-11-2010, 09:12 AM
  3. [SOLVED] Could not find the main class: error...
    By bobleny in forum New To Java
    Replies: 21
    Last Post: 03-02-2010, 11:53 AM
  4. Replies: 1
    Last Post: 07-03-2009, 09:41 PM
  5. Replies: 1
    Last Post: 06-30-2009, 03:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •