1

I am absolutely new to Java and having trouble referencing a class that I have declared in another .java file. I read a lot about it on the Oracle Website and also many stackoverflow questions, but couldn't solve the problem. Here are the steps I have taken:

Set the class path. I am running this on UNIX. So I typed in % java -classpath /users/myUserName/algs4/assignment1. Instead of changing the class path it gives me a long list of other options.

Include the 'package name' in each of my .java files which have class definitions I want to refer to.

I also ensured that all my files that I want to refer are in the same directory. Here is some of my code:

package assignment1;
public class Percolation
{
    private int[][] grid;
    ....
    //Other method definitions etc.
   public Percolation(int N)
   {
      uf = new WeightedQuickUnionUF(N);
      grid = new int[N][N];
      ...
      //Other code

   }

 }

My WeightedQuickUnionUF class is also defined as:

package assignment1;
public class WeightedQuickUnionUF {
    private int[] id;    // id[i] = parent of i
    private int[] sz;    // sz[i] = number of objects in subtree rooted at i
    private int count;   // number of components

    //Create an empty union find data structure with N isolated sets.
    public WeightedQuickUnionUF(int N) {
        count = N;
        id = new int[N];
        sz = new int[N];
        for (int i = 0; i < N; i++) {
            id[i] = i;
            sz[i] = 1;
        }
     }
}

But now when I compile using javac Percolation.java. It gives me the errors:

Percolation.java:7: cannot find symbol
symbol  : class WeightedQuickUnionUF
location: class assignment1.Percolation
WeightedQuickUnionUF uf;
^

I get it that it is because the compiler doesn't know what is the class WeightedQuickUnionUF. It cannot refer it. But how do I do it then? I have already tried the popular solutions.

I am not using any IDE. Just a text editor and then compiling it on the terminal.

(algs4 folder has files like stdlib.jar and others)

EDIT: I missed the 'new' keyword. I have added that. Also I am compiling my WeightedQuickUnionUF class before Percolation.java

3 Answers 3

2

The java command is not setting the classpath but trying to run a java class. That's why the long option list.

You can specify the classpath upon compilation: run javac -classpath . assignment1/Percolation.java from the algs4 directory. Pay attention to the dot it means current directory.

There is no need to compile the files in order.

To run your program: java -cp . assignment1.Percolation from the algs4 directory.

Sign up to request clarification or add additional context in comments.

3 Comments

Tested on my machine and it compiles and runs successfully.
Thanks works! Just one more question, why do I compile from the algs4 directory and not the assignment1?
you should compile from the algs: like in the above answer
1

You need to first compile the dependencies, ie, in your case the WeightedQuickUnionUF class.

And if you want an anonymous object, declare it along with the new keyword :

uf = new WeightedQuickUnionUF(N);

Or use it as mentioned in another answer.

Since that answer is deleted(I don't see it anymore), the other way around is to have a named object:

WeightedQuickUnionUF myObj; 

..

myObj = new WeightedQuickUnionUF(N);
uf = myObj;

1 Comment

I compiled the WeightedQuickUnionUF class first. And also I added the new keyword, but the same message. Percolation.java:6: cannot find symbol symbol : class WeightedQuickUnionUF
0

You must compile the classes in the

order of the flow of the Object Graph.

That means Percolation class CANNOT be compiled before the WeightedQuickUnionUF get compiled.

So first compile the WeightedQuickUnionUF then Percolation.

I think you have even missed out the new keyword...while initializing WeightedQuickUnionUF(N);

uf = new WeightedQuickUnionUF(N);

1 Comment

I compiled the WeightedQuickUnionUF class first. (no errors). Also I added the new keyword, but the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.