Java Reference
In-Depth Information
you can replace the code in the try-catch block with the following snippet of code and
the program will work the same; you need to remove two import statements that imports
classes from the java.io package:
try {
// Execute the script in the file
engine.eval("load('" + scriptFileName + "');"); // First time, add a
// breakpoint here
}
catch (ScriptException e) {
e.printStackTrace();
}
Listing 13-2. Debugging Scripts That Are Called from Java Code
// PrimeTest.java
package com.jdojo.script;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class PrimeTest {
public static void main(String[] args) {
// Construct the script file path
String scriptFileName = "primetest.js";
Path scriptPath = Paths.get(scriptFileName);
// Make sure the script file exists. If not, print the full
// path of the script file and terminate the program.
if (!Files.exists(scriptPath)) {
System.out.println(scriptPath.toAbsolutePath() +"
"does not exist.");
return;
}
// Get the Nashorn script engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Search WWH ::




Custom Search