Esben86
Esben86

Reputation: 493

When compiling a java program from cmd i get "error: cannot find symbol", but not in Eclipse

I am trying to make a calculator to preform arithmetic operations on rational numbers. For this i have a Rational class. The program should be executed from commandline with args:

java (...) num/denom operator(+-./) num/denom

It seems like a get a compilation error when creating instances of the Rational class, and this happens when I try to compile from cmd. I don't get this error when compiling in Eclipse. The main method with the calculator logic is a bit messy at the moment, so I will paste an example from a test class where I am creating some instances of Rational. I will also paste the code for Rational.

Test method below:

public class TestRational {

    public static void main(String[] args) {

        Rational r1 = new Rational(1, 2);
        Rational r2 = new Rational(1, 2);
        Rational result = new Rational();

        result = r1.add(r2);

        System.out.println("r1 + r2 = " + result);

    }

}

The Rational class:

public class Rational extends Number implements Comparable<Rational> {

    private long numerator = 0;
    private long denominator = 1;

    private long[] r = new long[2];
    // numerator: r[0]
    // denominator: r[1]

    public Rational() {
        this(0, 1);
    }

    public Rational(long numerator, long denominator) {
        long gcd = gcd(numerator, denominator);
        this.r[0] = ((denominator > 0) ? 1 : -1) * numerator / gcd;
        this.r[1] = Math.abs(denominator) / gcd;
    }

    private static long gcd(long n, long d) {
        long n1 = Math.abs(n);
        long n2 = Math.abs(d);
        int gcd = 1;

        for (int k = 1; k <= n1 && k <= n2; k++) {
            if (n1 % k == 0 && n2 % k == 0)
                gcd = k;
        }
        return gcd;
    }

    public long getNumerator() {
        return r[0];
    }

    public long getDenominator() {
        return r[1];
    }

    public Rational add(Rational secondRational) {
        long n = r[0] * secondRational.getDenominator()
                + r[1] * secondRational.getNumerator();
        long d = r[1] * secondRational.getDenominator();
        return new Rational(n, d);
    }

    public Rational subtract(Rational secondRational) {
        long n = r[0] * secondRational.getDenominator()
                - r[1] * secondRational.getNumerator();
        long d = r[1] * secondRational.getDenominator();
        return new Rational(n, d);
    }

    public Rational multiply(Rational secondRational) {
        long n = r[0] * secondRational.getNumerator();      
        long d = r[1] * secondRational.getDenominator();
        return new Rational(n, d);
    }

    public Rational divide(Rational secondRational) {
        long n = r[0] * secondRational.getDenominator();    
        long d = r[1] * secondRational.getNumerator();
        return new Rational(n, d);
    }

    @Override
    public String toString() {
        if (r[1] == 1)
            return r[0] + "";
        else
            return r[0] + "/" + r[1];
    }

    @Override
    public boolean equals(Object other) {
        return (((this.subtract((Rational)(other))).getNumerator() == 0));
    }

    @Override
    public int intValue() {
        return (int)doubleValue();
    }

    @Override
    public float floatValue() {
        return (float)doubleValue();
    }

    @Override
    public double doubleValue() {
        return r[0] * 1.0 / r[1];
    }

    @Override 
    public long longValue() {
        return (long)doubleValue();
    }

    @Override
    public int compareTo(Rational o) {
        if (this.subtract(o).getNumerator() > 0)
            return 1;
        else if (this.subtract(o).getNumerator() < 0)
            return -1;
        else
            return 0;
    }

}

The error message looks like this:

TestRational.java:7: error: cannot find symbol
Rational r1 = new Rational(1, 2)
^
symbol: class Rational
location: class TestRational

I get one error message for each occurence of the Rational word, with a "^" pointing up against the "R".

I have read this post, but have not been able to solve the problem: link

Can anyone see what is causing the error, and why is it only caused when compiling the program for the commandlinde?

Upvotes: 0

Views: 2992

Answers (2)

Amit Garg
Amit Garg

Reputation: 1218

Generally this error comes when compiler is not able to find other java file which you using in your program.

One possible reason can be this, you have save your file with any another name in place of Rational.java. Compiler will give this error when same class name not found. Solution: Change the class name and recompile.

Second when you using package statement at top of your class and complie your class without "-d" Switch.

Solution: compile your java file using "javac -d E:\ TestRational.java Rational.java"

What "-d" does

Link : http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
-d directory Set the destination directory for class files. The directory must already exist; javac will not create it. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d C:\myclasses and the class is called com.mypackage.MyClass, then the class file is called C:\myclasses\com\mypackage\MyClass.class. If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.

Note: The directory specified by -d is not automatically added to your user class path.

While we compile our java file using javac command without '-d' switch, compiler create a temporary package compile your class, save class file in your current directory and delete the package

What happen in your case:

When you compile your TestRational.java file then compiler create a temporary package and try to find out Rational.java in that package and when compiler not able to find that class then compile shows this error.

when you use eclipse IDE eclipse take all these things using build tool like ant/maven, so you do not get this kind of error there.

If Rational.java is in different package then compile this using -d and then TestRational.java using following command javac -cp locationOfRationalClass -d locationOfNewPackage TestRational.java

Upvotes: 1

Panky031
Panky031

Reputation: 443

I am able to execute the code with command prompt, please check your class name once. Successfully compiled /tmp/java_Rgjz2b/TestRational.java <-- main method Successfully compiled /tmp/java_Rgjz2b/Rational.java O/p: r1 + r2 = 1

Upvotes: 0

Related Questions