script

Import package in script

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 2nd, 2013
0 119 1 minute read

In this example we shall show you how to import a package in script. We are using theScriptEngine interface, that provides methods for basic scripting functionality. To import a package in script one should perform the following steps:

  • Create aStringBuilder to build the script. Append to it all the commands to be executed, along with the import package command. Get the String representation of the StringBuilder, to be used as the script.
  • Create a newScriptEngineManager. 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 thegetEngineByExtension(String extension) API method to look up and create aScriptEngine for the js extension.
  • Use theeval(String script) method to execute the script,

as described in the code snippet below.  

package com.javacodegeeks.snippets.core;import javax.script.ScriptEngineManager;import javax.script.ScriptEngine;import javax.script.ScriptException; public class ImportPackage {    public static void main(String[] args) {        // Create script engine manager and set js engine  ScriptEngineManager manager = new ScriptEngineManager();  ScriptEngine engine = manager.getEngineByExtension("js");   try {engine.eval(getScript());  } catch (ScriptException e) {e.printStackTrace();  }    }     // Get script method that includes the import of java.util package    private static String getScript() {  StringBuilder sb = new StringBuilder();  sb.append("importPackage(java.util);");  sb.append("");  sb.append("var today = new Date();");  sb.append("println('Today is ' + today);");  return sb.toString();    }}

Output:

Today is Sat Aug 11 2012 20:06:08 GMT+0300 (EEST)

  
This was an example of how to import a package with a script in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 2nd, 2013
0 119 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.

Related Articles

Bipartite Graph

Get script engine by name

November 11th, 2012
Bipartite Graph

Modify Java object in script

November 11th, 2012
Bipartite Graph

Evaluate a simple script

November 11th, 2012
Bipartite Graph

Evaluate a script file

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.