简体   繁体   中英

Converting Jython code into a Java Class

Goal: allow end users to write jython scripts that will create AI objects [for controlling fleets of spaceships in a game] in Java.

I want the user to be able to write all the logic for the AI in a .py file then convert that code into a java object. I would like to do this without using Java reflection and without running the user's code through an interpreter every time it is needed. The whole point of this is to make the code run as fast as possible, because the AI logic will have to run every game loop.

I've seen Jython's __toJava__ method, but I don't really understand how to use it or if it is what I need.

I would greatly appreciate any insight into a good solution to my problem, or knowledge about __toJava__ . I am committed to both Java and Jython, so suggesting alternatives is not productive :)

'jythonc' is no longer supported. You have to compile using jython:

jython

>>> import compileall
>>> compileall.compile_dir('directory/', force=True)

for a single file:

 >>> import py_compile
 >>> py_compile.compile('Building.py')

You should have a look at these ways to use jython code from a java application.

You should write you code in jython and use it, as it is, in your Java application with a little effort.

Jythonc is deprecated, but here is the old version with jythonc anyway. Compile with something like this:

jythonc --core --jar foo.jar foo.py

Another (untested) solution could be to write save your class as foo.py, open a jython shell in the same folder and type

import foo

This will automatically create a foo$py.class in that folder.

Although Jython is a nice language, the lack of good packaging tools made me go for Clojure which can be easily packaged for java interoperation with Leiningen . And since you are going to write AI, using a lisp might not be a bad idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM