2012-12-26 92 views
3

我在java中編寫了一個類,我想用python執行jython。 首先我得到的錯誤?使用jython從python訪問java類

Traceback (most recent call last): 
    File "/Users/wolverine/Desktop/project/jython_scripts/customer.py", line 3, in <module> 
    customer = Customer(1234,"wolf") 
TypeError: No visible constructors for class (Customer) 

我的Java類格式:

public class Customer { 
    public final int customerId; 
    public final String name; 
    public double balance; 

    /* 
    * Constructor 
    */ 
    Customer(int _customerId, String _name){ 
     customerId = _customerId; 
     name = _name; 
     balance = 0; 
    } 

我的Python 2行腳本

import Customer 

customer = Customer(1234,"wolf") 
print customer.getName() 

的目錄結構就像

folder/customer.py folder/Customer.java folder/Customer.jar 

我去了文件夾,並做了

%javac -classpath Customer.jar *.java 

然後我的Jython是在用戶/狼獾/ Jython的/ Jython的

要執行我做這

 %/Users/wolverin/jython/jython ~/Desktop/folder/customer.py 

又一次的錯誤是:

Traceback (most recent call last): 
    File "/Users/wolverine/Desktop/project/jython_scripts/customer.py", line 3, in <module> 
    customer = Customer(1234,"wolf") 
TypeError: No visible constructors for class (Customer) 

免責聲明。我剛開始使用java :(

回答

2

Customer類不在你的包中,並且你的構造函數不公開,這就是爲什麼你會看到你看到的錯誤 - 構造函數對你的python代碼不可見(這是另一個軟件包中有效)

Customer(int _customerId, String _name){ 

更改構造線

public Customer(int _customerId, String _name){ 

,它應該工作的罰款。另外,你可能找到this question有助於瞭解public/protected/private/default如何工作。

+0

工程就像一個魅力。謝謝 – Fraz