Results 1 to 8 of 8
  1. #1
    Swarvy is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default Getting errors for some reason

    Hi, I'm knew to java, although i do know some C/C++ (i've just finished a noughts and crosses game in C++). Anyway, I'm thinking of getting into java, and i have a netbeans, and when i make a program, it runs find through the netbeans IDE, but if i try running it in command prompt, using "java main", it comes up with a load of stuff, something like this:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: main (wrong name: javaapplication2/Main)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            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)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    (this is a hello world program btw)

    How can i get it to run through command prompt?

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    I don't know what Netbeans is, but to run a class you use "java ClassName" and it calls the main method for you.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    java.lang.NoClassDefFoundError: main (wrong name: javaapplication2/Main)
    What is the name of the class file you are trying to execute? Is it in a package?
    Your class file has the wrong name. It should be Main.class. To execute it you need to put it in folder javaapplication2 and move up one level and use: java javaapplication2.Main

    To check the above I renamed a class file to main.class. The class file contains the class ArrayUtil in package NormsTools.
    C:\Temp>java main
    Exception in thread "main" java.lang.NoClassDefFoundError: main (wrong name: NormsTools/ArrayUtil)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Swarvy View Post
    How can i get it to run through command prompt?
    Is that Java source file name is main.java? Actually source file name should be same as the class name, with case equivalent.

  5. #5
    Swarvy is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    lol. I'm not that much of a newbie that i tried running a .java file, :p I compiled the java file in netbeans and then went to the directory of the .class file, and typed "java main" (the file is main.class, but I heard that you shouldn't put the file extension in the argument for some reason, I had no idea if that's true). anyway, I had another go, this time, I tried doing it from different directories, this is what i got for each:

    >java ./build/classes/main

    Output:
    Exception in thread "main" java.lang.NoClassDefFoundError: //build/classes/main
    Caused by: java.lang.ClassNotFoundException: ..build.classes.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)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

    It does this same thing from which ever directory i try.

    [edit] I copied the class file into C:\ just to see if that would make any difference, but it didnt. when I tried "java main", "java c:\main" or "java c:\main.class", it comes up with the same thing.

    This is the source:
    Java Code:
    package javaapplication2;
    
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("Hello World");
        }
    
    }
    Lol, who would have thought that I would get this much trouble from a "hello world" program after knowing loads of C and C++ and writing a tictactoe game using the windows api. Am I just unlucky, or just a complete fool? :P

    [/edit]
    Last edited by Swarvy; 09-30-2008 at 01:35 PM. Reason: Add more detailed information onto the post

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Swarvy is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    When i compile like that, The output in netbeans is:
    Java Code:
    Compiling 1 source file to C:\Documents and Settings\Dominic Finch\My Documents\NetBeansProjects\JavaApplication2\build\classes
    compile:
    Building jar: C:\NetBeansProjects\JavaApplication2\dist\JavaApplication2.jar
    Not copying the libraries.
    To run this application from the command line without Ant, try:
    java -jar "C:\NetBeansProjects\JavaApplication2\dist\JavaApplication2.jar"
    jar:
    BUILD SUCCESSFUL (total time: 0 seconds)
    when i run it through the IDE, I get a dialob box saying that it couldn't find package main in main.java, but then it runs the program anyway. In command prompt I get:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: main (wrong name: Main)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            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)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Java is case sensitive. Make sure the .java filename is exactly the same as the public class that is in that file. Your class is Main in the file main.java Change the filename to Main.java

Similar Threads

  1. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 05:55 PM
  2. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-02-2008, 12:55 AM
  3. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 10:32 AM
  4. Errors in constructor
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 07-01-2007, 06:35 PM

Posting Permissions

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