jdk6增加了对动态语言的支持,我觉得他的原理就是把动态语言也编译成JVM可以理解的JAVA字节码的形式来执行的。主要支持的有javascript ,ruby ,php,python 等
这样就可以利用脚本语言的动态特性。
下面是对javascript支持的几个例子。
第1个:
Java代码
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class HelloScript {
public static void main(String[] args){
例子比较简单,就是打印hello word,重要的几个类:
写道
ScriptEngineManager factory=new ScriptEngineManager();
ScriptEngine engine=factory.getEngineByName("JavaScript");
Java代码
try {
engine.eval("print('hello word')");
} catch (ScriptException ex) {
Logger.getLogger(HelloScript.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
第二个:
java调用脚本语言的方法
Java代码
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TestJsFun {
public static void main(String[] args){
ScriptEngineManager factory=new ScriptEngineManager();
ScriptEngine engine=factory.getEngineByName("JavaScript");
try {
String script="function hello(name){return 'Hello'+name}";
engine.eval(script);
javax.script.Invocable inv=(Invocable)engine;
try {
String res = (String) inv.invokeFunction("hello", "chenhua");
System.out.println(res);
} catch (NoSuchMethodException ex) {
Logger.getLogger(HelloScript.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ScriptException ex) {
Logger.getLogger(HelloScript.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
脚本引擎本身并不负责执行在脚本中定义的方法,而是转换为javax.script.Invocable 的对象ivh,有ivh的方法invokeFunction()方法来调用脚本的方法。invokeFunction()方法的第一个参数必须是方法名。后面是参数列表。
第三个例子:脚本语言用java的变量
Java代码
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TestScriptVariables {
public static void main(String[] args){
ScriptEngineManager factory=new ScriptEngineManager();
ScriptEngine engine=factory.getEngineByName("JavaScript");
testScript(engine);
}
public static void testScript(ScriptEngine engine){
File file=new File("F:\\test.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(TestScriptVariables.class.getName()).log(Level.SEVERE, null, ex);
}
}
engine.put("f", file);
try {
engine.eval("println('file size:'+f.length())");
} catch (ScriptException ex) {
Logger.getLogger(TestScriptVariables.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
通过 写道
engine.put("f", file);
来设置一个全局的变量。这个API比较重要
这样就取到了文件的大小。
第4个例子
脚本语言适用java的类库
Java代码
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TestUsingJdkClass {
public static void main(String[] args) throws ScriptException, NoSuchMethodException{
ScriptEngineManager factory=new ScriptEngineManager();
ScriptEngine engine=factory.getEngineByName("JavaScript");
testScript(engine);
}
public static void testScript(ScriptEngine engine) throws ScriptException, NoSuchMethodException{
String js ="function doSwing(t){ var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";
engine.eval(js);
javax.script.Invocable inv=(Invocable)engine;
inv.invokeFunction("doSwing", "from javascript swing window");
}
}
在脚本中使用了Packages这一个全局变量,专门用于访问java的类库,程序运行后,会出现java Swing的一个窗体
看截图
最后一个例子:脚本语言实现java的接口
Java代码
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class TestScriptInterface {
public static void main(String[] args) throws ScriptException, NoSuchMethodException{
ScriptEngineManager factory=new ScriptEngineManager();
ScriptEngine engine=factory.getEngineByName("JavaScript");
testScript(engine);
}
public static void testScript(ScriptEngine engine) throws ScriptException, NoSuchMethodException{
String script="var obj= new Object();obj.run=function(){println('run() was called');}";
engine.eval(script);
Object obj=engine.get("obj");
javax.script.Invocable inv=(Invocable)engine;
Runnable r=inv.getInterface(obj, Runnable.class);
Thread t=new Thread(r);
t.start();
}
}
这里通过这句:
Object obj=engine.get("obj");
来获得脚本中的变量。
然后在通过ivh的getIntegerface()方法转化为java的接口。最后启动一个线程来执行它。
另外:在jdk6中有一个命令行工具:jrunscript
进入bin目录,下面可以运行的,默认为javascript语言,这个可以用来测试我们写的脚本是否正确
[img]http://dl.iteye.com/upload/attachment/259798/e158030e-dee5-30cf-aa45-62e990899c3d.jpg
------------------java调用perl脚本-----------------------
perl - Java调用perl
Java 调用 perl
Java代码
Runtime.getRuntime().exec("perl C:\\Perl\\execise.pl");
Java 调用 perl 通过IO的方式来得到返回值
Java代码
package fiveGame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class JavaCallPerl {
public static void main(String[] args) throws IOException {
StringBuffer resultStringBuffer = new StringBuffer();
String lineToRead = "";
int exitValue = 0;
try {
Process proc = Runtime.getRuntime().exec("perl C:\\Perl\\execise.pl");
InputStream inputStream = proc.getInputStream();
BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream));
// save first line
if ((lineToRead = bufferedRreader.readLine()) != null) {
resultStringBuffer.append(lineToRead);
}
// save next lines
while ((lineToRead = bufferedRreader.readLine()) != null) {
resultStringBuffer.append("\r\n");
resultStringBuffer.append(lineToRead);
}
// Always reading STDOUT first, then STDERR, exitValue last
proc.waitFor(); // wait for reading STDOUT and STDERR over
exitValue = proc.exitValue();
} catch (Exception ex) {
resultStringBuffer = new StringBuffer("");
exitValue = 2;
}
System.out.println("exit:" + exitValue);
System.out.println(resultStringBuffer.toString());
}
}
perl
Java代码
#!/usr/bin/perl -w
$pi = 3.141592654;
$circ = 2 * $pi * 12.5;
print "The circumference of a circle of radius 12.5 is $circ.\n";