This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the problem with this code

 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When place both of those classes in a file together I get a java.lang.NoSuchMethodError: main.

Hunter
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic