Article View: borland.public.jbuilder.java.language
Article #8037Compiling and Accessing Packages?
From: hi
Date: Sun, 29 Apr 2001 01:25
Date: Sun, 29 Apr 2001 01:25
119 lines
3836 bytes
3836 bytes
I would like to know how to compile a package from the classes I am
including below. Then I would like to know how to access the package when
the .class files are left in the directory with the package name, using the
method:
javac -classpath .; blah blah blah
I am using JDK1.3 on win98. I have no CLASSPATH set on my computer. Here
are the classes I created and their directories, and what I tried to do:
directory structure:
D:\
---java
--------Animals
---------------Dogs
--------------------Spaniel.java
--------------------Poodle.java
--------------------TestDogs.java
--------Chap5
---------------TestPackage.java
Spaniel.java:
==============
package Animals.Dogs;
public class Spaniel
{
public static void speak()
{
System.out.println("ruff...ruff..I am a Spaniel!");
}
}
===============================
Poodle.java:
==================
package Animals.Dogs;
public class Poodle
{
public static void speak()
{
System.out.println("yip..yip..I am a poodle.");
}
}
=================================
TestDogs.java:
===================
package Animals.Dogs;
public class TestDogs
{
public static void main(String[] args)
{
Spaniel.speak();
Poodle.speak();
System.out.println("This is the class TestDogs");
}
}
==================================
TestPackage.java:
====================
import Animals.Dogs.*;
public class TestPackage
{
public static void main(String[] args)
{
Spaniel.speak();
Poodle.speak();
System.out.println("This class is in not in the package.");
}
}
==================================
To compile the package, I used this command:
D:\java\Animals\Dogs>javac -classpath D:\java TestDogs.java
and all the .class files for Poodle.java, Spaniel.java, and TestDogs.java
appeared in the folder D:\java\Animals\Dogs. Then I tried to access the
package from TestPackage.java in the D:\java\Chap5 directory:
D:\java\Chap5>javac -classpath .;D:\java\Animals\Dogs TestPackage.java
and I get this output:
===================
TestPackage.java:1: package Animals.Dogs does not exist
import Animals.Dogs.*;
^
TestPackage.java:6: cannot access Spaniel
bad class file: D:\java\Animals\Dogs\Spaniel.class
class file contains wrong class: Animals.Dogs.Spaniel
Please remove or make sure it appears in the correct subdirectory of the
classpa
th.
Spaniel.speak();
^
2 errors
====================================
Then I tried this:
D:\java\Chap5>javac -classpath .;D:\java TestPackage.java
which seemed to compile because I got this:
D:\java\Chap5>
so, I entered:
D:\java\Chap5>java TestPackage
which gave me this output:
================
Exception in thread "main" java.lang.NoClassDefFoundError:
Animals/Dogs/Spaniel
at TestPackage.main(TestPackage.java:6)
========================================
Technical Details
Message-ID:
<3aeb6dd9_1@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out-sjo.usenetserver.com!feed2.onemain.com!feed1.onemain.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,e13db2519d61bb0d,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-28 18:44:03 PST From: "hi" <noneofyourbusiness@none.com> Newsgroups: borland.public.jbuilder.java.language Subject: Compiling and Accessing Packages? Date: Sat, 28 Apr 2001 19:25:57 -0600 Lines: 121 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 208.187.59.181 Message-ID: <3aeb6dd9_1@dnews> X-Trace: dnews 988507609 208.187.59.181 (28 Apr 2001 18:26:49 -0700) Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!cyclone-sjo1.usenetserver.com!news-out-sjo.usenetserver.com!feed2.onemain.com!feed1.onemain.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews