Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help !!! (package and accessibility)

 
Siu Yong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to java. I want to set up different packages such as com.mypackage and com.hispackage so that I can test the access modifiers such as private, protected and default. I have tried a program as follow :

package com.myPackage;

public class MyClass{
public static void main(String [] args){
System.out.println("Hello World");
}
}

I can compile the program but when I run the program I get the following :

C:\MyProgram>java MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass (wrong name:
com/myPackage/MyClass)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)


I think the problem is because the JVM cannot see the class file

Can someone help me to set up the different packages in my win XP environment so that I can test the programs I write in any different folder. My question is how to set up a real developement environment with many different packages.
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
create a directory called "com" like this:
c:\myJava>mkdir com

Also, create a subdirectory called "myPackage" like this c:\myJava\com>myPackage

Save the the file in your post (MyClass.java) in myPackage folder.
Then go to the myPackage folder and enter this:

c:\myJava\com\myPackage>javac MyClass.java

You will get compile successful message.

Then to run the class file do the following:
C:\myJava>java com.myPackage.MyClass
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic