- Loading...
...
| Code Block | ||
|---|---|---|
| ||
// JavaImporter constructor accepts one or more Java Package objects
var imports = new JavaImporter(java.util, java.io);
// a JavaImporter can be used as a "with" expression object
with(imports) {
// classes from java.util and java.io packages can
// can be accessed by unqualified names
var map = new HashMap(); // refers to java.util.HashMap
map.put("js", "javascript");
map.put("java", "java");
map.put("cpp", "c++");
print(map);
var f = new File("."); // refers to java.io.File
print(f.getAbsolutePath());
} |
$ARG (-scripting mode only)
This global object can be used to access the arguments passed to the script, similar to how the arguments object is used, for example:
| Code Block | ||
|---|---|---|
| ||
>> jjs -scripting -- arg1 arg2 arg3
jjs> $ARG
arg1,arg2,arg3
jjs> $ARG[1]
arg2
|
$OPTIONS (-scripting mode only)
...
| Code Block | ||
|---|---|---|
| ||
// print $JAVA_HOME and $PATH from the OS shell print($ENV["JAVA_HOME"]) print($ENV["PATH"]) print($ENV.JAVA_HOME) print($ENV.PATH) |
readLine (-scripting mode only)
This function reads one line of input from stdin and sends it to stdout, or you can assign the result to a variable. You can also pass a string to the readLine() function to get a prompt line as in the following example:
| Code Block | ||
|---|---|---|
| ||
jjs> var name = readLine("What is your name? ")
What is your name? Bob
jjs> print("Hello, ${name}!")
Hello, Bob!
jjs>
|
readFully (-scripting mode only)
This function reads the entire contents of a file passed in as a string argument and sends it to stdout, or you can assign the result to a variable.
| Code Block | ||
|---|---|---|
| ||
jjs> readFully("text.txt")
This is the contents of the text.txt file located in the current working directory.
|
"Java" global property is a script object that defines useful functions for script-to-Java interface.
...