Example: Running a Java program from a Jython program

This example creates a custom action that uses a Java program to print “Hello World.” The Java program is called from a Jython program that is executed by the wsadmin scripting client.

In this example, the Java class file HelloWorld was compiled with a no-args constructor and contains a method called sayHello. The Java class file is contained in a Java package called hello that is saved to a JAR file called hello-world.jar.

The custom action is available at the cell scope for a network deployment WebSphere® Application Server cell.

The custom action is saved to the build file: custom_configure_was_common_nd.xml.

  1. Copy the JAR file that contains the compiled Java program files to RAFW_HOME/user/lib/java/wsadmin_lib/hello-world.jar.

    The hello-world.jar file is compiled using the JDK V1.4 so that it can be executed by the wsadmin client on a WebSphere V6.0 target system.

  2. Create a Jython program that invokes the Java sayHello method. Save the Jython program to hello.py: RAFW_HOME/user/actions/configure/was/common/nd/scripts/hello.py.
    import hello.HelloWorld as HelloWorld
    
    ## parse any arguments from wsadmin and say hello
    optDict, args = SystemUtils.getopt(sys.argv, 'who:')
    recv= optDict['who']
    
    talker = HelloWorld() 
    talker.sayHello(recv)
  3. Add the Ant target to the custom_configure_was_common_nd.xml build file located in RAFW_HOME/user/actions/configure/was/common/nd.
    <target name="user_say_hello" description="Use wsadmin to say hello">
    	 <antcall target="call_wsadmin">
    		<param name="TASK" value="user_say_hello"/>
    		<param name="SCRIPT_NAME" value="${RAFW_HOME}/user/actions/configure/
                                          was/common/nd/scripts/hello.py"/>
    		<param name="ARGS" value="-who World"/>
    	 </antcall>
    </target>
  4. Test your custom action.
    • List the actions available at the cell scope to confirm that the custom action user_say_hello is available:
      rafw.sh -e env_name -c cell_name -list
    • Run the action in execute mode with the -preview flag. Preview runs the action but does not make any changes on the target system.
  5. Run the action on the target system.
    rafw.sh -e env_name -c cell_name -t user_say_hello

Feedback