Results 1 to 9 of 9
- 11-13-2011, 09:29 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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
- 11-13-2011, 09:34 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-13-2011, 09:40 PM #3
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.
- 11-13-2011, 11:01 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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.
- 11-13-2011, 11:05 PM #5
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.
- 11-13-2011, 11:17 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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!
- 11-13-2011, 11:38 PM #7
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.
- 11-14-2011, 12:22 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
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....
- 11-14-2011, 12:31 AM #9
Re: Exception in thread "main" wrong name
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.have to be outside the folder of the actual class file, does anyone know why that is so?
Similar Threads
-
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks