jrunscript as a cross platform scripting environment
Posted
by user12798506
on Oracle Blogs
See other posts from Oracle Blogs
or by user12798506
Published on Tue, 28 Aug 2012 10:28:00 +0000
Indexed on
2012/08/28
15:45 UTC
Read the original article
Hit count: 294
/Java
?????????????????????????????????????????????????????????sh????????????UNIX???????????????????sh???????????????????????????????????????????Windows?????????????????
sh??????????????find?grep?sed?awk???Windows??????????????????????????????????????????????????????????????????????????????????????????????Windows???Cygwin????????????sh??????Windows??????????????Cygwin????????????????????????????????????????????JDK?????jrunscript?????JavaScript???????????????????????1?????????jrunscript???????????????????
- Windows???UNIX???????????????????????
- find?grep?sed?awk?????????sh???????????????Windows Script Host???????
- Java?????????????
- ??????????????????????????????????????????????????????????(?????????????????????????????????????????)
?????????????JDK 6??????????????????????????PC????????????????JDK 6?PC????????????????????????????????????JDK????????????????????????????????????????jrunscript??????????????????????????
?????jrunscript????JavaScript??????????????????????????????????????????
1) Windows???UNIX?????????????????
?????????????????????????????????????????JavaScript???mytool.js???????????????????????jrunscript???????????UNIX????sh???????Windows????bat?????????????????????
mytool.sh (UNIX?):
#!/bin/sh bindir=$(cd $(dirname $0) && pwd) case "`uname`" in CYGWIN*) bindir=`cygpath -w "$bindir"` ;; esac jrunscript "${bindir}/mytool.js" $*mytool.bat (Windows?):
@echo off set bindir=%~dp0 jrunscript "%bindir%mytool.js" %*
UNIX??sh????????Cygwin???????????????????????????????????????????js??????????????UNIX?Windows???????????????????????????????
2) jrunscript??cat, cp, find?grep??????
jrunscript???UNIX??????????????????????????????????
????UNIX??sh?????????????????????UNIX?????????????????????????????????????????src??????????java????????????enum???????java??????????????????????????????????????????????find('src', '.*.java', function(f) { grep('enum', f); });
???????UNIX?????????????????????????????????????????????????????????????????????????????????????????cp(from, to)??????????????????????????????????????????UNIX???????????
$ cp -r src/* tmp/?????????????????????????????????????????find()???????cp -r????????·??????????????????????
function cpr(fromdir, todir, pattern) { if (pattern == undefined) { pattern = ".*"; } var frdir = pathToFile(fromdir).getCanonicalPath(); find(fromdir, pattern, function(f) { // relative dir of file f from 'fromdir'. var relative = f.getParentFile().getCanonicalPath().substring(frdir.length() + 1); var dstdir = pathToFile(todir + "/" + relative); if (!dstdir.exists()) { // Create the destination dir for file f. mkdirs(dstdir); } // Copy file f to 'dstdir'. cp(f, dstdir + "/" + f.getName()); }); }java?????I/O?API??Windows?????????????"/"??????????????????????????????UNIX?Windows??????????????
????????????exec(cmd)?????????jar????????????????????????????????????????????
$ jrunscript js> exec("jar xvf example.jar") META-INF/ ?????????????µ???B META-INF/MANIFEST.MF ???W?J???????µ???B com/ ?????????????µ???B com/example/ ?????????????µ???B com/example/Bar.class ???W?J???????µ???B com/example/dummy/ ?????????????µ???B com/example/dummy/dummy.txt ?????o???????µ???B com/example/dummy.properties ?????o???????µ???B com/example/Foo.class ???W?J???????µ???B???exec()?????????????????????????????????????????????????????????????????Windows????????????I/O??????????????????????????????????BAT?????????
errmsg.bat:
for /L %%i in (1,1,50) do echo "Error Message count = %%i" 1>&2jrunscript??exec()???????????????18???????????????????????????????????
C:\tmp>jrunscript -e "exec('errmsg.bat')" C:\tmp>for /L %i in (1 1 100) do echo "Error Message count = %i" 1>&2 C:\tmp>echo "Error Message count = 1" 1>&2 : C:\tmp>echo "Error Message count = 18" 1>&2 ? ??????????????exec()?????????????????????????????????????????????????????????????????DataInputStream????????????????????????
$ jrunscript js> this["exec"].toString() function exec(cmd) { var process = java.lang.Runtime.getRuntime().exec(cmd); var inp = new DataInputStream(process.getInputStream()); var line = null; while ((line = inp.readLine()) != null) { println(line); } process.waitFor(); $exit = process.exitValue(); }?????????????????????????????????????????????????????exec()???????????????exec()?????????????????????????????exec()???????
function exec(cmd) { var process = java.lang.Runtime.getRuntime().exec(cmd); var stdworker = new java.lang.Runnable( {run: function() { cat(process.getInputStream()); }}); var errworker = new java.lang.Runnable( {run: function() { cat(process.getErrorStream()); }}); new java.lang.Thread(stdworker).start(); new java.lang.Thread(errworker).start(); return proc.waitFor(); }???????????????????cat()???????????cat()?InputStreamReader??????????????????????????????????????????????????
3) JavaScript????????????????
JavaScript?Java???????????????????????JavaScript????????????Ruby?Groovy?Scala???????????????????????????????????????????????10MB?????????????????????????????????????JavaScript????????????????????KB?????????????MB?JAR??????????????????????????JRE?JDK?????????????????????????????????????????
© Oracle Blogs or respective owner