Results 1 to 9 of 9
  1. #1
    rodneyc8063 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Exception in thread "main" wrong name

    Hey

    Im a bit new to java and was trying to run program via the command line and was getting this error

    Exception in thread "main" java.lang.NoClassDefFoundError: FileViewer (wrong nam
    e: MCG338/FileViewer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :791)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:14
    2)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:472)

    Im using netbeans and am able to compile and run this with no problem, but when it comes to the command line i hit the above issue.

    I made sure that my path is set to the correct directory and I also went to the directory that netbeans created and stored off the class file that was created when i built the project.

    So im using the same class file that runs fine in netbeans but still hitting the above error.

    What am i missing?

    Thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Exception in thread "main" wrong name

    I bet your FileViewer class is in a package (MCG338?); if so you should call it by supplying its full name to the java.exe command: MCG338.FileViewer

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default Re: Exception in thread "main" wrong name

    Also you must be in the folder that contains the folder at the head of the package path (MCG338)
    or you must set the classpath to the folder that contains the package path folder.

  4. #4
    rodneyc8063 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" wrong name

    Ok im a bit further along now, i dont get a big error but now I get another error

    I use this command to run now

    java MCG338.FileViewer

    and i get

    Error: Could not find or load main class MCT338.FileViewer

    Though i can see my class being there in this folder.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default Re: Exception in thread "main" wrong name

    Since your code is in a package you must have the classpath point to the folder containing the MCG338 folder. The classpath defaults to the current folder. You must be in the folder that contains the MCG338 folder when you issue the command:
    java MCG338.FileViewer

    You must be able to see the MCG338 folder when you issue the command.

  6. #6
    rodneyc8063 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" wrong name

    Hm

    In the command line i navigate to the folder MCG338 and i run the command

    set path=C:\Folder where my java.exe is located

    I issue the command: java MCG338.FileViewer and i still get the same error.

    Thanks for the speedy replies, it helps!

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default Re: Exception in thread "main" wrong name

    You should be in the next folder up. You see the MCG338 folder in that folder.

    You should NOT be in the MCG338 folder.

  8. #8
    rodneyc8063 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" wrong name

    OMG!

    Argh!

    Thanks so much Norm!

    I was confused by your post as you said i need to see the folder, i thought i had to be IN the actual folder to set the path and run that command.
    I find that a bit counter-intuitive that I have to be outside the folder of the actual class file, does anyone know why that is so? I would have thought I need to be IN the folder, just like when i run the javac command....

    -------------------------------------------------------------------------

    To summarize for anyone else reading this thread for future reference:

    I created a java file with a package of MCG338 in a file system like C:\Java Homework

    I set my path variable to the folder of the FileViewer.java (C:\Java Homework\MCG338\), and also while in this folder in the command line I ran the command

    javac FileViewer.java

    to compile the class file

    Now when i run DIR i can see the java and class file in C:\Java Homework\MCG338\

    To actually run the command "java" i need to specify the package that the java file is in AND also be in the folder where i can see the folder name.

    So in the directory of C:\Java Homework I can see the folder MCG338. Here I run "java MCG338.FileViewer" and the file compiles successfully....

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default Re: Exception in thread "main" wrong name

    have to be outside the folder of the actual class file, does anyone know why that is so?
    The java program looks for the class file using the class's full name which includes the package. If you are in the folder with the class file it won't find the starting folder of the package path.

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  3. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  4. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  5. Replies: 1
    Last Post: 07-16-2009, 10:35 AM

Posting Permissions

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