• Complain

Sharan - Scripting in Java Integrating with Groovy and JavaScript

Here you can read online Sharan - Scripting in Java Integrating with Groovy and JavaScript full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. City: Berkeley;CA, year: 2014, publisher: Apress, Imprint, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Sharan Scripting in Java Integrating with Groovy and JavaScript
  • Book:
    Scripting in Java Integrating with Groovy and JavaScript
  • Author:
  • Publisher:
    Apress, Imprint
  • Genre:
  • Year:
    2014
  • City:
    Berkeley;CA
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Scripting in Java Integrating with Groovy and JavaScript: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Scripting in Java Integrating with Groovy and JavaScript" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Scripting in Java teaches you how to use the Java Scripting API and JavaScript to execute scripts and take advantage of the features of a scripting language while developing Java applications. The book also covers topics that enable scripting languages to take advantage of Java features and the Java class library, including the new Java Collections and JavaFX 8 APIs. Most of the examples in this book use JavaScript on the Nashorn engine. Author Kishori Sharan will show you scripts in JavaScript to demonstrate its power and use in your Java applications. Some of the examples use the jrunscript and jjs command-line tools. Furthermore, debugging is discussed to equip you for situations when or if you encounter any issues with this kind of Java scripting. After reading and using this book, you will have most of what you need to do scripting in Java.

Sharan: author's other books


Who wrote Scripting in Java Integrating with Groovy and JavaScript? Find out the surname, the name of the author of the book and a list of all author's works by series.

Scripting in Java Integrating with Groovy and JavaScript — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Scripting in Java Integrating with Groovy and JavaScript" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Kishori Sharan 2014
Kishori Sharan Scripting in Java 10.1007/978-1-4842-0713-0_1
1. Getting Started
Kishori Sharan 1
(1)
AL, United States
In this chapter, you will learn:
  • What scripting in Java is
  • How to execute your first script from Java
  • How to use other scripting languages such as JRuby, Jython from Java
  • javax.script API
  • How script engines are discovered and instantiated
What Is Scripting in Java?
Some believe that the Java Virtual Machine (JVM) can execute programs written only in the Java programming language. However, that is not true. The JVM executes language-neutral bytecode. It can execute programs written in any programming language, if the program can be compiled into Java bytecode.
A scripting language is a programming language that provides you with the ability to write scripts that are evaluated (or interpreted) by a runtime environment called a script engine (or an interpreter). A script is a sequence of characters that is written using the syntax of a scripting language and used as the source for a program executed by an interpreter. The interpreter parses the scripts, produces intermediate code, which is an internal representation of the program, and executes the intermediate code. The interpreter stores the variables used in a script in data structures called symbol tables .
Typically, unlike in a compiled programming language, the source code (called a script) in a scripting language is not compiled but is interpreted at runtime. However, scripts written in some scripting languages may be compiled into Java bytecode that can be run by the JVM.
Java 6 added scripting support to the Java platform that lets a Java application execute scripts written in scripting languages such as Rhino JavaScript, Groovy, Jython, JRuby, Nashorn JavaScript, and so on. Two-way communication is supported. It also lets scripts access Java objects created by the host application. The Java runtime and a scripting language runtime can communicate and make use of each others features.
Support for scripting languages in Java comes through the Java Scripting API. All classes and interfaces in the Java Scripting API are in the javax.script package.
Using a scripting language in a Java application provides several advantages:
  • Most scripting languages are dynamically typed, which makes it simpler to write programs.
  • They provide a quicker way to develop and test small applications.
  • Customization by end users is possible.
  • A scripting language may provide domain-specific features that are not available in Java.
