Jython exploration
Jython is a 100% pure Java implementation of Python programming languages. It combines the advantages of Python and Java virtual machines and libraries, and as a convenient supplement to the Java platform, but because the original developers are now developing Python under the .NET, It is still only compatible with Python 2.1 syntax, and the new version is not yet. Using people say that the next version is no longer compatible with Python, but the new is not yet, it is not good. But I think it is still compatible, so as long as I don't use the Python of the C API library, I can use it directly.
Initially Jython called JPython, it is a full Java application that allows developers to use the syntax and most of the Python programming languages. Jython's supplement to Java, it is mainly suitable for several aspects:
Built-in scripts, Java programmers can join Jython libraries in the system so that end users can expand their feature in their applications.
The interactive test environment, Jython provides an interactive interpreter interface so that it can be combined with the Java package or the running Java program. Programmers can apply Jython experiments and debug his system, which makes some developments simpler, and it is especially useful in the automatic test framework.
Efficient application development. Python is 2-10 times higher than the Java language, and developers like Python's grammar and the sensation of this language; they find that it is a more productivity of the development and maintenance of the productivity of Java applications. Seamless combination of Python and Java enables developers to use these two languages during the development system.
First, Jython Getting Started
The operating environment is Windows 2000 SP4, JDK 1.4.2.
1, run Jython
Download Jython_21.class from http://sourceforge.net/projects/jython//projects/jython/.
Put the downloaded Jython-21.class and place the file in the ClassPath and run Java Jython-21, you can enter the installation interface.
After the installation is complete, join the installation directory to the path, run Jython, you can enter Jython Shell, just like Python under this shell, no longer introduce it.
Call commands under Jython under the shell:
Jython [Options] [-jar jar | -c cmd | file | -] [args]
Options and parameters and arguments:
-i
Keep an interactive state when the script is completed
-S
Do not display prompt information
-Dprop = value
Set the Python property Prop to Value
-jar jar
Run the __run__.py file in the specified jar.
-C cmd
Run script string
File
Run script file
-
Read the script from the standard input. You can use a file to Jython: Filter File | Jython -
--help
help information
--Version
Version Information
argg
Passment into the program (Sys.Argv [1:]) parameters
2, adjust Jython in Java
Mainly to call this class org.python.util.pythoninterpreter method.
Here is an example given by Jython:
Import org.python.util.pythoninterpreter;
Import org.python.core. *;
Public class simpleembedded {
Public static void main (string [] args) throws pyexception
{
PythonInterpreter interp =
New pythoninterpreter ();
System.out.println ("Hello, Brave New World");
Interp.exec ("Import Sys");
Interp.exec ("Print Sys");
Interp.set ("a", New Pyinteger (42));
Interp.exec ("Print A");
Interp.exec ("x = 2 2");
PyObject x = interp.get ("x");
System.out.println ("x:" x);
System.out.println ("Goodbye, Cruel World");
}
3, operate Java in Jython
Make Python's easy use of existing Java libraries in Jython's goals.
example:
In Jython's interactive environment, how to use Jython to create an instance of a random number class under Java (Java.util.random) and use this instance.
C: / Jython> Jython
Jython 2.0 on java1.2.1
TYPE "Copyright", "Credits" or "license" for more information.
>>> from Java.util Import Random
>>> R = Random ()
>>> R. NextINT ()
-790940041
>>> for i in inning (5):
... Print R.Nextdouble ()
...
0.23347681506123852
0.8526595592189546
0.3647833839988137
0.3384865260567278
0.5514469740469587
>>>
Other content
In this example, it shows that the JAVA package and the Python library in the Jython environment are small, but there are still things that need attention.
Importing
Jython 2.0 on java1.2.1
TYPE "Copyright", "Credits" or "license" for more information.
>>> from java.util import *
>>> Random
>>> HashTable
>>>
Establish an example
You can build a Java class instance with the method of establishing a Python class instance, you must call with parameter "Java class name", which must be adapted to the constructor of the Java class. The contents of the parameters will be described in more detail below.
Call Java method and function
Java classes have both static and instance methods this makes them behave much like a cross between a Python module and class. As a user, you should rarely need to be concerned with this difference. Call Java methods and functions like call these methods The Python copy is the same. The type of input parameter type and the type of return value are automatically enforced.
This table shows how the input parameter of the Python object is converted to Java objects. Note If the java.lang.object input parameter is expected to be a string, there is a problem when replacing.
Parameter type of Java method
Corresponding Python type
charr
String (length must be 1)
Boolean
Integer (True = non-zero)
Byte, Short, Int, Long
Integer
Float, Double
Float
Java.lang.String, Byte [], char []
String
Java.lang.class
Class or JavaClass (if the subclass of the Java class does not allow multiple Java classes to inherit multiple Java classes, only one Java class is allowed)
Foo []
Array (can only be an object specified or this subclass)
Java.lang.object
String-> java.lang.string, other unchanged
Org.python.core.pyObject
constant
Foo
Instance-> foo (instance of subclass of INSTANCE); JavaInstance -> Foo (JavaInstance is an instance of Foo or Foo subclass)
The Java method returns value is also converted to the response Python object.
Return value type of Java method
Returned Python type
charr
String (length 1)
Boolean
Integer (True = 1, False = 0)
Byte, Short, Int, Long
Integer
Float, Double
Float
Java.lang.string
String
Java.lang.class
Javaclass as Java Class
Foo []
Array (including objects of foo and subclasses of foo)
Org.python.core.pyObject (Or Subclass)
Changeless
Foo
JavaInstance as JavaClass foo
Overloaded Java Method processing
FOO has two, "void foo (int x); Void foo (byte x);" Calling the second method can use the following method:
From Java.lang Import Byte
Foo (byte (10))
There should be no better way.
Treatment of Name and Python Keyword Conflict
Since the Java keyword and the Python keyword are different, there are many Java class names, and the method name has conflicts with the Python keyword. This must be clearly distinguished. Something can be written as this:
Java.lang.system.out.print ("hi")
or
Java.lang.Runtime.Getruntime (). EXEC (CMD)
At very few, the conflict can be solved by Python, so it needs to be added with a lower slider after the reserved word, for example: Print_.