Results 1 to 4 of 4
Thread: Error from a beginners tutorial
- 07-22-2011, 02:48 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
Error from a beginners tutorial
Appologies in advance for printing blocks of code. I am right at the beginning of learning Java and have hit a bump with a tutorial scripts...
This is the tutorial page i am learning from:
Introduction to Java Access Modifiers | Java Beginner
Could somebody possible assist...?
I am have copied and understand the 1st example name 'SubclassInSamePackage'.
I have compiled the bytecode.
However when i try and run the class i get the following error:
This is the code from the tutorial:D:\WORK\Java>java SubclassInSamePackage
Exception in thread "main" java.lang.NoClassDefFoundError: SubclassInSam
(wrong name: pckage1/SubclassInSamePackage)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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: SubclassInSamePackage. Program will exit
Java Code:package pckage1; class BaseClass { public int x = 10; private int y = 10; protected int z = 10; int a = 10; //Implicit Default Access Modifier public int getX() { return x; } public void setX(int x) { this.x = x; } private int getY() { return y; } private void setY(int y) { this.y = y; } protected int getZ() { return z; } protected void setZ(int z) { this.z = z; } int getA() { return a; } void setA(int a) { this.a = a; } } public class SubclassInSamePackage extends BaseClass { public static void main(String args[]){ BaseClass rr = new BaseClass(); rr.z = 0; SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access Modifiers - Public System.out.println("Value of x is : "+subClassObj.x); subClassObj.setX(20); System.out.println("Value of x is : "+subClassObj.x); //Access Modifiers - Private // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are private /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access Modifiers - Protected System.out.println("Value of z is : "+subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : "+subClassObj.z); //Access Modifiers - Default System.out.println("Value of x is : "+subClassObj.a); subClassObj.setA(20); System.out.println("Value of x is : "+subClassObj.a); } }
- 07-22-2011, 02:54 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
ps, i am still under 24hours of learning java. I have come from PHP/JS...etc
If anyone knows of any other guides which they think might be better suited, i would be very appreciative to know them :)
- 07-22-2011, 03:47 PM #3
Your problem is your class is in a package: pckage1.ava.lang.NoClassDefFoundError: SubclassInSamePackage
(wrong name: pckage1/SubclassInSamePackage)
One solution is to remove the package statement from your program and recompile.
Another solution is to create a folder with the SAME name as the package name, move your source into that folder and compile it. Then move up one level to the folder containing the folder with the class file and issue:
java <PACKAGENAME>.<CLASSNAME>
where the words in <>s is what you have named your stuff.
For example in the folder containing the pckage1 folder enter:
java pckage1.SubclassInSamePackageLast edited by Norm; 07-22-2011 at 03:49 PM. Reason: fix spelling
- 07-22-2011, 04:39 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Error Using Email Client tutorial from Java-Tips
By lasyn in forum AWT / SwingReplies: 6Last Post: 09-28-2011, 08:41 PM -
Beginners fault
By NGE in forum New To JavaReplies: 6Last Post: 06-23-2011, 07:28 PM -
I am getting a cannot find symbol error on this code from Oracle Java tutorial site
By bigsonny in forum New To JavaReplies: 8Last Post: 06-15-2011, 05:26 AM -
Help with beginners program
By DanK in forum New To JavaReplies: 6Last Post: 12-15-2008, 05:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks