Home » Core Java » script » Evaluate a script file

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Evaluate a script file

With this example we are going to demonstrate how to evaluate a simple script file in Java. We are using the ScriptEngine interface that provides methods for the basic scripting functionality. In short, to evaluate a script file you should:

  • Create a new ScriptEngineManager. The ScriptEngineManager implements a discovery and instantiation mechanism for ScriptEngine classes and also maintains a collection of key/value pairs storing state shared by all engines created by the Manager.
  • Use the getEngineByName(String shortName) method to look up and create a ScriptEngine for a given name.
  • Create an instance of File object that points to the scripting file.
  • Create a FileReader to read the file.
  • Use the eval(Reader reader) method to execute the script provided by the Reader.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.File;
import java.io.Reader;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class EvaluateScriptExample {
    public static void main(String[] args) {

  // Obtain JavaScript Engine

  ScriptEngineManager manager = new ScriptEngineManager();

  ScriptEngine engine = manager.getEngineByName("ECMAScript");

  try {

  	//Create an instance of File object that point to scripting file

File script = new File("sample.js");

//create a FileReader to read the file to be passed to the ScriptEngine.eval() method.

Reader reader = new FileReader(script);

engine.eval(reader);

  } catch (FileNotFoundException e) {

e.printStackTrace();

  } catch (ScriptException e) {

e.printStackTrace();

  }
    }
}

 
This was an example of how to evaluate a simple script file in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of