package -- what am I doing wrong?

J

Jeff Sandler

Java-ians of all levels,
Managing and importing classes is supposed to be easy using PACKAGE.
However, I cannot make it work. Here is a recent attempt:

the file called packageThingie.java contains

package myPackage;
public class packageThingie
{
}

the file called testMyPackage.java contains
import myPackage.*

public class testMyPackage
{
testMyPackage() //default constructor
{
} //closes constructor

public static void main(String args[])
{
testMyPackage myTMP = new testMyPackage();
}
}

When I compile, I keep getting a message saying the package myPackage
doesn't exist. FYI, both .java files and their corresponding .class
files are in the C:\j2sdk1.4.1\bin directory.

HELP!!
 
C

Chris Smith

Jeff said:
Managing and importing classes is supposed to be easy using PACKAGE.
However, I cannot make it work. Here is a recent attempt:
[...]

When I compile, I keep getting a message saying the package myPackage
doesn't exist. FYI, both .java files and their corresponding .class
files are in the C:\j2sdk1.4.1\bin directory.

Ack! Get them out of there. Why are they there? Create a new
directory somewhere for your project, and put the code there. Your own
applications don't belong in the Java 2 SDK install directory.

Once you've done that, you can make packages work. When a class is in a
package, it belongs in a subdirectory named after the package. If your
classpath includes a directory like c:\myapp (or, if you don't have a
classpath, c:\myapp is the current working directory), a class file for
a class in a package called "myPackage" should be in a directory called
"c:\myapp\myPackage". It's recommended that you put the source files in
the same directory structure. In the case of class files, the javac
compiler will build that directory structure for you if you specify a -d
option.

It's also customary, by the way, to use all lowercase for packages
(mypackage instead of myPackage), and capitalize class names
(TestMyPackage instead of testMyPackage).

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
L

Larry Coon

Jeff said:
Java-ians of all levels,
Managing and importing classes is supposed to be easy using PACKAGE.

My advice, having gone through this myself not too
long ago:

1. Do everything Chris says. (This is regarding both
his specific advice to you in this thread and his
advice in general.)

2. If you're still having problems, make sure you
understand how classpath works. In fact, I should
word this more strongly -- don't bother trying to
make packages work UNTIL you understand how classpath
works.

3. I got stuck by not correctly understanding the
search criteria when import is used with a "*". If
you're still having problems, try doing:
import myPackage.packageThingie;
rather than:
import myPackage.*;
 
J

Joona I Palaste

My advice, having gone through this myself not too
long ago:
1. Do everything Chris says. (This is regarding both
his specific advice to you in this thread and his
advice in general.)

Chris, here's your chance to ask Jeff to pay you $10 million. =)

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top