2

This might be a very simple problem indeed, but I am absolutely new to Java, and not getting how to resolve it.

I have a stdlib.jar file which has some Classes defined like StdRandom and I have to to use them in my program. I read, that to use files like stdlib.jar I have to use the default package. So I removed the "package algs4" from my program below. But it didn't work.

package algs4;
public class PercolationStats
{
   //Some method declarations
   public static void main(String[] args)
   {
      //Some code
      for(int counter=0; counter<T; counter++)
      {
        while(!p.percolates())
        {
            iIndex = StdRandom.uniform(N);
            jIndex = StdRandom.uniform(N);
            ...
        }
      }



} 

Every time I compile this program using:

javac -cp . algs4/PercolationStats.java

I get the error:

algs4/PercolationStats.java:30: cannot find symbol
symbol  : variable StdRandom
location: class algs4.PercolationStats
                iIndex = StdRandom.uniform(N);

I also tried to compile using:

javac -cp .:stdlib.jar algs4/PercolationStats.java

But the error remains same.

Though it is not a good practice, I also extracted the files from the .jar file, which gave me the StdRandom.java file, but it still doesn't work.

All my files including the stdlib.jar are in the same directory.

How should I go about it?

EDIT: Just to make the question more elaborate for any future references: stdlib.jar used in the program was in a "default" package.

mvb
  • 701
  • 1
  • 11
  • 20

2 Answers2

6

EDIT: If StdRandom isn't in a package, then you may indeed need to take your code out of a package too. (I'll give it a try - it's a long time since I've had to work with a class which wasn't in a package.)

In general, it would be much better to get a version of stdlib.jar which had the classes in packages.


We don't know what package StdRandom is in. You probably just want an import statement:

import foo.bar.StdRandom;

... where foo.bar is the package containing StdRandom. Alternatively, you can import all the classes in a package with a wildcard:

import foo.bar.*;

See the "Creating and using packages" part of the Java tutorial for more details.

You'll definitely need to have stdlib.jar in your classpath though, so stick to your second compilation approach.

You might also want to consider using an IDE (e.g. Eclipse) which can make all of this easier for you, and even work out import statements on demand.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • StdRandom is part of the stdlib.jar so I am unable to figure out what package it would be. – mvb Aug 29 '12 at 06:14
  • @mvb: Do you not have any documentation for it at all? Just using `jar tvf stdlib.jar` should show you the directory structure, and you can infer the package name from that. Fundamentally though, you should try to avoid using code you have *no* knowledge of, to the extent that you don't even know its full name. – Jon Skeet Aug 29 '12 at 06:17
  • Sorry about a lot of confusion. Actually I have got this file from our instructor. And the file comes with a warning: "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." That's leaving me all more confused. But I'll try your solution and work out. – mvb Aug 29 '12 at 06:23
  • @mvb: Ick - okay, that does make sense. You'll need to take out your package declaration - at that point, your second compilation command really should work. But please talk to your instructor and *strongly urge* him to follow standard Java good practice, and put the classes in a package. – Jon Skeet Aug 29 '12 at 06:25
  • Since they use the auto corrector to check the code, they say use the default package version. (Here is the link as well: http://introcs.cs.princeton.edu/java/stdlib/) – mvb Aug 29 '12 at 06:31
  • @Jon Skeet The library used by mvb is found here http://introcs.cs.princeton.edu/java/stdlib/ , and yes it doesn't use package declarations – MaVRoSCy Aug 29 '12 at 06:32
  • @JonSkeet: Thanks, finally got it to work. Removed the code from the package. And compiled it from the directory where all the files were present. – mvb Aug 29 '12 at 06:48
0

try compiling with

java -classpath "lib/*:." my.package.Program

or

java -cp "Test.jar:lib/*" my.package.MainClass

see Setting multiple jars in java classpath for more info

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125