1

I'm trying to compile the following code:

package week1;

public class ThreeSum {
    public static int count(int[] a) {
        // count triples that sum to 0
        int count = 0;
        for (int i = 0; i<a.length; i++) {
            for (int j = i+1; j < a.length; j++) {
                for (int k = j+1; k < a.length; k++) {
                    if (a[i] + a[j] + a[k] == 0) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] a = In.readInts(args[0]);
        StdOut.println(count(a));
    }
}

This code is in the ThreeSum.java file in week1 folder. Both classes "In" and "StdOut" are in the package stdlib.jar which is in the ./lib folder. I've always used an IDE and now decided to use a command line. So on my

javac -cp .:lib/stdlib.jar week1/ThreeSum.java

and other variants of classpath parameters it returns an error:

week1\ThreeSum.java:20: error: cannot find symbol
        int[] a = In.readInts(args[0]);
                  ^
  symbol:   variable In
  location: class ThreeSum

week1\ThreeSum.java:21: error: cannot find symbol
        StdOut.println(count(a));
        ^
  symbol:   variable StdOut
  location: class ThreeSum

How a proper -cp option should look like in my case?

4lex1v
  • 21,367
  • 6
  • 52
  • 86
  • use javac -cp .:./lib/stdlib.jar week1.ThreeSum.java and import Packages of In and StdOut – Patton Aug 14 '12 at 10:17
  • You need to specify the package name to `In` type. Something like - `com.abc.In` – KV Prajapati Aug 14 '12 at 10:17
  • @Patton there are no packages in stdlib.jar just java and class files in the root. As i understand i shouldn't import them. – 4lex1v Aug 14 '12 at 10:34
  • You do not need to import the classes if they have no package. The absence of a package name is a package name in it self. – Erik Aug 14 '12 at 11:05
  • What platform are you running on? If on windows, then the classpath elements needs to be separated with ';' – Erik Aug 14 '12 at 11:06
  • Classes in the default package are a very bad idea. You're learning Java. I'd recommend doing this simple thing on your own, without a library. – duffymo Aug 14 '12 at 11:56

5 Answers5

0

What are In and StdOut? There are no such classes in the JDK. You do have System.in and System.out. Please see the Java docs.

There's no readInts() method on System.in. Perhaps you mean some custom classes of your own.

Write it like this and forget about that JAR dependency:

package week1;

/**
 * ThreeSum
 * @author mduffy
 * @since 8/14/12 1:53 PM
 * @link http://stackoverflow.com/questions/11950145/cant-correctly-setup-classpath#comment15924351_11950145
 */
public class ThreeSum {
    public static int count(int[] a) {
        // count triples that sum to 0
        int count = 0;
        for (int i = 0; i<a.length; i++) {
            for (int j = i+1; j < a.length; j++) {
                for (int k = j+1; k < a.length; k++) {
                    if (a[i] + a[j] + a[k] == 0) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] values = new int[args.length];
        int valuesCount = 0;
        for (String arg : args) {
            int value = Integer.valueOf(arg);
            values[valuesCount++] = value;
        }
        System.out.println(String.format("count: %d", count(values)));
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • As he writes, they are from a provided JAR (hopefully ;). – Volker Stolz Aug 14 '12 at 10:18
  • Yes, but there are no imports for them, either. They'd have to be in the weeks package. – duffymo Aug 14 '12 at 10:19
  • @duffymo stdlib.jar is just a container for this classes, there are no packages in it. How should import look like in this case? This example is from Princeton algorithm book and there are no imports in the code either – 4lex1v Aug 14 '12 at 10:32
0

You does not define or import In and StdOut variables. You should do it because compiler does not know where and what they are.

gkuzmin
  • 2,414
  • 17
  • 24
0

Ok I believe you have directory structure as shown below

/src
    -- week1
          -- ThreeSum.java
/lib
    -- stdlib.jar

Where src contains class files.

$cd /src 
$javac -cp ".:../lib/stdlib.jar" week1.ThreeSum.java

I think this helps you!

PS: if you have some kind root directory instead of src then in such a case replace src with name of the root directory

Patton
  • 2,002
  • 3
  • 25
  • 36
  • Ok, thanks for our help, but the right way is to add week1 older to the -cp option =) And you have to use path way to the class file instead of package way, it's written in Oracles tutorial on classpath. – 4lex1v Aug 14 '12 at 11:40
0

Ok i compiled this with javac -cp .:lib/stdlib.jar:week1 week1/ThreeSum.java

so week1 folder should be added to the -cp option.

4lex1v
  • 21,367
  • 6
  • 52
  • 86
0

Actually I was facing same problem before,then I found this:Problem with stdlib.jar

As what the author of that passage said,the problem is because the classes inside the stdlib.jar file are not in package structure.So if you try to use them in your code,you should not organize your code in package structure.

I am using the eclipse IDE and I move my code outside my package(then the code will be placed in 'default package'), then it works.Of course the stdlib.jar should be added into the .classpath file.

tonychow
  • 36
  • 2