Results 1 to 4 of 4
  1. #1
    freelance84 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default 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:
    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
    This is the code from the tutorial:
    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);
        	
        	
        }
    }

  2. #2
    freelance84 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    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 :)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    ava.lang.NoClassDefFoundError: SubclassInSamePackage
    (wrong name: pckage1/SubclassInSamePackage)
    Your problem is your class is in a package: pckage1.
    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.SubclassInSamePackage
    Last edited by Norm; 07-22-2011 at 04:49 PM. Reason: fix spelling

  4. #4
    freelance84 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Thumbs up

    ooo... thank you very much. I guess that all makes a lot of sense actually

Similar Threads

  1. Error Using Email Client tutorial from Java-Tips
    By lasyn in forum AWT / Swing
    Replies: 6
    Last Post: 09-28-2011, 09:41 PM
  2. Beginners fault
    By NGE in forum New To Java
    Replies: 6
    Last Post: 06-23-2011, 08:28 PM
  3. Replies: 8
    Last Post: 06-15-2011, 06:26 AM
  4. Help with beginners program
    By DanK in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •