OSX中的NoSuchMethodError,但不是Ubuntu

时间:2013-08-27 00:12:57

标签: 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;
    }


}

ComplexTester

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");



    }
}

2 个答案:

答案 0 :(得分:2)

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

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

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


FWIW:

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

     boolean equals(Complex)
    

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

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

答案 1 :(得分:0)

尝试更改x.setImaginary(2); x.setReal(5);到x.setImaginary(2.0); x.setReal(5.0);.也许mac上的java版本需要double而不是int。

相关问题