I'm currently coding a game in Java that is growing bigger over time.
So now I'm at a point where more dynamic implementations of code would become quite handy.
I decided to take a look into Jython and got it working with some simple scripts already.
(Btw I'm using the newest standalone Jython if it matters somehow).
Now my Question is: Can I execute methods that are in my Java project in a Jython script that is executed in the mentioned Java project?
Here is an example of pseudo-code for better understanding what I want to ask:
Let's say I have a Script that looks like the following:
def main():
killPlayer()
main()
And a Java class that contains the method "killPlayer()":
public void killPlayer() {
player.setAlive(false);
}
While the Jython script is executed as following:
PythonInterpreter pyInterp = new PythonInterpreter();
pyInterp("script.py");
Is anything of that kind possible?
Thanks in advance and sorry for my poor english ^^
You can import Java classes in Jython like so:
from javax.swing import JFrame
f = JFrame('Hello, World!', defaultCloseOperation=JFrame.EXIT_ON_CLOSE, size=(300, 300), locationRelativeTo=None)
f.setVisible(True)
(source)
So my suggestion is to use the Java code as a library and implement the "setup everything" in Python. That way, you won't need to figure out a way to look up instances of Java objects from the script.
If you need to look up instances, I suggest to create a static global variable somewhere which gives you access to a class that exposes important game instances. In the Java code, you can then register the instances with a name in a Map for the Jython scripts.
Related
I've been trying to find an answer to this for some time, but I think part of my problem is that I don't really know how to phrase my question. I understand the that JVM ultimately preforms all the system calls at run-time that the Java program needs to make, my confusion is about the underlying way that Java classes tell the JVM to do this.
Take for example, the File class in the standard Java library. As far as I know, this is considered the most fundamental API for opening/creating files in Java. But, File is just another class right? So in theory I should be able to write my own File class from scratch which doesn't utilize the pre-exisitng one, right? How would I do that? What is happening inside the File class that tells the VM to actually create the file? I looked at the source code for the File class, and it looks like it calls another class called VMFile, but I could find no explanation of what VMFile is. When I looked at the VMFile source code, it just had function declarations with no definitions.
Thank you for your help.
The Java Native Interface (JNI) is the glue between Java classes and the OS. Native methods have a 'native' attribute (look it up in the JLS).
I want user to be able to input a snippet of code with a class or function implemented in Java and the program should run this code, without referring to external tools like javac or saving class files to fisk.
In Clojure it is clear, as there is read and eval. Can it be done for a plain Java code, possibly with some third party jar?
The code is expected to be some variables, some loops, some mathematical functions and maybe some calls to the framework that is running it.
I am working on a project, which will introduce programmable computers to Minecraft, similar to ComputerCraft, except using Python as opposed to lua.
I am aware of Jython, so thought it would be suitable to check if I could use that for my project, however couldn't find enough information (on their website and with a few searches) to be certain.
I am aware of the topic discussing using Java from within Jython, however this is not how I want my project to work. Those that have used Computercraft, know that you have only the libraries that Computercraft provides you, whereas the topic linked above has full access to.. well everything. In my use case, everything isn't possible. I also don't want from pycomputers.api import Colors, I want the 'colors' api to be used like colors.red.
Hopefully the above is possible, within Jython, if not I would love to know another Python interpreter (that can be used from Java), to make my project with.
According to the docs if you want to embed the Jython into a java application then you have to invoke it with the PythonInterpreter class. It's pretty strait forward from there, just note that the overloads that provide a filename argument are for the name of the main executable file (this information is normally available through the sys module in regular old CPython).
Now in order to expose bindings in java to python (say to control the minecraft world), we need to add the jar files to sys.path as is described here, and if you want to control the sys.path value from within java then use
PythonInterpreter pi = new PythonInterpreter();
pi.getSystemState().path.append(new PyString("path/to/java/modules.jar"));
Note that the packages inside the jar files cannot be organized in the reverse url style. Instead, follow the instructions on package naming. Particularly make note of Naming Python Modules and Packages and if you follow the guidelines in Proper Python Naming then you will get the results you desire.
Finally we execute the code with
String source = ...;
pi.execute(pi.compile(source));
I have here a small Scala console application and its working fine. I have now just one trouble: this application should be extended. One of the requests are, that there should be a GUI (AWT,SWT, Swig - doesnt matter, it just have to run on windows).
Well I really dont want to write my own GUI. I like tools like the WindowBuilder. Can I combine this effective? If I would create the GUI with the editor, I'll get generated Java code right? Could this be a problem? I just have to create a Mixin-Project, build the GUI and for each GUI element (Button or whatever with a listener) allocate a scala function. So I dont have to rewrite any of the scala stuff, just point to the function. Is this how it could work? Any suggestions? Thank you
Edit: For me it looks dirty to generate java code in a scala application, but well as I see there is no other solution.
I've done this several times and see no problem with mixing Java and Scala code in one project.
To make things easier for Scala compiler don't put Java and Scala classes into the same package.
I have three different classes written in python. They contain several methods which I want to use them inside my java program (creating objects from these python classes and use these objects to call the methods). Actually my level in java might be intermediate. However, i am absolutely new in python. I have read that it is possible to use Python class methods inside java but there is no clear way that is well explained. So, please help me with a script or a tutorial or any advice that can help understanding using python classes methods in java.
Jython is the way to go. If you have a couple of hours I would recommend you go over the tutorial for it.
If you are curious to see how jython can be used inside java skip to:
http://wiki.python.org/jython/LearningJython#integrating-jython-python-into-java