0

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?

Community
  • 1
  • 1
Esben86
  • 483
  • 8
  • 21
  • 2
    Are you specifing the classpath? http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html – m0skit0 Jul 23 '16 at 17:55
  • *when I try to compile from cmd* -- Because you have to compile all the classes together. Using a build tool like Ant/Maven/Gradle really helps with that. – OneCricketeer Jul 23 '16 at 18:03
  • @m0skit0: I have set the classpath as instructed in the standard tutorial when installing JDE/JDK/Eclipse. – Esben86 Jul 23 '16 at 18:12
  • @cricket_007: Is it as simple as to just compile the Rational class before compiling the test/main class? I am still a beginner, so I haven't dived into any of the build tools that you mention yet. I only compiled the main/test class... – Esben86 Jul 23 '16 at 18:14
  • Thats one possibility. Or you need to `javac -cp ` – OneCricketeer Jul 23 '16 at 18:17
  • @Esben86 I'm talking about classpath on command line. – m0skit0 Jul 23 '16 at 18:20

2 Answers2

1

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

Amit Garg
  • 838
  • 1
  • 10
  • 21
  • I have a few other files called Rational.java, but in other packages. The Rational class that I am using with the testclass are both in the same package. I believe that the testclass should use the Rational class that is in the same package by default? – Esben86 Jul 23 '16 at 20:26
  • As I understand that you class has package statement also. – Amit Garg Jul 24 '16 at 03:35
  • As I understand that your class has package statement also. I think you are compiling your code using javac tool without switch. While your class has package making command then you have to use a '-d' switch to compile your class. Compile your java file using following command "javac -d E:\ TestRational.java Rational.java" or you can use "javac -d E:\ *.java" – Amit Garg Jul 24 '16 at 03:46
  • "javac -d E:\ TestRational.java Rational.java". In this command javac is java compiler command. -d is a switch for Specify where to place generated class files. E:\ is the location where I want to put my .class files. TestRational.java, Rational.java is my java class files which i want to compile. To run this file got to location 'E:\' and use java mypackagename.TestRational – Amit Garg Jul 24 '16 at 03:51
  • Is `Rational` in a package as well? – nitind Jul 24 '16 at 05:47
  • @nitind: Rational.java and TestRational.java are both in a package named "ch13". I checked my enviroment variables, and it seems like I have only set PATH. I assume that the CLASSPATH folder should be where all the class files are stored, which in my case is "D:\users\esben\java\workspace\*;" ? I have tried to set CLASSPATH to this directory, and also the C:\(java install directory)\jdk_xxxx\bin; and C:\(java install directory)\jre_xxxx\bin; , as shown in different tutorials. Nothing still works... – Esben86 Jul 24 '16 at 13:07
  • @Esben86: Have you tried compile your class file using "javac -d E:\ TestRational.java Rational.java"? – Amit Garg Jul 24 '16 at 15:12
  • @AmitGarg This did work actually :) If you edit your answer so it is more specific to the question, I will upwote and accept answer. Do I always have to use -d switch to avoid this problem, or is there any settings i can edit so I can compile the files from their standard directory without placing them somewhere else? – Esben86 Jul 24 '16 at 18:14
0

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

Panky031
  • 425
  • 1
  • 5
  • 14