Skip to content

Commit

Permalink
Java Collections and lambda usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
paulvi committed Apr 16, 2014
1 parent 505dcc7 commit 2327f74
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
10 changes: 5 additions & 5 deletions org.nodeclipse.help/contents/.nashorn.md.html
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@

</head>
<body class="markdown-body">
<h1> <a name="nashorn" class="anchor" href="..md.html#nashorn"><span class="octicon octicon-link"></span></a>Nashorn</h1>
<h2> <a name="links" class="anchor" href="..md.html#links"><span class="octicon octicon-link"></span></a>Links</h2>
<h1> <a name="user-content-nashorn" class="anchor" href="..md.html#nashorn"><span class="octicon octicon-link"></span></a>Nashorn</h1>
<h2> <a name="user-content-links" class="anchor" href="..md.html#links"><span class="octicon octicon-link"></span></a>Links</h2>
<p>Main blog <a href="https://blogs.oracle.com/nashorn/">https://blogs.oracle.com/nashorn/</a> by Jim Laskey</p>
<p>Sources: <a href="http://hg.openjdk.java.net/nashorn/jdk8/nashorn">http://hg.openjdk.java.net/nashorn/jdk8/nashorn</a></p>
<ul>
Expand All @@ -464,9 +464,9 @@ <h2> <a name="links" class="anchor" href="..md.html#links"><span class="octicon
</ul>
<p>Old and excited blog post: <a href="http://kaeff.net/posts/why-ruby-and-nodejs-folks-should-care-about-project-nashorn.html">http://kaeff.net/posts/why-ruby-and-nodejs-folks-should-care-about-project-nashorn.html</a></p>
<p>If you use JavaFX inside JavaScript, you should use SDK, not just JRE.</p>
<h2> <a name="java-8-script-utils" class="anchor" href="..md.html#java-8-script-utils"><span class="octicon octicon-link"></span></a>Java 8 script utils</h2>
<h2> <a name="user-content-java-8-script-utils" class="anchor" href="..md.html#java-8-script-utils"><span class="octicon octicon-link"></span></a>Java 8 script utils</h2>
<p>Java 8 goes with 2 utils: <code>jjs</code> for running JavaScript on Nashorn engine and <code>jrunscript</code> for running any script engine</p>
<h3> <a name="jjs-command-line-util" class="anchor" href="..md.html#jjs-command-line-util"><span class="octicon octicon-link"></span></a><code>jjs</code> command line util</h3>
<h3> <a name="user-content-jjs-command-line-util" class="anchor" href="..md.html#jjs-command-line-util"><span class="octicon octicon-link"></span></a><code>jjs</code> command line util</h3>
<pre><code>
C:\Program Files\Java\jdk1.8.0\bin&gt;jjs.exe -v

Expand Down Expand Up @@ -504,7 +504,7 @@ <h3> <a name="jjs-command-line-util" class="anchor" href="..md.html#jjs-command-
-v, -version (Print version info of Nashorn.)
param: [true|false] default: false
</code></pre>
<h3> <a name="jrunscript-command-line-util" class="anchor" href="..md.html#jrunscript-command-line-util"><span class="octicon octicon-link"></span></a><code>jrunscript</code> command line util</h3>
<h3> <a name="user-content-jrunscript-command-line-util" class="anchor" href="..md.html#jrunscript-command-line-util"><span class="octicon octicon-link"></span></a><code>jrunscript</code> command line util</h3>
<pre><code>
C:\Program Files\Java\jdk1.8.0\bin&gt;jrunscript.exe -h
Usage: jrunscript [options] [arguments...]
Expand Down
77 changes: 77 additions & 0 deletions org.nodeclipse.help/contents/nashorn.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,83 @@ C:\Program Files\Java\jdk1.8.0\bin>jrunscript.exe -q
Language ECMAScript ECMA - 262 Edition 5.1 implemention "Oracle Nashorn" 1.8.0
</code></pre>

### Examples

#### Using threads in JavaScript

From <http://www.infoq.com/articles/nashorn>

```javascript
// this is how we get access to Java class Thread
var Thread = Java.type("java.lang.Thread");
// subclass with our run method
var MyThread = Java.extend(Thread, {
run: function() {
print("Run in separate thread");
}
});
var th = new MyThread();
th.start();
th.join();
```

More

```javascript
var r = new java.lang.Runnable({
run: function() {
print("running...\n");
}
});
```

#### Lambdas

Compare

```javascript
var jsArray = [4,1,3,2];
jsArray.forEach(function(el) { print(el) } );
```

and the same with Java Collections and lambda usage:

```javascript
var jsArray = [1,2,3,4];
var list = java.util.Arrays.asList(jsArray);
list.forEach(function(el) { print(el) } );
```

#### Scripting

From <https://blogs.oracle.com/nashorn/entry/csi_nashorn_shell_scripting_in>

```javascript
#!/usr/bin/jjs
# This script hunts down the change set associated with a
# source file and a line number.
#
// Report proper command usage.
function usage() {
error(<<EOS);
usage: suspect javaFileName lineNumber
javaFileName - name of file in local mercurial repository
lineNumber - file line number
EOS
}
// Provide meaningful names for arguments.
var fileName = $ARG[0];
var lineNumber = $ARG[1];
// If arguments are missing, report proper usage.
if (!fileName || !lineNumber) {
usage();
}
```

### Contribute

<a href="https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.help/contents/nashorn.md" target="_blank">Edit online on GitHub</a>
Expand Down

0 comments on commit 2327f74

Please sign in to comment.