java : mozilla rhino js引擎 使用教程
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip , 解压后运行 cmd
cd D:\rhino\rhino1_7R5
先测试最简单的: java -jar js.jar swing1.js
var SwingGui = new JavaImporter(javax.swing, javax.swing.event, javax.swing.border, java.awt.event);
with (SwingGui) { var frame = new JFrame("test");frame.setSize(300,200);var pane = frame.getContentPane();var button1 = new JButton("button1"); pane.add(button1);frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
测试发行版中例子: java -jar js.jar examples/SwingApplication.js 居然报错了
js: Cannot convert function to interface java.awt.event.WindowListener since it
contains methods with different names
看了书【Java 程序设计】,明白了,改一处即可。
with (swingNames) {try {UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());} catch (e) { }//Create the top-level container and add contents to it.var frame = new swingNames.JFrame("SwingApplication");frame.getContentPane().add(createComponents(), BorderLayout.CENTER);// Pass JS function as implementation of WindowListener. It is allowed since // all methods in WindowListener have the same signature. To distinguish // between methods Rhino passes to JS function the name of corresponding // method as the last argument
/*frame.addWindowListener(function(event, methodName) {if (methodName == "windowClosing") { java.lang.System.exit(0);}});
*///Finish setting up the frame, and show it.frame.pack();frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
一个较复杂的例子: 来自参考书【JavaScript 权威指南】第12章
java -jar js.jar rhinoURLFetcher.js
/** A download manager application with a simple Java GUI*/
var swingNames = new JavaImporter(javax.swing, javax.swing.event, javax.swing.border, java.awt,java.awt.event); importClass(java.net.URL);
importClass(java.io.FileOutputStream);
importClass(java.lang.Thread);with (swingNames) {
var frame = new JFrame("Rhino URL Fetcher"); // The application window
var urlfield = new JTextField(30); // URL entry field
var button = new JButton("Download"); // Button to start download
var filechooser = new JFileChooser(); // A file selection dialog
var row = Box.createHorizontalBox(); // A box for field and button
var col = Box.createVerticalBox(); // For the row & progress bars
var padding = new EmptyBorder(3,3,3,3); // Padding for rows// Put them all together and display the GUI
row.add(urlfield); // Input field goes in the row
row.add(button); // Button goes in the row
col.add(row); // Row goes in the column
frame.add(col); // Column goes in the frame
row.setBorder(padding); // Add some padding to the row
frame.pack(); // Set to minimum size
frame.visible = true; // Make the window visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// When the user clicks the button, call this function
button.addActionListener(function() {try {// Create a java.net.URL to represent the source URL.// (This will check that the user's input is well-formed)var url = new URL(urlfield.text);// Ask the user to select a file to save the URL contents to.var response = filechooser.showSaveDialog(frame);// Quit now if they clicked Cancelif (response != JFileChooser.APPROVE_OPTION) return;// Otherwise, get the java.io.File that represents the destination filevar file = filechooser.getSelectedFile();// Now start a new thread to download the urlnew java.lang.Thread(function() { download(url,file); }).start();} catch(e) {// Display a dialog box if anything goes wrongJOptionPane.showMessageDialog(frame, e.message, "Exception",JOptionPane.ERROR_MESSAGE);}
});// Use java.net.URL, etc. to download the content of the URL and use
// java.io.File, etc. to save that content to a file. Display download
// progress in a JProgressBar component. This will be invoked in a new thread.
function download(url, file) {try {// Each time we download a URL we add a new row to the window// to display the url, the filename, and the download progressvar row = Box.createHorizontalBox(); // Create the rowrow.setBorder(padding); // Give it some paddingvar label = url.toString() + ": "; // Display the URL row.add(new JLabel(label)); // in a JLabelvar bar = new JProgressBar(0, 100); // Add a progress barbar.stringPainted = true; // Display filename inbar.string = file.toString(); // the progress barrow.add(bar); // Add bar to this new rowcol.add(row); // Add row to the columnframe.pack(); // Resize window// We don't yet know the URL size, so bar starts just animatingbar.indeterminate = true; // Now connect to the server and get the URL length if we canvar conn = url.openConnection(); // Get java.net.URLConnectionconn.connect(); // Connect and wait for headersvar len = conn.contentLength; // See if we have URL lengthif (len) { // If length known, then bar.maximum = len; // set the bar to display bar.indeterminate = false; // the percent downloaded}// Get input and output streamsvar input = conn.inputStream; // To read bytes from servervar output = new FileOutputStream(file); // To write bytes to file// Create an array of 4k bytes as an input buffervar buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE,4096);var num;while((num=input.read(buffer)) != -1) { // Read and loop until EOFoutput.write(buffer, 0, num); // Write bytes to filebar.value += num; // Update progress bar}output.close(); // Close streams when doneinput.close();}catch(e) { // If anything goes wrong, display error in progress barif (bar) {bar.indeterminate = false; // Stop animatingbar.string = e.toString(); // Replace filename with error}}
}
}
cd D:\rhino\rhino1_7R5\examples
java -cp ..\js.jar;. org.mozilla.javascript.tools.shell.Main unique.js test1.txt
编写 rhino.bat
@echo off
java -cp D:/rhino/rhino1_7R5/js.jar;. org.mozilla.javascript.tools.shell.Main %*
rhino.bat swing1.js
rhino.bat rhinoURLFetcher.js
彩蛋 : [ mozilla rhino js Test URL or Socket ]
mozilla rhino 用 jsc 编译 *.js
编写 jsc.bat
@echo off
java -cp D:/rhino/rhino1_7R5/js.jar;. org.mozilla.javascript.tools.jsc.Main %*
编写 run.bat
@echo off
java -cp D:/rhino/rhino1_7R5/js.jar;. %*
例如:
编译 jsc.bat chat.js 生成 chat.class
运行 run.bat chat
java : mozilla rhino js引擎 使用教程相关推荐
- java : mozilla rhino js Test URL or Socket
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip ,解压后运行 cmd cd D:\rhino\rhino1_7R5 编写测试脚本 test2.js ...
- v8,spidermonkey,chakra,spidermonkey四大主流JS引擎安装教程
四大主流JS引擎安装教程 参考网址: https://github.com/sslab-gatech/DIE/tree/master/engines 安装步骤: ./download-engine.s ...
- java rhino import_java调用javascript :js引擎rhino
前段时间,在浏览javaeye论坛,看见有人征集如何在java中运行数学表达式. 结果方案五花八门: 1.jakarta commons JEXL. 2.Beanshell 3.Java Math E ...
- java js引擎,Java8 Nashorn JavaScript引擎
使用Java8,Nashorn大大提高了JavaScript 引擎引入,以取代现有的Nashorn Java脚本引擎.Nashorn提供2至10倍更好的性能,因为它直接编译代码在存储器,并传递到字节码 ...
- mozilla js 引擎_Mozilla的内容拦截器,新JavaScript引擎以及更多开源新闻
mozilla js 引擎 在本周的开放源代码新闻摘要中,Mozilla宣布了新的内容阻止程序并结束了Firefox OS,Edge浏览器JavaScript引擎源发布等. 开源新闻让您阅读愉快. 2 ...
- java rhino js类_让Rhino JS看Java类
我正在玩 Rhino,我已经成功使用了stdlib中的Java类,但没有使用我编译的Java代码. 例如,这工作正常: print(new java.util.Date()); 但是使用NanoHTT ...
- DIE(一个JS引擎Fuzzing工具)安装教程
DIE安装教程 简介: 研究领域:JS引擎Fuzzing测试 发表会议:In S&P'20 文章地址:文章 源码地址:源码 系统环境 Ubuntu16.04 DIE依赖工具安装 安装nodej ...
- JS逆向--PyExecJS基本用法--网易云音乐逆向思路,node.js安装教程,逆向思路,逆向分析,加密机制,RSA,AES加密算法,加密算法啊破解,js引擎,定位数据包,分析栈结构,无痕窗口
文章目录 前言 一.JS逆向以及PyExecJS模块介绍 1.JS逆向 2.PyEecJS 二.使用步骤 1.环境安装 安装PyExecJS模块 安装node.js开发环境(官网链接 https:// ...
- JavaScript/Ajax/JQuery知识点(BOM/DOM/ScriptEngine/JS引擎),JSCore
捋顺JavaScript底层知识,重点讲解如原型.作用域.执行上下文.变量对象.this.闭包.按值传递.call.apply.bind.new.继承等难点概念?? JS中的继承?JS的原型模式, ...
- 浏览器内核-渲染引擎、js引擎
一个完整的浏览器包含浏览器内核和浏览器的外壳(shell).浏览器核心--内核分成两部分:渲染引擎和js引擎.由于js引擎越来越独立,内核就倾向于只指渲染引擎. 1 浏览器组成结构 浏览器一般由七个模 ...
最新文章
- php阿里大于验证码开发,阿里大于验证码发送 (ThinkPhp框架)
- Zuul Gateway 网关
- CoreML的入门例子
- 去除EditPlus自动备份功能. bak
- java 内部类 作用_java内部类的作用分析
- cpython vm_【协程原理】 - cPython的VM真变态
- ui kit模板,让新手设计师临摹提高!
- Centos 查看系统硬件信息
- 1、NESSUS安装
- 华为笔记本换装linux双系统,华为及荣耀笔记本装Deepin双系统不能引导Windows的解决...
- DS1308 Datasheet
- 电压源 电流源 置零时的作用
- D2D与蜂窝系统间的干扰
- Android封装sdk页面为h5,Android/H5混合 SDK 集成文档
- pandas计算主力合约(模拟文化财经规则)
- 图片转为字符串(蔡徐坤之舞动人生)
- 【camera专题】 Camera Open/Close (1)
- 【听译大师说话】python 创始人访谈录 1: 关于 django 和 ruby
- mescroll在vue中的应用
- 通威股份80亿投建5万吨多晶硅 欲打造世界级清洁能源企业