成都网站建设设计

将想法与焦点和您一起共享

几个基础的Jython脚本示例

安装 Jython开发环境后,您可以尝试编写***个 Jython脚本。假定您需要查询系统属性,例如 OS 平台。清单 1 中显示了实现此操作的脚本。

成都创新互联公司专业为企业提供富蕴网站建设、富蕴做网站、富蕴网站设计、富蕴网站制作等企业网站建设、网页设计与制作、富蕴企业网站模板建站服务,10余年富蕴做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

清单 1. Jython脚本示例

 
 
 
  1. #import java.lang.System.class  
  2. from java.lang import System  
  3.  
  4. def querySysProperty(propertyName):  
  5.  prop = System.getProperty(propertyName)  
  6.  return prop  
  7.  
  8. prop = querySysProperty('os.name')  
  9. print 'the os property \"os.name\" is assigned with the value: ', prop  

注意:在 Python 中,# 符号是行注释标识符。

在您将清单 1 中的代码输入 Jython解释器交互控制台后,控制台上会显示结果。也可以将此代码片段另存为具有 .py 扩展名的 Jython脚本文件。如果文件路径是 /root/sample.py,则在 wsadmin 中使用命令 wsadmin -lang Jython–f /root/sample.py 运行此脚本。清单 2 中显示了结果。

清单 2. Jython脚本示例运行结果:显示 OS 名称

 
 
 
  1. The os property "os.name" is assigned with the value: "Linux"; 

如果某个脚本调用其他 Jython模块的函数或类方法,则使用 execfile 函数加载来自其他 Jython脚本文件的 Jython脚本。清单 3 显示了一个示例:

清单 3.  Jython脚本示例:调用外部函数

 
 
 
  1.  execfile('/root/Sample.py')  
  2.  
  3. prop = querySysProperty('os.name')  
  4. print 'the os property \"os.name\" is assigned with the value:', prop  

本文标题:几个基础的Jython脚本示例
地址分享:http://chengdu.cdxwcx.cn/article/cdedghi.html