question--package name

X

Xiaoshen Li

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.
 
B

BartCr

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
 
Z

zero

Xiaoshen Li said:
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.

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.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top