Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/scripting/scriptpad/README.txt
38829 views
1
Scriptpad Sample
2
3
* Introduction
4
5
Scriptpad is a notepad like editor to open/edit/save and run
6
script (JavaScript) files. This sample demonstrates the use of
7
javax.script (JSR-223) API and JavaScript engine that is bundled
8
with JDK 6.
9
10
Scriptpad sample demonstrates how to use Javascript to use Java
11
classes and objects to perform various tasks such as to modify,
12
customize Swing GUI or to connect to a running application and
13
monitor it using JMX (Java Management Extensions) API.
14
15
* How to run Scriptpad?
16
17
Scriptpad can be run with the following command:
18
19
java -jar ./build/scriptpad.jar
20
21
(be sure to use the correct version of java). You can
22
open/edit/save scripts using menu items under "File" menu.
23
To run currently edited script, you can use "Tools->Run" menu.
24
25
For example, you may enter
26
27
alert("hello, world");
28
29
in the editor and run the same with "Tools->Run" menu.
30
You will see an alert box with the message "hello, world".
31
32
In addition to being a simple script editor/runner, scriptpad
33
can be used to connect to a JMX MBean server ("Tools->JMX Connect"
34
menu). User can specify JMX hostname and port. After connecting,
35
user can use "monitoring and management" script functions defined
36
in "mm.js" (see below).
37
38
* Scriptpad Sources
39
40
com.sun.demo.scriptpad.Main class is the entry point of this
41
sample. This class creates ScriptEngine and evaluates few
42
JavaScript "files" -- which are stored as resources (please
43
refer to src/resources/*.js). Actual code for the scriptpad's
44
main functionality lives in these JavaScript files.
45
46
1. conc.js
47
-- simple concurrency utilities for JavaScript
48
49
2. gui.js
50
-- simple GUI utilities for JavaScript
51
52
3. mm.js
53
-- Monitoring and Management utilities for JavaScript
54
55
4. scriptpad.js
56
-- This creates main "notepad"-like GUI for open/edit/save
57
and run script files
58
59
5. Main.js
60
-- This script file can be used under "jrunscript" tool.
61
jrunscript is an experimental tool shipped with JDK (under
62
$JDK_HOME/bin directory). The scriptpad application can be
63
run by the following commands:
64
65
cd ./src/resources
66
$JDK_HOME/bin/jrunscript -f Main.js -f -
67
68
69
* Extending Scriptpad:
70
71
It is possible to extend scriptpad using scripts. There is a global
72
object called "application". This object has 2 fields and a method.
73
74
Fields of the application object:
75
76
frame -> JFrame of the scriptpad
77
editor -> editor pane of the scriptpad
78
79
Method of the application object:
80
81
addTool -> adds a menu item under "Tools" menu
82
83
Example script to add "Tools->Hello" menu item:
84
85
application.addTool("Hello",
86
function() { alert("hello, world"); });
87
88
After running the above script, you can click Tools->Hello menu item
89
and you'll see an alert box.
90
91
Scriptpad customization may also be done by defining a file named
92
"scriptpad.js" under your home directory,. If this file is found,
93
scriptpad loads this file just after initializating everything.
94
In your initialization file, you can additional script functions
95
by "load" function.
96
97
* Script Samples:
98
99
On clicking the menu items under "Examples" menu, scriptpad shows
100
built-in examples in the editor. Also, there are few script samples
101
under the ./src/scripts directory.
102
103
* Monitoring and Management with Scriptpad:
104
105
(1) Start the application with the JMX agent - here's an example of
106
how the Java2D demo is started
107
108
java -Dcom.sun.management.jmxremote.port=1090 \
109
-Dcom.sun.management.jmxremote.ssl=false \
110
-Dcom.sun.management.jmxremote.authenticate=false \
111
-jar $JDK_HOME/demo/jfc/Font2DTest/Font2DTest.jar
112
113
(2) Start scriptpad and click on "Tools->JMX Connect" menu.
114
In the prompt, enter "localhost:1090" to connect to the above
115
program.
116
117
After connecting to a MBeanServer (using "Tools->JMX Connect"),
118
you can run any script that uses functions defined in "mm.js".
119
For example, it is possible to load and run management scripts that
120
are part of JConsole script shell plugin under the directory:
121
122
$JDK_HOME/demo/scripting/jconsole-plugin/src/scripts
123
124