This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "packages" Watch "packages" New topic
Author

packages

babla somaiah
Greenhorn

Joined: Feb 02, 2001
Posts: 6
i had coded a simple package which consisted of just one class which was to print a message.
if i am not mistaken,a directory which shares the same name as the package needs to be created in which the classes in the complied form are stored.this obviously means that the .java file will be in the current directory and also the directory with the same name as the package inside which the copiled form of the classes are stored.
until this,it worked fine.but when i tried importing the package(with all necessary class path given prperly in the control panel),it wasnt possible exact error in the bracket(Can't access class mypack1.a. Class or interface must be public, in same package, or an accessible member class.)here,mypack1 is the package name while a is a class in it.also the class has been declared public in contrary to the error.
another question is should the .java file be named as the package name or the class name?if the class name,suppose there are more than one class,then which class name?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi babla,
I am not sure what you are doing, but make sure your second .java file uses the correct import statement:
import mypack1.*;
As far as your second question, that is the idea behind packages. You can have identical class names as long as they are in different packages! A good example is the Date class. It is declared in java.util package and java.sql package. If you are using import statements the last one imported wins ... Therefore, if you import both packages like:
import java.util.*;
import java.sql.*;
and try using Date you will be using the sql version and not the util one. If you want to use the util one you need to specify the full class name:
java.util.Date d = new java.util.Date();
Regards,
Manfred.
babla somaiah
Greenhorn

Joined: Feb 02, 2001
Posts: 6
hey manfred,
thanx for the reply.but my problem is a little diffent ,the code are as follows
package mypack1;
public class a
{
public void display()
{
System.out.println("hello all");
}
}
is the code for the package,and the code below is for importing the package,
import mypack1.a;
public class usage
{
public static void main(String args[])
{
a b = new a();
b.display();
}
}
for importing,i could have as well given as "import mypackage1.*"
but,since there is only one class ie 'a',i gave it as "mypackage.a".rest of the information is same.
regards ,
babla
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Babla,
I am not having any problem running your code. I will try and explain what I did and you can compare it with what you are doing.
1. Place class usage into c:\temp\ directory
2. Create directory named c:\temp\mypack1\
3. Place class 'a' into c:\temp\mypack1\ directory
4. Compiled class 'a' in directory c:\temp\mypack1\
5. Compiled class 'usage' in directory c:\temp\
6. Run class 'usage' from directory c:\temp\
Make sure you compile the class 'a' before you try and compile class usage!
Manfred.
babla somaiah
Greenhorn

Joined: Feb 02, 2001
Posts: 6
hi again manfred,
sorry for troubling u again,but i had and have again tried exactly what u have told me to do.i just couldnt compile the usage.java file.the error what it gave is as follows,
C:\jdk1.2\bin\babla\usage.java:1: Can't access class mypack1.a. Class or interface must be public, in same package, or an accessible member class.
import mypack1.a;
^
even though as the code clearly indicates that a is a public class.
as explaine by u,i have stored my a.class(compiled) in
c:\jdk1.2\bin\babla\mypack1 and usage.java file in,
c:\jdk1.2\bin\babla
but while trying to compile usage.java file,it gave me the above error.
regards,
babla
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Babla,
Try this:
0. Change directory to c:\temp.
1. Copy both files a.java and usage.java into c:\temp.
2. First compile a.java with command: javac -d c:\temp a.java
3. Now compile usage.java with: javac -classpath c:\temp usage.java
To execute use command: java -classpath c:\temp usage
-Peter
babla somaiah
Greenhorn

Joined: Feb 02, 2001
Posts: 6
peter and manfred,
it is working fine now.one last query,suppose we have more than
one class say class b,class c.....naturally in a package all the classes should be public in order to be accessible.so,what will be the .java file name,will it be the package name or any one of the class names or does it matter or not in the sense that any name will do?
regards,
babla
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Actually the class name should be something meaningfull about what the class is/does. It should not be the name of the package (how confusing that would be).
If I have a package mystuff (and I am keeping my source and compiled code together)
c:\java\gui\mystuff
In mystuff I will have many .java files. Say one of them is called CoolGui.java. The first class in this file would be "public CoolGui" (well it doesn't HAVE to be first), however in the same file I might have other - not public classes, that are used by CoolGui. These will have no modifier and be "friendly" (available to any class in the package). I could just as easily keep them in their own files, but let's say that I am lazy (which I sometimes am).
When I compile CoolGui.java I will end up with more than 1 .class file, one for each of the classes in the file, and one for each inner class that might be in there also.

"JavaRanch, where the deer and the Certified play" - David O'Meara
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: packages
 
Similar Threads
from RHE Bonus Questions
Problem with importing classes and finding packages
what is right....and why???
What name to use to save files; what classes are allowed?
Object "Savvy"