Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.FileWriter;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        File outputFile = new File("jsoutput.txt");
        System.out.println("Script output will be written to " + outputFile.getAbsolutePath());

        FileWriter writer = new FileWriter(outputFile);
        ScriptContext defaultCtx = engine.getContext();
        defaultCtx.setWriter(writer);
        String script = "print('Hello custom output writer')";
        engine.eval(script);
        writer.close();

    }
}