aoqi@0: #// Usage: jjs -scripting javashell.js aoqi@0: aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@962: * aoqi@0: * Redistribution and use in source and binary forms, with or without aoqi@0: * modification, are permitted provided that the following conditions aoqi@0: * are met: attila@962: * aoqi@0: * - Redistributions of source code must retain the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer. attila@962: * aoqi@0: * - Redistributions in binary form must reproduce the above copyright aoqi@0: * notice, this list of conditions and the following disclaimer in the aoqi@0: * documentation and/or other materials provided with the distribution. attila@962: * aoqi@0: * - Neither the name of Oracle nor the names of its aoqi@0: * contributors may be used to endorse or promote products derived aoqi@0: * from this software without specific prior written permission. attila@962: * aoqi@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS aoqi@0: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, aoqi@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR aoqi@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR aoqi@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, aoqi@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, aoqi@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR aoqi@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF aoqi@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING aoqi@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS aoqi@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. aoqi@0: */ aoqi@0: aoqi@0: // Simple Java "shell" with which you can try out aoqi@0: // your few liner Java code leaving imports, main etc. aoqi@0: // And you can leave even compilation as this script aoqi@0: // takes care boilerplate+compile step for you. aoqi@0: aoqi@0: // Java types used aoqi@0: var Arrays = Java.type("java.util.Arrays"); aoqi@0: var BufferedReader = Java.type("java.io.BufferedReader"); aoqi@0: var FileWriter = Java.type("java.io.FileWriter"); aoqi@0: var LocalDateTime = Java.type("java.time.LocalDateTime"); aoqi@0: var InputStreamReader = Java.type("java.io.InputStreamReader"); aoqi@0: var PrintWriter = Java.type("java.io.PrintWriter"); aoqi@0: var ProcessBuilder = Java.type("java.lang.ProcessBuilder"); aoqi@0: var System = Java.type("java.lang.System"); aoqi@0: aoqi@0: // read multiple lines of input from stdin till user aoqi@0: // enters an empty line aoqi@0: function input(endMarker, prompt) { aoqi@0: if (!endMarker) { aoqi@0: endMarker = ""; aoqi@0: } aoqi@0: aoqi@0: if (!prompt) { aoqi@0: prompt = " >> "; aoqi@0: } aoqi@0: aoqi@0: var str = ""; aoqi@0: var reader = new BufferedReader(new InputStreamReader(System.in)); aoqi@0: var line; aoqi@0: while (true) { aoqi@0: System.out.print(prompt); aoqi@0: line = reader.readLine(); aoqi@0: if (line == null || line == endMarker) { aoqi@0: break; aoqi@0: } aoqi@0: str += line + "\n"; aoqi@0: } aoqi@0: return str; aoqi@0: } aoqi@0: aoqi@0: // write the string to the given file aoqi@0: function writeTo(file, str) { aoqi@0: var w = new PrintWriter(new FileWriter(file)); aoqi@0: try { aoqi@0: w.print(str); aoqi@0: } finally { aoqi@0: w.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // generate Java code with user's input aoqi@0: // put inside generated main method aoqi@0: function generate(className) { aoqi@0: var usercode = input(); aoqi@0: if (usercode == "") { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: var fullcode = <