Nashorn: novo motor
Javascript no Java 8
Bruno Borges
Principal Product Manager
Java Evangelist
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.3
• Java Evangelist
• Orale Product Manager
• Entusias...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.4
Agenda
• Nashorn Overview
• Nashorn e Java
• Nashorn...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.5
Nashorn Overview
• Runtime Java baseado na linguagem...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.6
Nashorn Overview
• Forte integração com a plataforma...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.7
NASHORN E JAVA
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.8
Nashorn e Java
import javax.script.*;
public class M...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.9
Nashorn e Java
String script = "var x = 10;n" +
"var...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.10
Nashorn e Java
engine.put("x", 100);
engine.put("y"...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.11
Nashorn e Java
var HashMap = java.util.HashMap;
var...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.12
NASHORN
USANDO JJS
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.13
Nashorn usando jjs
• Nashorn pode ser chamado usand...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.14
Nashorn usando jjs
>> where jjs
/usr/bin/jjs
>> jjs...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.15
Nashorn usando jjs
Exemplo 6
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.16
SHELL SCRIPTING
COM NASHORN
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.17
Shell Scripting com Nashorn
• Por que não utilizar ...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.18
Schell Scripting usando Nashorn
#!/usr/bin/jjs -doe...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.19
NASHORN E
JAVAFX
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.20
Nashorn e JavaFX
• Método bem natural de trabalhar ...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.21
Nashorn e JavaFX
load("fx:base.js");
load("fx:contr...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.22
Recapitulando
• Forte integração com Java
• Pode se...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.23
Para saber mais
• Download: java.oracle.com
• Docum...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.25
The preceding is intended to outline our general pr...
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.26
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.27
of 25

Nashorn: nova engine Javascript do Java SE 8

Descubra as capacidades do novo motor de Javascript, Nashorn, presente no Java SE 8
Published on: Mar 3, 2016
Published in: Technology      
Source: www.slideshare.net


Transcripts - Nashorn: nova engine Javascript do Java SE 8

  • 1. Nashorn: novo motor Javascript no Java 8 Bruno Borges Principal Product Manager Java Evangelist
  • 2. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.3 • Java Evangelist • Orale Product Manager • Entusiasta JavaFX e IoT • Onde me encontrar • @brunoborges • plus.google.com/+BrunoBorges Bruno Borges
  • 3. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.4 Agenda • Nashorn Overview • Nashorn e Java • Nashorn usando o comando jjs • Shell Scripting com Nashorn • Nashorn e JavaFX
  • 4. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.5 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
  • 5. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.6 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
  • 6. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.7 NASHORN E JAVA
  • 7. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.8 Nashorn e Java 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
  • 8. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.9 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
  • 9. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.10 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
  • 10. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.11 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
  • 11. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.12 NASHORN USANDO JJS
  • 12. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.13 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
  • 13. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.14 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 5
  • 14. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.15 Nashorn usando jjs Exemplo 6
  • 15. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.16 SHELL SCRIPTING COM NASHORN
  • 16. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.17 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
  • 17. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.18 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
  • 18. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.19 NASHORN E JAVAFX
  • 19. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.20 Nashorn e JavaFX • Método bem natural de trabalhar com JavaFX • Nashorn pode ser invocado a partir de FXML • FXML é uma opção, em formato XML, para definir interfaces em JavaFX • Modelo familiar para desenvolvedores de conteúdo Nashorn foi feito para o JavaFX
  • 20. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.21 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
  • 21. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.22 Recapitulando • Forte integração com Java • Pode ser usado a partir do Java • Nova ferramenta por linha de comando jjs • Supore a Shell Scripting • Suporte completo ao JavaFX
  • 22. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.23 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: nashorn-dev@openjdk.java.net Java 8 e outros sites youtube.com/java blogs.oracle.comjava facebook.com/ilovejava @java @javaembedded nighthacking.com
  • 23. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.25 The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
  • 24. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.26
  • 25. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.27

Related Documents