Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

question--package name

Thread view: 
Xiaoshen Li - 14 Dec 2005 15:41 GMT
Dear All,

I have a Java class ToolBox inside the file ToolBox.java, which has many
static methods. I put it in(Linux OS): /home/Xiaoshen/programming/java/

Now if I have a file in the same directory, I have no problem in using
ToolBox's static methods. For example, in the file Test.java, I can have:

int iNum = ToolBox.averageTwoInts(2, 4);

But if I am working in a different directory, say,
/home/Xiaoshen/programming/java/homework/proj_1/. The problem came:
ToolBox cannot be found.

(
I know setting CLASSPATH solves the problem:
export CLASSPATH= /home/Xiaoshen/programming/java/:.
)

Here I am trying to go through package name. I have added the line on
the top of the file:

import home.Xiaoshen.programming.java.*;

But it doesn't work. Should I add a line to the top of the file
ToolBox.java:

package mypackage;

Could anybody help me out? Thank you very much.
BartCr - 14 Dec 2005 19:24 GMT
If you put the package statement in you'll need to make sure the .class
file is in /home/Xiaoshen/programming/java/mypackage. And the set your
classpath to /home/Xiaoshen/programming/java/:.

Test can be in another package, or no package. And looks like this

import mypackage.Test;

public class Test {
...
   int iNum = ToolBox.averageTwoInts(2, 4);
...
}

Bart
zero - 14 Dec 2005 19:38 GMT
> Dear All,
>
[quoted text clipped - 28 lines]
>
> Could anybody help me out? Thank you very much.

The package name is appended to the classpath to find the .class file.  
So, if you use a package and no classpath, Java looks for ToolBox at
location

./mypackage/ToolBox.class

If you do use the classpath mentioned above, Java would expect the class
file to be at

/home/Xiaoshen/programming/java/mypackage/ToolBox.class

So basically what you're trying to do can only be done by setting the
classpath correctly.  Using a package is a good idea, but you'll still
need the classpath.

Signature

Beware the False Authority Syndrome



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.