Java Scripting Programmer's Guide - Oracle

Java Platform, Standard Edition

Java Scripting Programmer's Guide

Release 12

F13636-01 March 2019

Java Platform, Standard Edition Java Scripting Programmer's Guide, Release 12

F13636-01

Copyright ? 2015, 2019, Oracle and/or its affiliates. All rights reserved.

This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.

The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.

If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable:

U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agencyspecific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government.

This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications.

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group.

This software or hardware and documentation may provide access to or information about content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services unless otherwise set forth in an applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services, except as set forth in an applicable agreement between you and Oracle.

Contents

Preface

Audience

iv

Documentation Accessibility

iv

Related Documents

iv

Conventions

iv

1 Scripting Languages and Java

2 The Java Scripting API

The JavaScript Package

2-1

How to Use the Java Scripting API to Embed Scripts

2-1

Java Scripting API Examples with Java Classes

2-2

3 Using Java from Scripts

Accessing Java Classes

3-1

Importing Java Packages and Classes

3-2

Using Java Arrays

3-3

Implementing Java Interfaces

3-4

Extending Abstract Java Classes

3-5

Extending Concrete Java Classes

3-6

Accessing Methods of a Superclass

3-7

Binding Implementations to Classes

3-7

Selecting Method Overload Variant

3-8

Mapping Data Types

3-9

Passing JSON Objects to Java

3-9

iii

Preface

Preface

This document provides an overview of the scripting features in Java Platform, Standard Edition (Java SE).

Audience

This document is intended for Java application programmers who want to execute code written in scripting languages. It is assumed that the you are familiar with Java and a version of the ECMAScript language standard (JavaScript).

Documentation Accessibility

For information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at ? ctx=acc&id=docacc.

Access to Oracle Support

Oracle customers that have purchased support have access to electronic support through My Oracle Support. For information, visit lookup?ctx=acc&id=info or visit if you are hearing impaired.

Related Documents

See the Java Scripting API specification, the javax.script package.

Conventions

The following text conventions are used in this document:

Convention boldface italic

monospace

Meaning

Boldface type indicates graphical user interface elements associated with an action, or terms defined in text or the glossary.

Italic type indicates book titles, emphasis, or placeholder variables for which you supply particular values.

Monospace type indicates commands within a paragraph, URLs, code in examples, text that appears on the screen, or text that you enter.

iv

1

Scripting Languages and Java

This section describes the characteristics of scripting languages and how they can be used by Java programmers. Scripting languages are programming languages that support the ability to write scripts. Unlike source files for other programming languages that must be compiled into bytecode before you run them, scripts are evaluated by a runtime environment (in this case, by a script engine) directly. Most scripting languages are dynamically typed. This enables you to create new variables without declaring the variable type (the interpreter assigns the type based on the type of the object associated with the variable), and you can reuse the same variable for objects of different types (type conversion is performed automatically). Scripting languages generally have simple syntax; they allow complex tasks to be performed in relatively few steps. Although scripting languages are usually interpreted at runtime, they can be compiled into Java bytecode that can then be executed on the Java Virtual Machine (JVM). Scripting languages can be faster and easier to use for certain problems, so it is sometimes chosen by developers of Java applications. However, if you write your Java application in a scripting language, then you lose the benefits of the Java language (such as type safety and access to the class library). Java Specification Request (JSR) 223: Scripting for the Java Platform addresses the issue of integrating Java and scripting languages. It defines a standard framework and application programming interface (API) to embed scripts in your Java applications and access Java objects from scripts. By embedding scripts in your Java code, you can customize and extend the Java application. For example, you can have configuration parameters, business logic, math expressions, and other external parts written as scripts. When developing your Java application, you do not need to choose the scripting language. If you write your application with the Java Scripting API (defined by JSR 223), then users can write scripts in any language compliant with JSR 223. See The Java Scripting API. When writing a script in a language compliant with JSR 223, you have access to the entire standard Java library. See Using Java from Scripts.

1-1

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download