java中使⽤动态语⾔
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 EngineByName("JavaScript");
Java代码
try {
engine.eval("print('hello word')");
} catch (ScriptException 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 EngineByName("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) {
}
} catch (ScriptException ex) {
}
}
}
脚本引擎本⾝并不负责执⾏在脚本中定义的⽅法,⽽是转换为javax.script.Invocable 的对象ivh,有iv
h的⽅法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 EngineByName("JavaScript");
testScript(engine);
}
public static void testScript(ScriptEngine engine){
File file=new File("F:\\");
if(!ists()){
try {
} catch (IOException ex) {
}
}
engine.put("f", file);
try {
engine.eval("println('file size:'+f.length())");
} catch (ScriptException 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 EngineByName("JavaScript");
testScript(engine);
}
python转java代码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 EngineByName("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");
javax.script.Invocable inv=(Invocable)engine;
Runnable Interface(obj, Runnable.class);
Thread t=new Thread(r);
t.start();
}
}
这⾥通过这句:
Object ("obj");
来获得脚本中的变量。
然后在通过ivh的getIntegerface()⽅法转化为java的接⼝。最后启动⼀个线程来执⾏它。
另外:在jdk6中有⼀个命令⾏⼯具:jrunscript
进⼊bin⽬录,下⾯可以运⾏的,默认为javascript语⾔,这个可以⽤来测试我们写的脚本是否正确
[img]dl.iteye/upload/attachment/259798/e158030e-dee5-30cf-aa45-62e990899c3d.jpg[/img]
转⾃:chenhua-1984.iteye/blog/684237
------------------java调⽤perl脚本-----------------------
perl - Java调⽤perl
Java 调⽤ perl
Java代码
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().exec("perl C:\\Perl\\execise.pl");
InputStream inputStream = InputStream();
BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream));
// save first line
if ((lineToRead = adLine()) != null) {
resultStringBuffer.append(lineToRead);
}
// save next lines
while ((lineToRead = adLine()) != 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 = itValue();
} catch (Exception ex) {
resultStringBuffer = new StringBuffer("");
exitValue = 2;
}
System.out.println("exit:" + exitValue);
System.out.String());
}
}
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";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论