來源自 這裡
Preface:
I have been writing standalone Java applications lately, which are run from within a shell script, something like the snippet below. Calling Java standalones in this way is a fairly standard approach. However, being a Python fan(atic?), I would like to write these shell scripts using Python. This article describes how to call Java programs using Jython, which is a Java port of the Python programming language.
- Original shell script to invoke Java
How-To:
In MyClass.java, I have a static main(String[] argv) method which gets invoked as a result of the call above. All it does is instantiate an instance of MyClass and pass some parameters to it, and then call a method on it that does the work. Here is an example, basically yet another implementation of HelloWorld.java, but it will do for illustrating the pattern.
- MyClass.java
Arguably, classes in the real world have more code in their main() methods, but the approach I take when I see that I have more than one object being invoked in my mainmethod, is to factor out the logic into private methods of that class, or into a
Main.java class in that package if multiple classes are involved.
Anyway, the Jython script to call the main method of MyClass with a single string parameter that is passed in from the command line is shown below. Notice how the Jython script instantiates the MyClass object. The classpath is passed in to Jython using sys.path.append() calls. Resource files, such as .properties files or other XML configuration files need to be baked into the JAR file and should be accessible from within the Java code using getResourceAsStream().
As you can see, invoking Java programs from Jython leads to cleaner but more verbose scripts, but does seem to be overkill in our case. However, this approach can really help when you have to design more complicated scripts that deal with a large number of classes. One approach, and my preferred one, is to put this logic in Java in another abstraction layer. However, sometimes, this is not the most practical approach, perhaps because the rules are rather fluid, and you would like to be able tochange the rules without having to go through a big release cycle. In such cases, and depending on the complexity of the rules, I think I would be more productive using Python than Shell Scripts. Even if the rules were simple enough to be encoded within a Shell Script to start out with, it may still be more practical to use Jython, since the rules can get more complicated, and you may be faced with having to rewrite the script.
I don't think it is possible to set JVM parameters (such as minimum and maximum JVM sizes) to Jython from within the Jython script, something that is possible using a Shell Script. Jython itself is a shell script and calls the org.python.util.jython class, so these parameters can be passed into the Java invocation in the jython shell script, although this does not seem to be very clean. Running the class through Jython also seems a little slower than running it through a Shell Script, but I haven't done any benchmarks to say this conclusively.
Test:
底下是我根據上面文章自己寫的 testing code. 首先是 Java 的部分:
- MyClass.java
- Greet.java
接著將上面兩個 .java 編譯後, 壓縮到
Test.jar. 接著底下是 Jython script 來載入該 jar 檔並操作 MyClass 上面的方法:
- test.py
執行結果如下:
Preface:
I have been writing standalone Java applications lately, which are run from within a shell script, something like the snippet below. Calling Java standalones in this way is a fairly standard approach. However, being a Python fan(atic?), I would like to write these shell scripts using Python. This article describes how to call Java programs using Jython, which is a Java port of the Python programming language.
- Original shell script to invoke Java
- #!/bin/bash
- LIBDIR=/path/to/my/jar/repository
- java -classpath $LIBDIR/commons-lang-2.1.jar: $LIBDIR/com-mycompany-4.9.jar com.mycompany.mypackage.MyClass $*
In MyClass.java, I have a static main(String[] argv) method which gets invoked as a result of the call above. All it does is instantiate an instance of MyClass and pass some parameters to it, and then call a method on it that does the work. Here is an example, basically yet another implementation of HelloWorld.java, but it will do for illustrating the pattern.
- MyClass.java
- package com.mycompany.mypackage;
- import org.apache.commons.lang.StringUtils;
- public class MyClass {
- private String name;
- public MyClass() {
- super();
- }
- public void setName(String name) {
- this.name = name;
- }
- public String greet() {
- if (StringUtils.isEmpty(name)) {
- return "Hi there";
- } else {
- return "Hi " + name;
- }
- }
- public static void main(String[] argv) {
- MyClass myclass = new MyClass();
- myclass.setName(argv[0]);
- System.out.println(myclass.greet());
- }
- }
Anyway, the Jython script to call the main method of MyClass with a single string parameter that is passed in from the command line is shown below. Notice how the Jython script instantiates the MyClass object. The classpath is passed in to Jython using sys.path.append() calls. Resource files, such as .properties files or other XML configuration files need to be baked into the JAR file and should be accessible from within the Java code using getResourceAsStream().
- #!/usr/bin/env jython
- import sys
- def setClassPath():
- libDir = "/path/to/my/jar/files/"
- classPaths = [
- "commons-lang-2.1.jar",
- "com-mycompany-4.9.jar"
- ]
- for classPath in classPaths:
- sys.path.append(libDir + classPath)
- def runJavaClass(name):
- from com.mycompany.mypackage import MyClass
- mc = MyClass()
- if (name != None):
- mc.setName(name)
- print mc.greet()
- def main():
- if (len(sys.argv) < 2):
- name = None
- else:
- name = sys.argv[1]
- setClassPath()
- runJavaClass(name)
- if __name__ == "__main__":
- main()
I don't think it is possible to set JVM parameters (such as minimum and maximum JVM sizes) to Jython from within the Jython script, something that is possible using a Shell Script. Jython itself is a shell script and calls the org.python.util.jython class, so these parameters can be passed into the Java invocation in the jython shell script, although this does not seem to be very clean. Running the class through Jython also seems a little slower than running it through a Shell Script, but I haven't done any benchmarks to say this conclusively.
Test:
底下是我根據上面文章自己寫的 testing code. 首先是 Java 的部分:
- MyClass.java
- package demo;
- import java.io.UnsupportedEncodingException;
- public class MyClass {
- private String name;
- public MyClass() {
- super();
- }
- public void setName(String name) {
- this.name = name;
- }
- public Greet greet() {
- if (name==null || name.isEmpty()) {
- return new Greet("Hi there\n");
- } else {
- return new Greet(String.format("Hello %s\n", name));
- }
- }
- }
- package demo;
- public class Greet {
- public String greet;
- public Greet(String g){this.greet=g;}
- public void hi(){System.out.printf("%s\n", greet);}
- }
- test.py
- import sys
- sys.path.append("Test.jar") # 'Test.jar' 放在與此 .py 相同路徑下!
- from demo import *
- mc = MyClass()
- mc.setName("John")
- gt = mc.greet()
- gt.hi()
沒有留言:
張貼留言