Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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

Basic package question

 
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.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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\ .
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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.
 
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.


 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jason Chinese",
Please check your private messages regarding an important administrative matter.
 
Marshal
Posts: 70353
283
  • 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
 
Ranch Hand
Posts: 151
MyEclipse IDE Python Java
  • 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
 
Well THAT's new! Comfort me, reliable tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic