jython

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
mit
User
Beiträge: 285
Registriert: Dienstag 16. September 2008, 10:00

Hallo,
habe versucht aus Java aus ein JAR Archiv welches mit Jython erzeugt wurde aufzurufen.

Code: Alles auswählen

//CallJython.java
import java.lang.reflect.Method;

public class CallJython {
	private static Method[] methods;

	public static void main(String[] args) {
		MyClass my = new MyClass();
		methods = my.getClass().getMethods(); 
		for(Method m: methods)
			System.out.println(m);
		my.method1();
	}
}

Code: Alles auswählen

#MyClass.py 
class MyClass:
	attr1 = 10 # class attributes
	attr2 = "hello"

	def method1(self):
		print MyClass.attr1 # reference the class attribute
	
	def method2(self, p1, p2):
		print MyClass.attr2 # reference the class attribute

	def method3(self, text):
		self.text = text # instance attribute
		print text, self.text # print my argument and my attribute
Zuerst habe ich MyClass.py in ein Jar gepackt mit

Code: Alles auswählen

jythonc -c -j myjar.jar MyClass.py
Anscliessend habe ich myjar.jar in Eclipse wie folgt eingebunden: Build Path -> Configure Build Path...-> Libraries -> Add External JARs...


Ohne my.method1() bekomme ich diese Ausgabe:

Code: Alles auswählen

public static void MyClass.moduleDictInit(org.python.core.PyObject)
public static void MyClass.main(java.lang.String[]) throws java.lang.Exception
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Mit my.method1() bekomme ich diese Fehlermeldung in Eclipse:

Code: Alles auswählen

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method method1() is undefined for the type MyClass

	at CallJython.main(CallJython.java:11)


Aber leider wird my.method1() nicht gefunden. Woran kann es liegen?

Viele Gruesse
mit
User
Beiträge: 285
Registriert: Dienstag 16. September 2008, 10:00

Loesung ( http://www.rexx.com/~dkuhlman/jython_co ... ng-jythonc ):
Man benoetigt
* import java.lang.Object
* MyClass muss von java.lang.Object erben: class MyClass(java.lang.Object):
* die gewuenschte Methoden Rumpf muss in Java typisch sein: """@sig public void method1()"""

Code: Alles auswählen

#MyClass.py
import java.lang.Object

class MyClass(java.lang.Object):
   attr1 = 10 # class attributes
   attr2 = "hello"

   def method1(self):
      """@sig public void method1()"""
      print MyClass.attr1 # reference the class attribute
   
   def method2(self, p1, p2):
      print MyClass.attr2 # reference the class attribute

   def method3(self, text):
      self.text = text # instance attribute
      print text, self.text # print my argument and my attribute

Antworten