• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic package question  RSS feed

 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following simple class in the folder C:\SCJP\codePractice\pkga that compiles but produces an error when I attempt to run it.
The error is pasted after the code below. I believe it is the package statement that is causing my problem. Please help. The .class file
is also in the same folder.

package pkga;
public class Animal {
public static int i = 1;
public static void main(String[] args) {
Animal animal = new Animal();
System.out.println("protected i = " + animal.i);
}
}

C:\SCJP\CODEPR~1\pkga>java Animal
Exception in thread "main" java.lang.NoClassDefFoundError: Animal (wrong name: p
kga/Animal)
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(Unknown 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: Animal. Program will exit.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edward,

When you run it, your current directory should be pkga, and you should type

java pkga.Animal

The class name is "pkga.Animal", and Java needs you to use the full class name.
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. However please review the following error I am still getting after follow the instructions of navigating to the "pkga" directory and entering
java pkga.Animal. Please forgive me if I am doing something really stupid.

C:\SCJP\CODEPR~1\pkga>java pkga.Animal
Exception in thread "main" java.lang.NoClassDefFoundError: pkga/Animal
Caused by: java.lang.ClassNotFoundException: pkga.Animal
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: pkga.Animal. Program will exit.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I was not only slow, I was not thinking clearly. Your current directory should be C:\SCJP\CODEPR~1\ .
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you really read Darryl Burke post?
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. It worked!!!
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry Seetharaman Venkatasamy. Sometimes mere mortals like myself make mistakes like missing information when we read.
 
Baichuan Huang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in fact ,your command is right, but your variable
named classpath is not right,so you cant find current path.
.
Darryl Burke also can help you.
good luck.


 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jason Chinese",
Please check your private messages regarding an important administrative matter.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jason chinese wrote: . . . your variable named classpath is not right,so you cant find current path. . . .
Disagree. The errors experienced suggest that the PATH and classpath used are both correct.
 
Baichuan Huang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
jason chinese wrote: . . . your variable named classpath is not right,so you cant find current path. . . .
Disagree. The errors experienced suggest that the PATH and classpath used are both correct.

i am so sorry ,i make a mistake.now ,i know it
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:


thanks new information for me too i never tried java from console
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!