Question on "import" directive and directory structure (also OT questionon eclipse)

E

exquisitus

Hi

I am a C/C++ developer who has recently jumped onto the Java band wagon.
I have downloaded eclipse and created a simple Java library package
(nothing fancy) in one project. Incidentally all the classes and methods
are declared as public in the library.

I have created a separate test harness project, which I intend to use to
test the functions I have written in my library/package.

I need to find out how to do the ff:

1). Build Jar files from my first project (I can't seem to find
documentation on building Jars in eclipse anywhere)

2). Correctly import the library package in my second project. (There is
a directory structure to "import" directive mapping which I haven't
worked out yet)

My libraries (currently .class files) are in: root\package
and my second project is in folder: root\project2

How would I write my "import" declaration to be able to import the jar
files once I have succesfully built them?


Many tks in advance
 
S

Scotchy

To build jars right click on the Project and choose Export. In the popup
dialog you will see create jar.

To import the project you can export the jar in step 1 into your project or
you can choose project properties and in the build tab choose the project
which contains the project you wish to use in this project.

Your import will contain a fully qualified namespace such as com.acme.utils;
or com.acme.*;

Hope this helps
Scotchy
 
S

Symon

Write it as usual (you import classes in packages, not files or jars),
but don't forget to put your jar in your classpath at compile time and
at runtime (using environment variable or the -cp option).

In eclipse, you can set the classpath in the project properties.

Symon
 
E

exquisitus

Symon said:
Write it as usual (you import classes in packages, not files or jars),
but don't forget to put your jar in your classpath at compile time and
at runtime (using environment variable or the -cp option).

In eclipse, you can set the classpath in the project properties.

Symon

Tks Symon for the reply,

I relaized after I posted that I had not made myself very clear. The
problem is that I am not sure of the correct way to write "import"
declarations.

For example, if I write the ff:

import mypackage.someclass

There is an implicit file directory structure associated with the
"import" declaration. For example, in C/C++, when you include a header
file in a source file, you can explicitly specify where the dickens the
header files are located.

My question, properly stated then, is: How does one specify the location
of packages (without having to resort to the CLASSPATH env var - surely
there must be a better way ?)

Thanks
 
S

Scotchy

To adjust build path so that your project will build check the appropriate
Projects within the properties of the project. Right click on the project
folder to get the menu and choose properties. Choose Java Build Path and
then choose Projects Tab. Check the appropriate projects.

When exporting your jar just export to your classpath default. For example
you may include them in your java1.4.2/jre/lib folder.

when you write your import statements the import keyword will look at the
buildpath to resolve the name of the jar or package.

Hope this helps
Scotchy
 
E

exquisitus

Scotchy said:
To adjust build path so that your project will build check the appropriate
Projects within the properties of the project. Right click on the project
folder to get the menu and choose properties. Choose Java Build Path and
then choose Projects Tab. Check the appropriate projects.

When exporting your jar just export to your classpath default. For example
you may include them in your java1.4.2/jre/lib folder.

when you write your import statements the import keyword will look at the
buildpath to resolve the name of the jar or package.

Hope this helps
Scotchy

Hi Scotchy,

Apologies if I'm being slow but this is all new stuff to me. I have done
as you suggested, including dumping my .jar file in (where I think the
Jre is installed, I checked my CLASSPATH env var, but it was referring
to the binaries of some other Java apps I have on my machine. I looked
in "C:\Program Files\Java and so 2 folders j2re1.4.2_06 and
j2sdk.4.2_06. I opted for the former - i.e. j2re folder and plonked my
jar file in the lib folder. I then went back to eclipse, edited the
project properties as ff:

1). Selected the Libraries Tab
2). Added External JArs (my jar file)

My Jar appeared in the Package Explorer with the correct name etc - but
I still could not use the import keyword - this is really driving me
round the bend

BTW, I did follow your instructions previously - i.e. chosing the
projects tab and selecting the required project. That did not work....
Hmmm.... Please help. I don't think it should be this difficult. If I am
being thick, I don't mind you pointing that out. Just please help me to
get this poxy project to build.

Thanks
 
S

Scotchy

Shamelessly I thought you were in linux. It still is however the same.
Does your icon on the import statement have a lightbulb with and x on it?
Does it say the import cannot be resolved? Heres one are your files in a
package or on the default folder? I have found that if I include things in
the default trying to resolve their package tends to be a nightmare. Just
create a new package call it something like com.acme and drag your java
files into it. Then the checkmark in the build page should work. You will
have to resave your jar if you have not done so as the package changed.

Hope this helps
Scochy
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top