As you may know Java is not a scripting language, but it is possible to execute scripts written in scripting languages like JavaScript or Groovy using the Nashorn Scripting Engine.
Let us see a very basic "hello world" example:
package org.code2care.java.examples;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String javaScript = "var message = 'Hello Java from JavaScript!'; print(message);";
engine.eval(javaScript);
}
}
The Java Scripting API is a built-in feature of Java that allows you to execute scripts written in various languages, including JavaScript, Groovy, Ruby, and Python.
ScriptEngine is the class that you will create an instance of passing in the scripting language that you want to use, and make use of the eval() method to execute the script.
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- How to install Zsh shell - HowTos
- How to rename package name in Android Studio - Android-Studio
- How to check uptime of Linux/Unix/macOS system/server using console command? - Linux
- Terminal Command to get the list of available shells in macOS - MacOS
- Android : DeviceMonitor] Sending Tracking request failed! Error - Android
- Native getClass() method from java.lang.Object Class Explained with examples. - Java
- Spell check not working in Gmail : Mac OS X - Mac-OS-X
- Change the default login shell on macOS Ventura 13.0 - MacOS