Scripting languages have some disadvantages as well. For example, dynamic typing is good to write simpler code; however, it turns into a disadvantage when a type is interpreted incorrectly and you have to spend a lot of time debugging it.
Scripting support in Java lets you take advantage of both worlds: it allows you to use the Java programming language for developing statically typed, scalable, and high-performance parts of the application and use a scripting language that fits the domain-specific needs for other parts.
I will use the term script engine frequently in this book. A script engine is a software component that executes programs written in a particular scripting language. Typically, but not necessarily, a script engine is an implementation of an interpreter for a scripting language. Interpreters for several scripting languages have been implemented in Java. They expose programming interfaces so a Java program may interact with them.
JDK 7 was cobundled with a script engine called Rhino JavaScript. JDK 8 replaced the Rhino JavaScript engine with a lightweight, faster script engine called Nashorn JavaScript. This book discusses Nashorn JavaScript, not Rhino JavaScript. Please visit www.mozilla.org/rhino for more details on Rhino JavaScript documentation. If you want to migrate programs written with Rhino JavaScript to Nashorn, please visit the Rhino Migration Guide at https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide . If you are interested in using Rhino JavaScript with JDK 8, visit the page at https://wiki.openjdk.java.net/display/Nashorn/Using+Rhino+JSR-223+engine+with+JDK8 .
Java includes a command-line shell called jrunscript that can be used to run scripts in an interactive mode or a batch mode. The jrunscript shell is scripting-language-neutral; the default language is Rhino JavaScript in JDK 7 and Nashorn in JDK 8. I will discuss the jrunscript shell in detail in .
Java can execute scripts in any scripting language that provides an implementation for a script engine. For example, Java can execute scripts written in Nashorn JavaScript, Rhino JavaScript, Groovy, Jython, JRuby, and so on. Examples in this book use Nashorn JavaScript language.
In this book, the terms Nashorn, Nashorn Engine, Nashorn JavaScript, Nashorn JavaScript Engine, Nashorn Scripting Language, and JavaScript have been used synonymously.
The Nashorn scripting engine can be invoked in two ways:
  • By embedding the engine in the JVM
  • By using the jjs command-line tool
In this chapter, I will discuss both ways of using the Nashorn script engine.
Executing Your First Script
In this section, you will use Nashorn to print a message on the standard output. You will access the Nashorn engine from Java code. The same steps can be used to print a message using any other scripting languages, with one difference: you will need to use the scripting language-specific code to print the message. You need to perform three steps to run a script in Java:
  • Create a script engine manager.
  • Get an instance of a script engine from the script engine manager.
  • Call the eval() method of the script engine to execute a script.
A script engine manager is an instance of the ScriptEngineManager class. You can create a script engine, like so:
// Create a script engine manager
ScriptEngineManager manager = new ScriptEngineManager();
An instance of the ScriptEngine interface represents a script engine in a Java program. The getEngineByName(String engineShortName) method of a ScriptEngineManager is used to get an instance of a script engine. To get an instance of the Nashorn engine, use JavaScript as the short name of the engine as shown:
// Get the reference of a Nashorn engine
ScriptEngine engine = manager.getEngineByName("JavaScript");
Tip
The short name of a script engine is case-sensitive. Sometimes a script engine has multiple short names. Nashorn engine has the following short names: nashorn , Nashorn , js , JS , JavaScript , javascript , ECMAScript , ecmascript . You can use any of the short names of an engine to get its instance using the getEngineByName() method of the ScriptEngineManager class.
In Nashorn, the print() function prints a message on the standard output and a string literal is a sequence of characters enclosed in single or double quotes. The following snippet of code stores a script in a String object that prints Hello Scripting! on the standard output:
// Store a Nashorn script in a string
String script = "print('Hello Scripting!')";
If you want to use double quotes to enclose the string literal in Nashorn, the statement will look as shown:
// Store a Nashorn script in a string
String script = "print(\"Hello Scripting!\")";
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Scripting in Java Integrating with Groovy and JavaScript»

Look at similar books to Scripting in Java Integrating with Groovy and JavaScript. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Scripting in Java Integrating with Groovy and JavaScript»

Discussion, reviews of the book Scripting in Java Integrating with Groovy and JavaScript and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.