Nashorn: Novo Motor Javascript no Java SE 8

1,340 views

Published on

Nashorn é o novo motor Javascript presente no Java SE 8, substituindo o antigo Mozila Rhino e que traz além de maior performance para execução de códigos Javascripts, vem com uma série de novas features como integração com JavaFX, Shell Scripting, e muito mais.

Nashorn: Novo Motor Javascript no Java SE 8

  1. 1. 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
  2. 2. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Bruno Borges – Principal Product Manager, Java Evangelist – Oracle Latin America – @brunoborges – bruno.borges@oracle.c om Speaker Bruno Borges Principal Product Manager, Java Evangelist Oracle Latin America @brunoborges bruno.borges@oracle.com
  3. 3. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Rhino (Inglês) = Nashorn (Alemão)
  4. 4. 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
  5. 5. 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
  6. 6. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Nashorn dentro de código Java
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Nashorn usando Java 8 Streams API Exemplo 5
  12. 12. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Nashorn linha de comando – /usr/bin/jjs
  13. 13. 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
  14. 14. 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
  15. 15. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Nashorn e Javascript como Unix Shell Scripting Shell scripts para Unix escritos em Javascript!
  16. 16. 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
  17. 17. 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
  18. 18. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Nashorn e JavaFX Aplicacoes Desktop Escritas em Javascript!
  19. 19. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
  20. 20. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | FX
  21. 21. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
  22. 22. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
  23. 23. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
  24. 24. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
  25. 25. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JavaFX 3D
  26. 26. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 27 Criando Formas e Materiais Primitivos
  27. 27. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Colocando Textura em uma Esfera 28
  28. 28. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Colocando Textura em uma Esfera 29
  29. 29. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Colocando Textura em uma Esfera 30
  30. 30. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | JavaFX Scene Builder bit.ly/javafxdownload
  31. 31. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Your First JavaFX App for RaspberryPi
  32. 32. 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
  33. 33. 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
  34. 34. 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: nashorn-dev@openjdk.java.net Java 8 e outros sites youtube.com/jav a blogs.oracle.comjava facebook.com/ilovejava @java @javaembedded nighthacking.com

×