简体   繁体   English

从cmd编译Java程序时,出现“错误:找不到符号”,但是在Eclipse中却没有

[英]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. 为此,我有一个Rational类。 The program should be executed from commandline with args: 该程序应从命令行使用args执行:

java (...) num/denom operator(+-./) num/denom java(...)num / denom运算符(+-。/)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. 在创建Rational类的实例时,这似乎是一个编译错误,当我尝试从cmd进行编译时就会发生这种情况。 I don't get this error when compiling in Eclipse. 在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. 目前,使用计算器逻辑的main方法有点混乱,因此我将粘贴一个测试类的示例,在该示例中创建一些Rational实例。 I will also paste the code for Rational . 我还将粘贴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: Rational类:

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". 对于有理单词的每次出现,我都会收到一条错误消息,其中“ ^”指向“ 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? 谁能看到导致错误的原因,为什么仅在为Commandlinde编译程序时才导致错误?

Generally this error comes when compiler is not able to find other java file which you using in your program. 通常,当编译器无法找到您在程序中使用的其他Java文件时,就会出现此错误。

One possible reason can be this, you have save your file with any another name in place of Rational.java. 一个可能的原因是,您已经用其他任何名称代替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. 其次,当您在类顶部使用package语句并编译类时不使用“ -d”开关。

Solution: compile your java file using "javac -d E:\\ TestRational.java Rational.java" 解决方案:使用“ javac -d E:\\ TestRational.java Rational.java”编译Java文件。

What "-d" does “ -d”的作用

Link : http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html 链接: http : //docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
-d directory Set the destination directory for class files. -d目录设置类文件的目标目录。 The directory must already exist; 该目录必须已经存在; javac will not create it. javac不会创建它。 If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. 如果类是包的一部分,则javac会将类文件放在反映包名称的子目录中,并根据需要创建目录。 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. 例如,如果指定-d C:\\ myclasses且该类称为com.mypackage.MyClass,则该类文件称为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. 如果未指定-d,则javac会将每个类文件与从其生成源文件的目录放在同一目录中。

Note: The directory specified by -d is not automatically added to your user class path. 注意:-d指定的目录不会自动添加到您的用户类路径中。

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 虽然我们使用不带-d开关的javac命令来编译Java文件,但是编译器会创建一个临时包来编译您的类,将类文件保存在当前目录中并删除该包

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. 当您编译TestRational.java文件时,编译器会创建一个临时包并尝试在该包中查找Rational.java,而当编译器无法找到该类时,编译会显示此错误。

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. 当您使用eclipse时,IDE eclipse使用诸如ant / maven之类的构建工具来处理所有这些事情,因此您不会在这里遇到这种错误。

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 如果Rational.java在不同的软件包中,则使用-d然后使用以下命令将其编译为TestRational.java javac -cp locationOfRationalClass -d locationOfNewPackage TestRational.java

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 成功编译/tmp/java_Rgjz2b/TestRational.java <-主方法成功编译/tmp/java_Rgjz2b/Rational.java O / p:r1 + r2 = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM