java call jython

Status
Nicht offen für weitere Antworten.

flashdog

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

Code:
//CallJython.java
public class CallJython {
	public static void main(String[] args) {
		MyClass my = new MyClass();
		my.method1();
	}
}

Code:
#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:
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...

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

Viele Gruesse
 
Wird die MyClass denn gefunden? Vielleicht fehlt dir im Jythoncode noch ein public-Modifier an der method1.
 
kannst dus denn auf konsole kompilieren?
wie lautet denn die genaue fehlermledung?

method1 evtl erst public machen?
 
Myclass wird gefunden. Wenn ich mich nicht taeusche sind alle drei Methoden public oder meinst mit public-Modifier etwas anderes.
 
Genau das habe ich mit Public-Modifier gemeint.

Hm, versuch doch mal mit Reflection die Klasse zu untersuchen, dann siehste welche Methoden es gibt. Oder du decompilierst dir Klasse mit Jad und guckst dir den Javacode an.

Code:
Method[] methods = my.getClass().getMethods();
...

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getMethods()
 
Code:
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();
	}
}

Ohne my.method1() bekomme ich diese Ausgabe:
Code:
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:
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)
 
Ok, also deine in jython deklarierten Methoden sind auf jeden Fall nicht vorhanden.
Ich kenne mich mit jython nicht aus, aber vielleicht haste es nicht korrekt compiliert. Kannste denn auf die Methoden zugreifen, wenn du das jython-script in Java einbindest?
 
Loesung ( http://www.rexx.com/~dkuhlman/jython_course_03.html#calling-jython-from-java-using-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:
#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
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben