.net下使用rhino

rhino是java实现的,但我有段时间没有用java了,现在都是在.net上开发,但google了半天没有找到.net上js引擎,没办法,用ikvm(http://www.ikvm.net/userguide/ikvmc.html)转换下。

当然先下载rhino,官网下载页面:http://www.mozilla.org/rhino/download.html,下载zip包硬是没有下得下来。

没得办法,cvs下来自己编译吧。cvs客户端工具我用的是TortoiseCVS这个,很好用,下载安装参考(http://www.javaeye.com/topic/104)。

cvs客户端设置参考页面:https://developer.mozilla.org/en/Mozilla_Source_Code_Via_CVS

用了TortoiseCVS,我们只要看这段:

If you are using a graphical CVS interface, use the following server data:

  • host: "cvs-mirror.mozilla.org"
  • repository path: "/cvsroot"
  • user: "anonymous"
  • connection type: pserver
  • port: default (2401)

module选择: mozilla/js/rhino

 

把rhino的source下载下来了,下面就编译了,想起来机器上JDK都还没有安装呢,JDK的下载安装配置就不说了。

在rhino下有build.xml,当然就是用ANT编译啦,下载安装ANT参考(http://www.cnblogs.com/sunsonbaby/archive/2004/08/27/37156.html)。

CMD到rhino的根目录,输入ANT会有一个HELP出来的,照着HELP根据需要编译。

我这里就 ant jar javadoc了,生成jar库和帮助文档。有了jar接下来就用ikvmc工具把jar转换成dll程序集。

CMD到生成的jar的根目录,输入ikvmc有help的,根据需要给参数。

我这里就ikvmc -out:org.mozilla.rhino.dll js.jar了,生成org.mozilla.rhino.dll。

创建.net项目,添加引用就可以用了。

这里我就给个简单的c#的示例吧。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using org.mozilla.javascript;

namespace RhinoPractice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Context ctx = Context.enter();
            Scriptable scope = ctx.initStandardObjects();
            String str = "9*(1+2)";  
            Object result = ctx.evaluateString(scope,str,null,1,null);
            double res = Context.toNumber(result);
            MessageBox.Show(str + " : " + res.ToString());
        }
    }
}

 

当然,只是添加了org.mozilla.rhino.dll是不够得,还有ikvm实现的java的库。

当然,这个在你编译执行上面那段示例的时候会报错,错误会提示你去添加那些dll的。

posted on 2011-01-25 11:59  $tommix  阅读(519)  评论(3)    收藏  举报

导航