|
From: Robert W. <wi...@gm...> - 2006-08-07 09:53:46
|
Sorry for posting twice but my fist mail wasn't detailed enough.
I want to use a Jython class in a Java class, so here is the source code
of the classes. The classes a not really useful they are only for
testing and get familiar with Jython.
=========== PyClass.py ==============
class PyClass:
def echo(self, msg):
"@sig public java.lang.String echo(java.lang.String msg)"
ret = "selber: " + msg
return ret
========= JyUser.java =================
public class JyUser {
public static void main(String[] args) {
PyClass py = new PyClass();
System.out.println(py.echo("echo"));
}
}
The command jythonc PyClass.py produces the following error message:
=========================================================
Compiling .java to .class...
Compiling with args:
['C:\\Programme\\Java\\jdk1.5.0_01\\bin\\javac.exe', '-classpath',
'D:\\jython-2.1\\jython.jar;;.\\jpywork;;D:\\jython-2.1\\Tools\\jythonc;C:\\Dokumente
und
Einstellungen\\Robert\\Desktop\\jyc\\.;D:\\jython-2.1\\Lib;D:\\jython-2.1',
'.\\jpywork\\PyClass.java']
1 D:\jython-2.1\org\python\core\Py.java:989: as of release 1.4,
'assert' is a keyword, and may not be used as an identifier
(try -source 1.3 or lower to use 'assert' as an identifier)
public static void assert(PyObject test, PyObject message) {
^
D:\jython-2.1\org\python\core\Py.java:995: as of release 1.4, 'assert'
is a keyword, and may not be used as an identifier
(try -source 1.3 or lower to use 'assert' as an identifier)
public static void assert(PyObject test) {
^
D:\jython-2.1\org\python\core\Py.java:996: ')' expected
assert(test, Py.None);
^
D:\jython-2.1\org\python\parser\PythonGrammar.java:6739: as of release
1.5, 'enum' is a keyword, and may not be used as an identifier
(try -source 1.4 or lower to use 'enum' as an identifier)
for (java.util.Enumeration enum = jj_expentries.elements();
enum.hasMoreElements();) {
^
D:\jython-2.1\org\python\parser\PythonGrammar.java:6739: as of release
1.5, 'enum' is a keyword, and may not be used as an identifier
(try -source 1.4 or lower to use 'enum' as an identifier)
for (java.util.Enumeration enum = jj_expentries.elements();
enum.hasMoreElements();) {
^
D:\jython-2.1\org\python\parser\PythonGrammar.java:6740: as of release
1.5, 'enum' is a keyword, and may not be used as an identifier
(try -source 1.4 or lower to use 'enum' as an identifier)
int[] oldentry = (int[])(enum.nextElement());
^
D:\jython-2.1\org\python\core\Py.java:996: incompatible types
found : org.python.core.PyObject
required: boolean
assert(test, Py.None);
^
D:\jython-2.1\org\python\core\PyBeanProperty.java:36: warning:
non-varargs call of varargs method with inexact argument type for last
parameter;
cast to java.lang.Object for a varargs call
cast to java.lang.Object[] for a non-varargs call and to suppress this
warning
Object value = getMethod.invoke(iself, Py.EmptyObjects);
^
=========================================================
Question 1:
I get it compiled with the command jythonc -J "-source 1.3" PyClass.py but I still get warnings. The 1.3 option is necessary because jythonc generates methodes named assert, which is a keyword since java 1.4 an I'm using jdk 1.5. So I wander if I made something wrong or Jython is only compatible with Java 1.3???
If I try to compile the java class file I get the following error message.
=========================================================
JyUser.java:4: cannot find symbol
symbol : method echo(java.lang.String)
location: class PyClass
System.out.println(py.echo("echo"));
^
1 error
=========================================================
Note that I already fixed the problem of finding the class (only a wrong classpath) but now I can't access the method of the Jython class also I used the @sig-docstring.
Question 2:
How to use Jython classes/methods from Java?
A general question on using this mailing list:
How to answer on mails? Replay to the sender or post a new message to the mailing list?
thanks
robert
|
|
From: Josh J. <j.j...@mc...> - 2006-08-08 03:10:32
|
Robert- Sorry for the delay in response. The first issue I see with the Jython code is that you do not subclass a Java class. If you are not using anything which requires subclassing, just subclass java.lang.Object as such: from java.lang import Object class PyClass(Object): ... As far as the error goes, I haven't ever seen any issues using 1.5 in my own environment. I can bet you that the assertion error has something to do with the subclassing...but don't quote me on that. There are several good articles on this topic. Check this one out which I wrote last month: http://wiki.python.org/jython/JythonMonthly/Articles/July2006/1 , it may help you out. For the posting question, I think you are best to post to everyone...that way we all learn from the discussion. Best to you- Josh ----- Original Message ----- From: "Robert Wierschke" <wi...@gm...> To: <jyt...@li...> Sent: Monday, August 07, 2006 4:54 AM Subject: [Jython-users] how to: compiling jython class to java byte code(detailed) > Sorry for posting twice but my fist mail wasn't detailed enough. > > I want to use a Jython class in a Java class, so here is the source code > of the classes. The classes a not really useful they are only for > testing and get familiar with Jython. > > =========== PyClass.py ============== > class PyClass: > > def echo(self, msg): > "@sig public java.lang.String echo(java.lang.String msg)" > > ret = "selber: " + msg > return ret > > > ========= JyUser.java ================= > > public class JyUser { > public static void main(String[] args) { > PyClass py = new PyClass(); > System.out.println(py.echo("echo")); > } > } > > > The command jythonc PyClass.py produces the following error message: > > ========================================================= > Compiling .java to .class... > Compiling with args: > ['C:\\Programme\\Java\\jdk1.5.0_01\\bin\\javac.exe', '-classpath', > 'D:\\jython-2.1\\jython.jar;;.\\jpywork;;D:\\jython-2.1\\Tools\\jythonc;C:\\Dokumente > und > Einstellungen\\Robert\\Desktop\\jyc\\.;D:\\jython-2.1\\Lib;D:\\jython-2.1', > '.\\jpywork\\PyClass.java'] > 1 D:\jython-2.1\org\python\core\Py.java:989: as of release 1.4, > 'assert' is a keyword, and may not be used as an identifier > (try -source 1.3 or lower to use 'assert' as an identifier) > public static void assert(PyObject test, PyObject message) { > ^ > > D:\jython-2.1\org\python\core\Py.java:995: as of release 1.4, 'assert' > is a keyword, and may not be used as an identifier > (try -source 1.3 or lower to use 'assert' as an identifier) > public static void assert(PyObject test) { > ^ > > D:\jython-2.1\org\python\core\Py.java:996: ')' expected > assert(test, Py.None); > ^ > > D:\jython-2.1\org\python\parser\PythonGrammar.java:6739: as of release > 1.5, 'enum' is a keyword, and may not be used as an identifier > (try -source 1.4 or lower to use 'enum' as an identifier) > for (java.util.Enumeration enum = jj_expentries.elements(); > enum.hasMoreElements();) { > ^ > > D:\jython-2.1\org\python\parser\PythonGrammar.java:6739: as of release > 1.5, 'enum' is a keyword, and may not be used as an identifier > (try -source 1.4 or lower to use 'enum' as an identifier) > for (java.util.Enumeration enum = jj_expentries.elements(); > enum.hasMoreElements();) { > ^ > > D:\jython-2.1\org\python\parser\PythonGrammar.java:6740: as of release > 1.5, 'enum' is a keyword, and may not be used as an identifier > (try -source 1.4 or lower to use 'enum' as an identifier) > int[] oldentry = (int[])(enum.nextElement()); > ^ > > D:\jython-2.1\org\python\core\Py.java:996: incompatible types > found : org.python.core.PyObject > required: boolean > assert(test, Py.None); > ^ > > D:\jython-2.1\org\python\core\PyBeanProperty.java:36: warning: > non-varargs call of varargs method with inexact argument type for last > parameter; > cast to java.lang.Object for a varargs call > cast to java.lang.Object[] for a non-varargs call and to suppress this > warning > Object value = getMethod.invoke(iself, Py.EmptyObjects); > ^ > ========================================================= > > Question 1: > I get it compiled with the command jythonc -J "-source 1.3" PyClass.py but > I still get warnings. The 1.3 option is necessary because jythonc > generates methodes named assert, which is a keyword since java 1.4 an I'm > using jdk 1.5. So I wander if I made something wrong or Jython is only > compatible with Java 1.3??? > > If I try to compile the java class file I get the following error message. > > ========================================================= > > JyUser.java:4: cannot find symbol > symbol : method echo(java.lang.String) > location: class PyClass > System.out.println(py.echo("echo")); > ^ > 1 error > ========================================================= > > > Note that I already fixed the problem of finding the class (only a wrong > classpath) but now I can't access the method of the Jython class also I > used the @sig-docstring. > > Question 2: > How to use Jython classes/methods from Java? > > > A general question on using this mailing list: > How to answer on mails? Replay to the sender or post a new message to the > mailing list? > > thanks > robert > > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share > your > opinions on IT & business topics through brief surveys -- and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users |
|
From: Robert W. <wi...@gm...> - 2006-08-08 10:29:58
|
> The first issue I see with the Jython code is that you do not subclass > a Java class. If you are not using anything which requires > subclassing, just subclass java.lang.Object as such: > > from java.lang import Object > class PyClass(Object): > ... Ok. Now I can compile my java class (the compiler finds the class and the method). That makes a lot of sense; everything in Java must subclass java.lang.Object. Someone should write a script to at Object as superclass for all classes so we don't need to subclass explicitly, like it is in Java. > As far as the error goes, I haven't ever seen any issues using 1.5 in > my own environment. I can bet you that the assertion error has > something to do with the subclassing...but don't quote me on that. Very curious! I still need the "source 1.3" Option and i still get warnings with this option. I tried jython_2.2a (the readme.txt says "support for running on jdk1.5") and it compiles without "source"-option and without any warning!? So jython 2.1 is just not compatible with jdk1.5, because it generates method with names that are keyword (assert, enum) in newer java version. But why doesn't anybody but me have problems with this ??? Unfortunately it still doesn't work! :( > There are several good articles on this topic. Check this one out > which I wrote last month: > http://wiki.python.org/jython/JythonMonthly/Articles/July2006/1 , it > may help you out. I tried the first example in this article (Foo and FooTest) and I have the same problem as with my own example, I'm getting a NullPointerException from the constructor of the jython class: Exception in thread "main" java.lang.NullPointerException at org.python.core.PyJavaClass.lookup(PyJavaClass.java:43) at org.python.core.PyObject.<init>(PyObject.java:46) at org.python.core.PySingleton.<init>(PySingleton.java:8) at org.python.core.PyNone.<init>(PyNone.java:10) at org.python.core.PySystemState.initStaticFields(PySystemState.java:396) at org.python.core.PySystemState.initialize(PySystemState.java:375) at org.python.core.Py.initProperties(Py.java:696) at org.python.core.Py.initProxy(Py.java:738) at Foo.__initProxy__(Foo.java:186) at Foo.<init>(Foo.java:164) at FooTest.main(FooTest.java:3) so what's wrong now??? thanks for your help robert |