1

I'm using Eclipse on a Windows 7 64x machine. I've researched this problem and found many have had a similar one, but no solution I came across quite worked for me.

I'm working on a Project named Assignment_1, on a class named Percolation. I'd like to use the object WeightedQuickUnionUF which is inside a package contained within a jar file, named algs4.jar.

I seem to have added the Jar file I'm interested in to the build-path (it now appears under "Referenced Libraries"). The jar file algs4.jar resides in a folder named lib inside my project's folder.

However, when I try to declare an object of type WeightedQuickUnionUF inside my class, I get an error "WeightedQuickUnionUF cannot be resolved to a type".

I tried various import commands (including just import WeightedQuickUnionUF )before the class declaration and all of them yield the error "The import so and so cannot be resolved".

For example, this piece of code yields both of these errors. One at the import line, and another at the declaration of the WeightedQuickUnionUF object:

package assignment_1_package;
import algs4.WeightedQuickUnionUF;

public class Percolation {

    private int[][] grid;
    public int gridDimension;
    private int opensGrid[][];
    private WeightedQuickUnionUF model;

... //rest of class body here

This has baffled me for an entire day and I can't seem to figure this out. Thanks for your efforts.

Edit: here is a link to the class I wish to import: http://algs4.cs.princeton.edu/15uf/WeightedQuickUnionUF.java.html

Adar Hefer
  • 1,514
  • 1
  • 12
  • 18

3 Answers3

3

Assuming you are talking about the algs4.jar of the class http://algs4.cs.princeton.edu/code/ , your import is incorrect you should do :

import WeightedQuickUnionUF;

BUT it's never a good idea to have class in the default package and it's actually not allowed to import a type from the unnamed package: this gives a compilation error. http://docs.oracle.com/javase/specs/jls/se5.0/html/packages.html#7.4.2:

A type-import-on-demand declaration (§7.5.2) imports all the accessible (§6.6) types of a named type or package as needed. It is a compile time error to import a type from the unnamed package.

So in your case to solve your issue just create your classes in the default package so you don't have to do the import at all.

Frederic Close
  • 9,389
  • 6
  • 56
  • 67
  • I tried that import and I get the same error. You're right, this is where I got the jar file. When I expand the jar file to see its classes, I first have to click through `(default package)`. Is it not considered a package? – Adar Hefer Oct 05 '13 at 16:03
  • You must have a problem with your Eclipse project setup. – Frederic Close Oct 05 '13 at 16:14
  • Is it anyway related to having a `main` class? I don't currently have one in my project. In any case, do you have any idea what could be wrong with my project setup? Thanks for the the input anyway. :) – Adar Hefer Oct 05 '13 at 16:16
  • indeed you are right, it's not allowed to import classes from the default package this gives a compilation error, just updated my answer – Frederic Close Oct 05 '13 at 16:36
  • I see. It's very strange that a University would provide a jar file that cannot export its classes. It's just that this piece from their site regarding using the Jars says: "How can I classpath in the textbook libraries from the command line? If you use our Mac OS X or Windows installer, then you can automatically classpath in the textbook libraries using the commands `javac-algs4` and `java-algs4` (instead of `javac` and `java`)." – Adar Hefer Oct 05 '13 at 16:45
  • It just seems like it should be possible somehow, because if the only way to include the classes is to manually copy them, there would be no need to add them to the "Referenced Libraries" section, which is what they refer me to here: http://coursera.cs.princeton.edu/algs4/checklists/percolation.html – Adar Hefer Oct 05 '13 at 16:46
  • 1
    just create your own class in the default package as well, and it should work ok – Frederic Close Oct 05 '13 at 16:48
0

I'm in the same class, had the same problem. Removing my equivalent to these two statements

 package assignment_1_package; 
    import algs4.WeightedQuickUnionUF;

resolved the problem. That's to say the following now resolves correcly

 private WeightedQuickUnionUF model;
Tom Koenig
  • 67
  • 4
0

In my case, it helped adding

import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.WeightedQuickUnionUF;
Tunaki
  • 132,869
  • 46
  • 340
  • 423
techkuz
  • 3,608
  • 5
  • 34
  • 62