Search Tips   |   Advanced Search



Jython is an alternate implementation of Python, and is written entirely in Java.

The wsadmin tool uses Jython V2.1.

The following information is a basic summary of the Jython syntax. In all sample code, the => notation at the beginning of a line represents command or function output.

Supported configurations: WAS v7.0 uses a Jython version that does not support Microsoft Windows 2003 or Windows Vista operating systems.


Basic function

The function is either the name of a built-in function or a Jython function. For example, the following functions return "Hello, World!" as the output:

print "Hello, World!"
=>Hello, World!

import sys
sys.stdout.write("Hello World!\n")
=>Hello, World!

In the example, print identifies the standard output stream. Use the built-in module by running import statements such as the previous example. The statement import runs the code in a module as part of the importing and returns the module object. sys is a built-in module of the Python language. In the Python language, modules are name spaces which are places where names are created. Names residing in modules are called attributes. Modules correspond to files and the Python language creates a module object to contain all the names defined in the file.

In other words, modules are name spaces.



To assign objects to names, the target of an assignment should be on the left side of an equal sign (=) and the object that we are assigning on the right side. The target on the left side can be a name or object component, and the object on the right side can be an arbitrary expression that computes an object.

The following rules exist for assigning objects to names:

Variable name rules are similar to the rules for the C language, for example:

The following reserved words can not be used for variable names:

and assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while

For example:

 a  = 5 print a
=> 5
 b =  a print b
=> 5
 text1, text2, text3, text4  = 'good', 'bad', 'pretty', 'ugly' print text3
=> pretty
The second example assigns the value of variable a to variable b.


Types and operators

The following list contains a few of the built-in object types:

The following list contains a few of the operators:


Backslash substitution

If a statement needs to span multiple lines, we can also add a back slash (\) at the end of the previous line to indicate we are continuing on the next line. Do not use white space characters, specifically tabs or spaces, after the back slash character. For example:

 text =  "This is a test of a long lines" \
" continuing lines here." print text
=> This is a test of a long lines continuing lines here.


Functions and scope

Jython uses the def statement to define functions. Functions related statements include:

The basic syntax to define a function is the following:

 def name (arg1, arg2, ... ArgN)
   return value
where name is the name of the function being defined. It is followed by an open parenthesis, a close parenthesis and a colon. The arguments inside parenthesis include a list of parameters to the procedures.

The next line after the colon is the body of the function. A group of commands that form the body of the function. After you define a Jython function, it is used just like any of the built-in functions. For example:

 def intersect(seq1, seq2):
   res = []
      for x in seq1:
         if x in seq2:
   return res
To call the function above, use the following command:

 s1 = "SPAM" s2 = "SCAM" intersect(s1, s2)
=> [S, A, M]
 intersect([1,2,3], (1.4)) 
=> [1]



Make comments in the Jython language with the pound character (#).


Command line arguments

The Jython shells pass the command line arguments to the script as the value of the sys.argv. In wsadmin Jython, the name of the program, or script, is not part of sys.argv. Unlike wsadmin Jython, Jython standalone takes the script file as the first argument to the script. Since sys.argv is an array, use the index command to extract items from the argument list. For example, test.py takes 3 arguments a, b, and c.

wsadmin -f test.py  a  b  c
test.py content:

 import sys first  =  sys.argv[0] second = sys.argv[1] third = sys.argv[2] arglen = len(sys.argv)


Basic statements

There are two looping statements: while and for. The conditional statement is if. The error handling statement is try. Finally, there are some statements to fine-tune control flow: break, continue and pass.

The following is a list of syntax rules in Python:



The if statement selects actions to perform. The if statement may contain other statements, including other if statements. The if statement can be followed by one or more optional elif statements and ends with an optional else block.

The general format of an if looks like the following:

 if test1
   statements1 elif test2
   statements2 else
For example:

 weather = 'sunny' if weather == 'sunny':
   print "Nice weather" elif weather == 'raining':
   print "Bad weather" else:
   print "Uncertain, don't plan anything"



The while statement consists of a header line with a test expression, a body of one or more indented statements, and an optional else statement that runs if control exits the loop without running into a break statement. The while statement repeatedly executes a block of indented statements as long as a test at the top keeps evaluating a true value. The general format of an while looks like the following:

 while test1
   statements1 else
For example:

 a = 0; b = 10 while a < b:
   print a
   a = a + 1



The for statement begins with a header line that specifies an assignment target or targets, along with an object you want to step through.

The header is followed by a block of indented statements which you want to repeat.

The general format of a for statement looks like the following:

 for target in object:
   statements else:

It assigns items in the sequence object to the target, one by one, and runs the loop body for each. The loop body typically uses the assignment target to refer to the current item in the sequence as if it were a cursor stepping through the sequence. For example:

 sum = 0 for x in [1, 2, 3, 4]:
   sum = sum + x


Break, continue, and pass

We can control loops with the break, continue and pass statements.

The break statement jumps out of the closest enclosing loop (past the entire loop statement). The continue statements jumps to the top of the closest enclosing loop (to the header line of the loop), and the pass statement is an empty statement placeholder.



A statement will raise an error if it is called with the wrong number of arguments, or if it detects some error condition particular to its implementation.

An uncaught error aborts execution of a script. The try statement is used to trap such errors. Python try statements come in two flavors, one that handles exceptions and one that executes finalization code whether exceptions occur or not. The try, except, else statement starts with a try header line followed by a block of indented statements, then one or more optional except clauses that name exceptions to be caught, and an optional else clause at the end. The try, finally statements starts with a try header line followed by a block of indented statements, then finally clause that always runs on the way out whether an exception occurred while the try block was running or not.

The general format of the try, except, else function looks like the following:

   statements except name:
   statements except name, data:
   statements else
For example:

   myfunction() except:
   import sys
   print 'uncaught exception', sys.exc_info()
   myfilereader() except EOFError:
   break else:
   process next line here
The general format of a try and finally looks like the following:

   statements finally

For example:

 def divide(x, y):
   return x / y
 def tester(y):
      print divide(8, y)
      print 'on the way out...'


Calling scripts using another script

Use the execfile command to call a Jython script from another Jython script. For example:

Create a script called test1.py that contains the following:

print printName('Cathy', 'Smith')

Create a script called testFunctions.py that contains the following:

def printName(first, last):
name = first + ' ' + last
return name

Then pass the following path as a script argument:

wsadmin -lang jython -f 'c:/temp/script/test1.py'

You must use forward slashes (/) as the path separator. Backward slashes (\) will not work.

Switch statement

When key = a, value becomes 'r1'...

key = a
value = {
         'a' : lambda: 'r1',
         'b' : lambda: 'r2',
         'c' : lambda: 'r3',
         'd' : lambda: 'r4'

Same switch statement using arguments. When key is 'b', and arg is '2' value becomes 'r2 2'.

key = b
arg = 2
value = {
         'a' : lambda p: 'r1' + p,
         'b' : lambda p: 'r2' + p,
         'c' : lambda p: 'r3' + p,
         'd' : lambda p: 'r4' + p

Related concepts



Related tasks

Getting started with scripting


Related information