using python class methods inside java - java

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

Related

How do classes in the Java standard API interact with the OS?

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).

Access methods from Java project in Jython

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.

How to analyze method calls and objects of other classes used in a java class programmatically

i need to detect if a class relies on another class programatically,to detect inappropriate intimacy code smell(i want to analyze other java programs ,using my program).Any directions on
how to achieve this will be a great help.
And
How to identify all the objects created in a java program?
How to identify all the called methods in a java program?
Any help would be appreciated.
You might want to use what's already there instead of building something yourself. Especially if you're not very familiar with the internals of Java and the JVM.
Have a look at JDepend: http://clarkware.com/software/JDepend.html
Use a profiler as JConsole or VisualVM. With the use of profilers you can pretty much see everything that happens at runtime.
One way i think of is using logger, Put some log statement in the construct and in the methods you want to monitor. So through logs you can find out the objects created and methods accessed
I have found very useful the ObjectWeb asm-all Java bytecode manipulation and analysis library, also known as asm-all.jar
It allows you to convert any *.jar application into equivalent XML file. You can fully inspect the application structure, change it in the XML format and convert back into *.jar file
In order to use the XML files you'll need to understand what it contains. Oracle's The Java® Virtual Machine Specification is very good reference to start with
BTW: one thing you can do with this tool is to instrument the bytecode so that it creates runtime profiling information - which methods were called and by whom (as suggested by #upog)

Using Obj-C class from Java, is there a way?

I'm working on a project that requires me to run this code in java, but for a brief part of the code I want to access a class that is written in Objective-C. I want to pass parameters through the class then retrieve the data back into my Java file.
So to try to explain it clearer. My Java code needs to be able to access an Objective C file and retrieve data that I can implement in Java. My current IDE is eclipse, because I need to do this for android. Is there any possible way to make this work?
You can use Rococoa, http://code.google.com/p/rococoa/, or you can use JNI
Here is a similar question with answer: Wrapping an existing application with JNI
Have a look at JIGS, the Java Interface to GNUStep. http://www.gnustep.it/jigs/

Accessing C++ native DLL (with namespaces) through JAVA

I have a c++ DLL where the functions are nested within namespaces.
I need to access these functions in java.. i'm not much of a java expert but i do know a little of its basics.
I found basic java codes to access methods in C++ native DLL using JNI but im not sure how to access the functions nested within namespaces.
You will probably need to get the mangled names of the C++ functions. You can use nm to do that on Unix or dumpbin /exports on Windows. The mangled name will have the namespace and function name, so it should be easy to find (unless there are several overloads with the same name that you need to distinguish). Are you sure the functions are normal C++ functions and not class methods?
Do yourself a favour : use BridJ for C++ interop, it should be able to deal with most namespace issues :-)
(disclaimer : I'm BridJ's author)
"Jeremiah Willcock" answer is right, in any case, you can always access a D.L.L. functions, inside classes or namespaces using "mangled names", its the weird, dirty way of doing things, but, its works, even if there are other ways.
You could do some tests, accesign those functions, and later, make your own wrapper.

Categories