Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/scripting/scriptpad/README.txt
38829 views
Scriptpad Sample12* Introduction34Scriptpad is a notepad like editor to open/edit/save and run5script (JavaScript) files. This sample demonstrates the use of6javax.script (JSR-223) API and JavaScript engine that is bundled7with JDK 6.89Scriptpad sample demonstrates how to use Javascript to use Java10classes and objects to perform various tasks such as to modify,11customize Swing GUI or to connect to a running application and12monitor it using JMX (Java Management Extensions) API.1314* How to run Scriptpad?1516Scriptpad can be run with the following command:1718java -jar ./build/scriptpad.jar1920(be sure to use the correct version of java). You can21open/edit/save scripts using menu items under "File" menu.22To run currently edited script, you can use "Tools->Run" menu.2324For example, you may enter2526alert("hello, world");2728in the editor and run the same with "Tools->Run" menu.29You will see an alert box with the message "hello, world".3031In addition to being a simple script editor/runner, scriptpad32can be used to connect to a JMX MBean server ("Tools->JMX Connect"33menu). User can specify JMX hostname and port. After connecting,34user can use "monitoring and management" script functions defined35in "mm.js" (see below).3637* Scriptpad Sources3839com.sun.demo.scriptpad.Main class is the entry point of this40sample. This class creates ScriptEngine and evaluates few41JavaScript "files" -- which are stored as resources (please42refer to src/resources/*.js). Actual code for the scriptpad's43main functionality lives in these JavaScript files.44451. conc.js46-- simple concurrency utilities for JavaScript47482. gui.js49-- simple GUI utilities for JavaScript50513. mm.js52-- Monitoring and Management utilities for JavaScript53544. scriptpad.js55-- This creates main "notepad"-like GUI for open/edit/save56and run script files57585. Main.js59-- This script file can be used under "jrunscript" tool.60jrunscript is an experimental tool shipped with JDK (under61$JDK_HOME/bin directory). The scriptpad application can be62run by the following commands:6364cd ./src/resources65$JDK_HOME/bin/jrunscript -f Main.js -f -666768* Extending Scriptpad:6970It is possible to extend scriptpad using scripts. There is a global71object called "application". This object has 2 fields and a method.7273Fields of the application object:7475frame -> JFrame of the scriptpad76editor -> editor pane of the scriptpad7778Method of the application object:7980addTool -> adds a menu item under "Tools" menu8182Example script to add "Tools->Hello" menu item:8384application.addTool("Hello",85function() { alert("hello, world"); });8687After running the above script, you can click Tools->Hello menu item88and you'll see an alert box.8990Scriptpad customization may also be done by defining a file named91"scriptpad.js" under your home directory,. If this file is found,92scriptpad loads this file just after initializating everything.93In your initialization file, you can additional script functions94by "load" function.9596* Script Samples:9798On clicking the menu items under "Examples" menu, scriptpad shows99built-in examples in the editor. Also, there are few script samples100under the ./src/scripts directory.101102* Monitoring and Management with Scriptpad:103104(1) Start the application with the JMX agent - here's an example of105how the Java2D demo is started106107java -Dcom.sun.management.jmxremote.port=1090 \108-Dcom.sun.management.jmxremote.ssl=false \109-Dcom.sun.management.jmxremote.authenticate=false \110-jar $JDK_HOME/demo/jfc/Font2DTest/Font2DTest.jar111112(2) Start scriptpad and click on "Tools->JMX Connect" menu.113In the prompt, enter "localhost:1090" to connect to the above114program.115116After connecting to a MBeanServer (using "Tools->JMX Connect"),117you can run any script that uses functions defined in "mm.js".118For example, it is possible to load and run management scripts that119are part of JConsole script shell plugin under the directory:120121$JDK_HOME/demo/scripting/jconsole-plugin/src/scripts122123124