2013-08-27 23 views
0

我有一個類調用另一個類的方法。我在Ubuntu 13.04上編譯並運行它,運行良好。在OSX 10.8.4,我得到下面的輸出:螺紋OSX但不是Ubuntu的NoSuchMethodError

異常 「主要」 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"); 



    } 
} 
+0

你可以發佈代碼? – 2013-08-27 00:21:32

+0

我剛剛發佈了這兩個類。 –

+0

它會給你什麼錯誤?在你的部分堆棧跟蹤中,它給了我們一個行號,但是該行似乎是一條評論。 – mattbdean

回答

0

嘗試改變x.setImaginary(2); x.setReal(5);到x.setImaginary(2.0); x.setReal(5.0);.也許mac上的java版本需要double而不是int。

+0

這與Java版本無關。相信我。該代碼與自Java 1.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不正確。 (雖然...這是一個不同的問題)

+0

我該如何解決這個問題? –

+0

1)找出問題是不正確的構建過程還是不正確的運行時類路徑。 2)更正構建過程或類路徑。 3)檢查你沒有把你的類放入「認可的」目錄中......正如垃圾桶所建議的那樣。 –

+0

位於OSX上的java.ext.dirs位於何處? –