This post explains about bindings and how we can use them to pass java objects to the script code. Java Scripting API provides map data structure which can be accessed, modified using Bindings Interface. The map entry’s key matches with the variable name in the script and their value can be a primitive value or…… Continue reading Global Scope and Engine Scope Bindings
Tag: Java
Default Interface Example
Java 8 introduces a new feature called Default Interface. This feature allows interface creator to provide default implementations for methods, so that concrete classes that implementing the interface don’t have to provide implementations for those methods. The below code gives you an example of how to create default interface and how to use them. Interface…… Continue reading Default Interface Example
Creating a file archiver in zip format
This post explains how to create file archiver in WinZip format. WinZip is a file archiver and compression tool. Whenever a zip file is created by default the file is compressed and archived together in a single file. We can change the default behaviour (i.e., compression) by setting the level property in ZipOutputStream. The below…… Continue reading Creating a file archiver in zip format
Creating a zip file of a folder with CRC32 checksum
This post explains how to create a zip file of a folder (also containing sub folders) with CRC32 checksum. The below code is similar to the previous post “Creating zip file of a folder” with minor change in main method. The main method code snippet from the previous post as shown below File folder =…… Continue reading Creating a zip file of a folder with CRC32 checksum
Executing compiled script
In the previous posts under “Java Scripting”, we used ScriptEngine eval method to execute scripts stored in a string variable or file. The ScriptEngine’s eval methods takes the script compiles to intermediate code and execute it immediately. If we have a script which has to be executed repeatedly, then instead of compiling and executing it…… Continue reading Executing compiled script
Combining Predicates
This post explains how we can combine two or more predicates using logical operations as mentioned below 1) and 2) or 3) negate Below is an example Person package Function; public class Person { private String ssn; private String fname; private String lname; private String description; private int age; public String getSsn() { return ssn;…… Continue reading Combining Predicates
Java Predicate
Java provides a new interface named Predicate which is a functional interface. A functional interface is an interface which will have exactly one abstract method in addition to one or more default methods. In the case of Predicate interface, the abstract method is test method whose signature is as shown below boolean test(T t) In…… Continue reading Java Predicate
Getting access to all printers configured in machine
This post explains how to programmatically get access to all printers configured in a machine. Java provides lookup facitlity through PrintServiceLookup class. This class has static methods through which we can get a reference to all the printers. We use the lookupPrintServices method in the PrintServiceLookup class. The api definition is as shown below public…… Continue reading Getting access to all printers configured in machine
Executing scripts written in a file using java
This post explain how to execute javascript code stored in a file. We create an instance of FileReader representing the physical file and pass it to eval method of an instance of ScriptEngine. Please refer to previous posts to know more about ScriptEngine and ScriptEngineManager The javascript code in the file that has to be…… Continue reading Executing scripts written in a file using java
Executing scripts using java
This post explain how to execute code written in scripting language like php or javascript etc in java. The scripts are executed by calling eval on an instance of ScriptEngine interface. An instance of ScriptEngine is used to represent scripting language specific engines. For example in case of JavaScript, Mozilla Rhino provided out of the…… Continue reading Executing scripts using java