java - OSX 中出现 NoSuchMethodError,但 Ubuntu 中没有

标签 java macos ubuntu

我有一个类调用另一个类的方法。我在Ubuntu 13.04上编译并运行它,运行良好。在 OSX 10.8.4 上,我得到以下输出:

线程“main”中出现异常 java.lang.NoSuchMethodError: Complex.equals(LComplex;)Z 在ComplexTester.main(ComplexTester.java:11)

我尝试在 Eclipse、Netbeans 和终端中运行它,并得到相同的输出。任何帮助将不胜感激。

复杂

public class Complex {

    private double real;
    private double imaginary;

    /**
     * 
     * @param real the real part of the Complex number
     * @param imaginary the imaginary part of the Complex number
     */
    public Complex(double newReal,double newImaginary){
        real=newReal;
        imaginary=newImaginary;
    }

    /**
     * 
     * @return the real part of the Complex number
     */
    public double getReal(){
        return real;
    }


    /**
     * 
     * @return the imaginary part of the complex number
     */
    public double getImaginary(){
        return imaginary;
    }

    /**
     * gives real a new value
     * @param newReal the new value of real
     */
    public void setReal(double newReal){
        real=newReal;
    }

    /**
     * gives imaginary a new value
     * @param newImaginary the new value of Imaginary
     */
    public void setImaginary(double newImaginary){
        imaginary=newImaginary;
    }


    /**
     * 
     * @param x the new Complex object whose instance variables must be added to the old one
     * @return a new Complex object that is a combination of the parameters of both Complex objects
     */
    public Complex add(Complex x){
        Complex result=new Complex(x.getReal()+real,x.getImaginary()+imaginary);
        return result;
    }


    /**
     * 
     * @param other the Complex object being compared
     * @return true if both Complex objects have the same imaginary and real parts
     */
    public boolean equals(Complex other){
        return other.getImaginary()==imaginary && other.getReal()==real;
    }


}

复杂测试仪

public class ComplexTester {

    public static void main(String[] args){
        Complex x=new Complex(23.2,33.1);
        Complex y=new Complex(23.2,33.1);

        //test the equals method
        System.out.println(x.equals(y));
        System.out.println("Expected: True");

        //test the setImaginary() and setReal() methods
        x.setImaginary(2);
        x.setReal(5);

        //test the getImaginary() and getReal() methods
        System.out.println(x.getImaginary());
        System.out.println("Expected: 2");

        System.out.println(x.getReal());
        System.out.println("Expected: 5");

        //test the equals method again
        System.out.println(x.equals(y));
        System.out.println("Expected: False");

        //test the add method
        Complex added=x.add(y);
        System.out.println(added.getReal());
        System.out.println("Expected: 28.2");

        System.out.println(added.getImaginary());
        System.out.println("Expected: 35.1");



    }
}

最佳答案

问题在于您构建和/或运行代码的方式。在 OSX 上,您不知何故使用旧的 Complex.class 文件运行,该文件与源代码不匹配,并且与 ComplexTest.class< 的内容不匹配 文件期望

  • 发生这种情况可能是因为您没有正确重建。

  • 发生这种情况可能是因为您的运行时类路径上有 Complex.class 的旧副本。

<小时/>

FWIW:

  1. “Complex.equals(LComplex;)Z”字符串表示 JVM 希望在 Complex 类中找到具有此签名的方法:

     boolean equals(Complex)
    

    此方法存在于Complex源代码中...

  2. 如果您打算重载 Object.equals(Object) 方法,则您的 equals 方法不正确。 (不过这是一个不同的问题......)

关于java - OSX 中出现 NoSuchMethodError,但 Ubuntu 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18455035/

相关文章:

java - Spring Data JPA 忽略空参数

java - 从中缀表达式转换为后缀表达式后表达式之间的正确间距

java - 我在哪里可以找到如何在不支持 https 的 java 上设置 https 的示例

mysql - 在 Mac 上将数据库导入 MAMP

macos - SourceTree filemerge 立即退出并创建 4 个文件。如何解决?

Java 运行时错误

macos - 更新后,vscode 调试器在 macOS 上不再工作

ubuntu - Nginx如何将不同的站点托管到同一个域但在不同的端口上

ubuntu - 安装本地版本的 postman-to-k6 会导致 npm 错误

ubuntu - Solr,服务不可用 503