jeffstern
jeffstern

Reputation: 5046

How to use classes in Referenced Libraries in Eclipse

I have imported the algs4.jar file as a referenced library as seen above. All seems to be fine but I can't seem to use or access any of the classes in the library.

Is there an import statement I'm missing?

Image reference:

Note how I'm trying to use BinarySearch from the referenced library but it is not offering me an option to use or import it.

Upvotes: 6

Views: 29556

Answers (6)

gprem062
gprem062

Reputation: 11

Just delete module_info.java file and the referenced libraries are accessible instantly

Upvotes: 0

devops
devops

Reputation: 9179

Firstly, BinarySearch can not be created (private Constructor). Use the static Method BinarySearch.rank(int key, int[] array)

Secondly you can access BinarySearch only from default package

Thirdly what about Arrays.binarySearch(array, key) ?

Upvotes: 0

Rex911
Rex911

Reputation: 19

If you want to import the classes, you need to download the packaged version of the library. See the Q&A section at the bottom of this webpage. http://introcs.cs.princeton.edu/java/stdlib/

Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not?

A. The libraries in stdlib.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use the packaged version stdlib-package.jar.

Upvotes: 1

Kristin Peterson
Kristin Peterson

Reputation: 341

I am taking an Algorithms class via Coursera that utilizes the algs4.jar reference library.

The Problem:

The issue you are having is that the reference library is located in the default package and the source in which you are accessing the reference library is not.

The Solution

All classes in your project need to be in the default package, otherwise the reference libraries (which are located in the default package) will not be recognized. Your project classes need to be in the src directory and you should not declare a package at the beginning of your .java files. Additionally you also need to make sure you've added the reference libraries to your build path.

Upvotes: 7

MrHopko
MrHopko

Reputation: 899

Note that the classes in the jar files are all contained in the default package.

If your classes are then contained in a package then they will not be directly accessible.

I submit this answer because I am taking the algorithms course to which these jar files belong and this was the specific cause of my program not working.

Upvotes: 2

devang
devang

Reputation: 5516

I am not sure, I did not try this. Some interpretations though.

Is this a standard library or someone created it by himself and gave you?

The problem is class BinarySearch is in default package. You will not be able to import it. If this library is created by you, or by any of your friend, you need to ask your friend to move all classes from default package to a good namespace and then re-create the jar.

If class is accessible, ctr + shift + o should import the class.

Upvotes: 6

Related Questions