• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Please Help !!! (package and accessibility)

 
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.
 
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
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic