Accessing Java
Scripting in Ignition executes in the java based implementation of Python called Jython. (See Python or Jython?). While this doesn't have any great effect on the Python language itself, one of the great side benefits is that your Python code can seamlessly interact with Java code as if it were Python code. This means that your Python code has access to the entire Java standard library.
To use Java classes, you simply import them as if they were Python modules. For example, the following code will print out all of the files in the user's home directory. This code uses the Java classes java.lang.System
and java.io.File
to look up the user's home directory and to list the files. Notice that we can even use the Python-style for loop to iterate over a Java sequence.
Code Block |
---|
language | py |
---|
title | Example Python - Accessing Java |
---|
|
# Importing the appropriate java libraries.
from java.lang import System
from java.io import File
# Used to look up the files in the users home directory.
homePath = System.getProperty("user.home")
homeDir = File(homePath)
# Loops through the list of files and prints them.
for filename in homeDir.list():
print filename |
You can find the reference documentation for the Java standard class library (also known as, the "JavaDocs") at: http://docs.oracle.com/javase/8/docs/api/
Subclassing Java
You can also create Python classes that implement Java interfaces. You do need some understanding of Java and object-oriented programming concepts, which are outside the scope of this manual. To create a Python class that implements a Java interface, you simply use the interface as a superclass for your Python class. For example, we could augment the example above to use the overload java.io.File.list(FilenameFilter). To do this, we'll need to create a FilenameFilter
, which is an interface in Java that defines a single function:
boolean accept(File dir, String name)
To implement this interface, we create a Python class that has java.io
.FilenameFilter
as its superclass, and implements that Java-style function in a Python-esque way.
Code Block |
---|
language | py |
---|
title | Example Python - Implementing Java Interfaces |
---|
|
# Importing the appropriate java libraries.
from java.lang import System
from java.io import *
# This sets up an extension filter that can check the file extension. Txt is the default.
class ExtensionFilter(FilenameFilter):
def __init__(self, extension=".txt"):
self.extension=extension.lower()
def accept(self, directory, name):
# make sure that the filename ends in the right extension
return name.lower().endswith(self.extension)
# Used to look up the files in the users home directory.
homePath = System.getProperty("user.home")
homeDir = File(homePath)
# Prints out all .txt files. Txt is provided if nothing is specified.
for filename in homeDir.list(ExtensionFilter()):
print filename
# Prints out all .pdf files.
for filename in homeDir.list(ExtensionFilter(".pdf")):
print filename |