Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn Novo Motor Javascript no Java SE 8
Bruno Borges Principal Product Manager Oracle Latin America Agosto, 2014
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Bruno Borges
– Principal Product Manager, Java Evangelist
– Oracle Latin America
– @brunoborges
Speaker
Bruno Borges
Principal Product Manager, Java Evangelist Oracle Latin America @brunoborges [email protected]
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Rhino (Inglês) = Nashorn (Alemão)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn Overview
• Runtime Java baseado na linguagem Javascript
– ECMAScript 262 v5.1
• Familar para desenvolvedores de conteúdo
• Permite uso de uma série de bibliotecas e ferramentas escritas em Javascript
– Node.JS
• Obtém todas as vantagens das tecnologias Java
Scripting for Java
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn Overview
• Forte integração com a plataforma Java
– Extensões foram adicionadas para prover acesso ao Java
• Nashorn pode ser usado no Java usando a ScriptEngine API
• Nashorn pode também ser usado pela linha de comando jjs
• Suporte para shell scripting
• Suporte completo ao JavaFX
• NetBeans 8 suporta desenvolvimento com Nashorn
Funcionalidades
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn dentro de código Java
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn e Java, usando JSR 223 – ScriptEngine
import javax.script.*;
public class Main {
public static void main(String[] args) {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("nashorn");
try {
engine.eval("print('hello world');");
} catch (final ScriptException se) {System.err.println(se); }
}
}
Exemplo 1
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn e Java
String script = "var x = 10;\n" +
"var y = 20;\n" +
"var z = x + y;\n" +
"z;\n";
Object result = engine.eval(script);
int value = (Integer)result;
System.out.println(value);
Exemplo 2
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn e Java
engine.put("x", 100);
engine.put("y", 200);
engine.eval("var z = x + y;");
int value = (Integer)engine.get("z");
System.out.println(value);
Exemplo 3
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn e Java
var HashMap = java.util.HashMap; var map = new HashMap(); map.put("apple", "red"); map.put("bear", "brown"); map.put("canary", "yellow"); print("A bear is " + map.get("bear")); for (var key in map) print("key: " + key); for each (var value in map) print("value: " + value);
Exemplo 4
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn usando Java 8 Streams API Exemplo 5
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn linha de comando – /usr/bin/jjs
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn usando jjs
• Nashorn pode ser chamado usando a ferramenta jjs por linha de comando
• Facilita o uso de Javascript no dia-a-dia
– Tarefas rápidas
– Prototipação
– Experimento com novas features do Java
– Shell scripts
A partir da linha de comando
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn usando jjs
$> where jjs
/usr/bin/jjs
$> jjs
jjs> var x = 10;
jjs> var y = 20;
jjs> print(x + y);
30
jjs> quit();
$>
Exemplo 6
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn e Javascript como Unix Shell Scripting Shell scripts para Unix escritos em Javascript!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Shell Scripting com Nashorn
• Por que não utilizar uma linguagem que você já está familiarizado?
• Acesso a um vasto número de bibliotecas Java
• Extensões de scripting para simplificar
– Documentos
– Templates de Strings
– Execução de comandos
– Variáveis de ambiente
Substituto mais simples para outras linguagens shell
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Schell Scripting usando Nashorn
#!/usr/bin/jjs -doe -scripting # var dir = __DIR__ + "photos"; var files = `ls ${dir}`.trim().split("\n"); var count = 1; for each (var file in files) { if (file.edsWith(".jpg")) { `mv ${dir}/${file} ${dir}/Photo${count++}.jpg`; } }
Exemplo 7
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn e JavaFX Aplicacoes Desktop Escritas em Javascript!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 27
Criando Formas e Materiais Primitivos
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Colocando Textura em uma Esfera
28
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Colocando Textura em uma Esfera
29
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Colocando Textura em uma Esfera
30
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
JavaFX Scene Builder bit.ly/javafxdownload
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Your First JavaFX App for RaspberryPi
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Nashorn e JavaFX
load("fx:base.js");
load("fx:controls.js");
load("fx:graphics.js");
$STAGE.title = "Hello World!";
var button = new Button();
button.text = "Say 'Hello World'";
button.onAction = function() print("Hello World!");
var root = new StackPane();
root.children.add(button);
$STAGE.scene = new Scene(root, 300, 250);
$STAGE.show();
Exemplo 8
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Recapitulando
• Forte integração com Java
• Reuso de bibliotecas Java existente
• Pode ser usado a partir do Java
• Nova ferramenta por linha de comando jjs
• Shell Scripting
• Aplicações escritas em JavaFX
• Motor do WebView no JavaFX
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
Para saber mais
• Download: java.oracle.com
• Documentação: docs.oracle.com/javase
• Treinamentos: education.oracle.com/java
• Central do Java 8: www.oracle.com/java8
• Nashorn para Usuários
– wiki.openjdk.java.net/display/Nashorn/Nashorn+Documentation
• Exemplos: blogs.oracle.com/nashorn/
• Dúvidas: [email protected]
Java 8 e outros sites
youtube.com/java
blogs.oracle.comjava
facebook.com/ilovejava
@java @javaembedded
nighthacking.com
Top Related