• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem running Inner Class Applications...

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have just started learning about inner classes. To observe the effect of inner classes I tried to run the following application taken from a book. It compiled fine but gave a huge runtime error. The program is as below:



class InnerTest{
public static void main(String[] args){
Outer o = new Outer();
Outer.Inner i = o.new Inner();//one way
i.seeOuter();
i = new Outer().new Inner();//another way
i.seeOuter();
}
}
public class Outer{
private String s = "outer s";
void makeAndSeeInner(){
System.out.println(this);//refers to Outer.this
Inner i = new Inner();//No need of Outer.this explicitly, because, Outer.this already exists here.
i.seeOuter();
}
void seeInner(){
System.out.println(s);//How to see Inner s here? You can't, because Inner.this not present.
}
strictfp class Inner{
private String s = "inner s";
void seeOuter(){
System.out.println(this);
System.out.println(Outer.this.s);//Need to mention Outer because this refers to Inner.this here.
System.out.println(s);//Or, this.s
}
}
abstract class AbInner{
private String s = "abstract inner s";
void seeOuter(){
System.out.println(this);//this refers to the subclass not the abstract class.
System.out.println(Outer.this.s);
System.out.println(s);
}
abstract void abMethod();
}
class Some extends AbInner implements Runnable, Animal{//can extend and implement
public void run(){}
void abMethod(){
System.out.println(this);
System.out.println(super.s);
}
}
public static void main(String[] args){
Inner i = new Outer().new Inner();
//Inner i = new Inner();//can't exist w/o outer class instance
i.seeOuter();
Outer o = new Outer();
o.makeAndSeeInner();
o.seeInner();
//new Outer().makeAndSeeInner();
Some so = new Outer().new Some();
so.seeOuter();
so.abMethod();
}
}
interface Animal{
}




The runtime exception that i received was:



Exception in thread "main" NoClassDefFoundException : Outer
caused by java.lang.ClassNotFoundException : Outer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrevileged(Native Method)
at java.lang.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)


I got similar runtime exception for other two Inner Class Apps also that i tried to run.Please Help!!!


Regards,

Rekha
 
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It ran perfectly well when I tried it.
Please use code tags on your quoted code.
Maybe you forgot to compile all the classes?
 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

This code is stored under the name Outer.java on my system.
I compiled it : javac Outer.java
It compiled fine and I checked that InnerTest.class, Outer.class, Outer$Inner.class, Outer$AbInner.class and Outer$Some.class were present in the directory after compilation. Then which class file is it looking for..?

Regards
Rekha
[ March 13, 2008: Message edited by: Rekha Anand ]
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, I am afraid.
 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody Please Help !!!
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Less of the thank you; we are trying to help.

Which version of jdk are you using? Have you done an "ls" (Unix/Linux) or "dir" (DOS/Windows) call while you are in the terminal window? Have you got a . for current directory in your CLASSPATH?

I have just tried compiling it and it wouldn't compile InnerTest, presumably because of the file name (earlier I copies InnerTest into its own file). It would however compile and run Outer. It would appear you can't find Outer.

I suspect you have either slipped off the directory you think you are in, or you have the . missing from your CLASSPATH.
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and here is a copy-and-paste from my terminal window

[Campbell@queeg ~]$ cd java/applications
[Campbell@queeg applications]$ gedit Outer.java&
[1] 3157
[Campbell@queeg applications]$ javac Outer.java
[Campbell@queeg applications]$ java Outer
Outer$Inner@3e25a5
outer s
inner s
Outer@19821f
Outer$Inner@addbf1
outer s
inner s
outer s
Outer$Some@a90653
outer s
abstract inner s
Outer$Some@a90653
abstract inner s
[Campbell@queeg applications]$ javac InnerTest.java
javac: file not found: InnerTest.java
Usage: javac <options> <source files>
use -help for a list of possible options
[Campbell@queeg applications]$ ls Outer*
Outer$AbInner.class Outer$Inner.class Outer$Some.class
Outer.class Outer.java
[Campbell@queeg applications]$

 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir,
I am using jdk1.6.0_05 downloaded from sun. My classpath is properly set with dot.And I am running it from the proper dir.

Regards
Rekha
[ March 13, 2008: Message edited by: Rekha Anand ]
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, I am afraid. As I said, I have compiled and run your code twice on different computers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic