| Author |
what is the problem with this code
|
Arun C. Giridharan
Ranch Hand
Joined: Jul 11, 2010
Posts: 96
|
|
it's okay at compile time .On execution it gives me a lot of error ......
Error:
---------- Run Java Class ----------
java.lang.NoClassDefFoundError: sub (wrong name: Sub)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Exception in thread "main"
Output completed (0 sec consumed) - Normal Termination //
i'm using Editplus 3 as an Editor.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
In Java you can't have two public classes inside the same .java file. Try separating the classes into different files and see if that works.
Hunter
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
Hunter, the classes aren't public so that's not a problem.
Arun, Java is case sensitive, even in launching. The class is called Sub but you are calling "java sub" -- lowercase s.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Arun C. Giridharan
Ranch Hand
Joined: Jul 11, 2010
Posts: 96
|
|
...wen i execute .... i got a weird output
as shown
---------- Run Java Class ----------
(%s) x = %d, y = %d Super,0,0
(%s) x = %d, y = %d Sub,1,0
Output completed (0 sec consumed) - Normal Termination
Super,0,0 .....these 0's how did they appear ....i have declared x and y for subclass
1)first instance variable come into existence ,right
2)Init block
3)constructor gets executed
on this basis ..... i must get Super,1,0 and Sub 1,0 ...
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
Arun Giridharan wrote:  ...wen i execute .... i got a weird output
---------- Run Java Class ----------
(%s) x = %d, y = %d Super,0,0
(%s) x = %d, y = %d Sub,1,0
Output completed (0 sec consumed) - Normal Termination
google System.out.printf()
Weird I could have sworn that was it Rob. I split them into separate files and it executed fine. Do classes not default to public?
Hunter
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
Hunter McMillen wrote:Weird I could have sworn that was it Rob. I split them into separate files and it executed fine.
Because you were probably using "java Sub", not "java sub".
Do classes not default to public?Hunter
Just like anything else, omitting the access modifier will make the class have package-level access.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
When place both of those classes in a file together I get a java.lang.NoSuchMethodError: main.
Hunter
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
Are you calling it by the class name (Sub) or the file name (whatever you chose)? You should use the class name as always.
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
I am actually running it from an IDE, I just tried it from the command line and received that same error however. I have both classes in a file called Super.java.
Exception in thread "main" java.lang.NoClassDefFoundError: Sub
Caused by: java.lang.ClassNotFoundException: Sub
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: Sub. Program will exit.
Hunter
|
 |
Rene Larsen
Ranch Hand
Joined: Oct 12, 2001
Posts: 1179
|
|
Do you have a complied class called Sub?? I don't think you have....
I think it is called Super$Sub.class - isn't it??
|
Regards, Rene Larsen
Dropbox Invite
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
I don't think so. The brackets in the original code for class Super are closed before Sub is declared.
I find it highly strange that an IDE cannot execute a class it maintains itself. The remaining error usually is a sign of the class not being part of the class path.
|
 |
 |
|
|
subject: what is the problem with this code
|
|
|