{"id":17994669,"url":"https://github.com/noties/scriptjava","last_synced_at":"2025-03-26T02:31:06.038Z","repository":{"id":34846068,"uuid":"38838540","full_name":"noties/ScriptJava","owner":"noties","description":"Java REPL today","archived":true,"fork":false,"pushed_at":"2016-08-26T12:39:00.000Z","size":209,"stargazers_count":21,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-15T01:28:35.367Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noties.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-09T18:39:12.000Z","updated_at":"2023-07-16T22:57:33.000Z","dependencies_parsed_at":"2022-09-15T07:12:22.683Z","dependency_job_id":null,"html_url":"https://github.com/noties/ScriptJava","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noties%2FScriptJava","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noties%2FScriptJava/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noties%2FScriptJava/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noties%2FScriptJava/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noties","download_url":"https://codeload.github.com/noties/ScriptJava/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245576528,"owners_count":20638125,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-29T20:16:01.941Z","updated_at":"2025-03-26T02:31:05.719Z","avatar_url":"https://github.com/noties.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ScriptJava\nJava REPL today\n\n\nThere are a lot of benefits for a programming language to have a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop). It helps one learn and explore the language features, gives ability to execute fast a code in question without launching IDE or modifiying existing project.\n\n\n\nWriting *whole* Java code in command line seems like an overkill. Creating a class, then main method, then call to `System.out.print()` to execute such simple as `Integer.MAX_VALUE` command seems like a long way. What's why ScriptJava is *not* completely Java REPL. For example, semicolons(`;`) are *mostly* optional. There are a lot of *build-ins* methods, that can save a coder a few keyboard strokes. So, yeah, to know the value of `Integer.MAX_VALUE` you just input `Integer.MAX_VALUE`.\n\n![](https://raw.githubusercontent.com/noties/ScriptJava/master/gif/1.gif)\n![](https://raw.githubusercontent.com/noties/ScriptJava/master/gif/2.gif)\n\n![](https://raw.githubusercontent.com/noties/ScriptJava/master/gif/3.gif)\n![](https://raw.githubusercontent.com/noties/ScriptJava/master/gif/4.gif)\n\n![](https://raw.githubusercontent.com/noties/ScriptJava/master/gif/5.gif)\n![](https://raw.githubusercontent.com/noties/ScriptJava/master/gif/6.gif)\n\n## Build-in functions\n* **`bool()`** -\u003e inspired by python's function. In short: `null`, `0`, or `\"\"`(empty string) returns `false`. Defined in `Bool.java`\n* **`len()`** - \u003e inspired by python's function. Unifies calls to get length of different objects. Defined in `Length.java`\n* **`str()`** - \u003e returns *good* object representation as a String. Defined in `Str.java`. In case of a file (if exists) returns file contents as a String\n* **`print()`** \u003e do as expected. Accepts multiple objects: `print(1, 2, 3, 4)`. Every object's string representation will be obtained via `str()` call\n* **`printf()`** -\u003e Helper method to call `System.out.printf()`. Won't change the objects representation\n* **`list()`** -\u003e creates a list from supplied arguments, `list(1, 2, 3, 4)`\n* **`map()`** -\u003e original signature of this method is a bit scary: `map(String[] keys, Object[] values)`, but with the help of the substitution command becomes more friendly: `map(key1: 1, key2: 2)`\n* **`bin()`** -\u003e returns binary representation of an integer value\n* **`hex()`** -\u003e return hex representation of an integer value\n* **`date()`** -\u003e helper method to call `new Date()`\n* **`now()`** -\u003e helper method to call `System.currentTimeMillis()`\n* **`exec()`** -\u003e executes supplied command as a String in a different process, returns String (the process output): `exec(\"java -version\")`\n* **`file()`** -\u003e this method has different signatures. Called without parameters returns current execution folder. Called with a String as a parameter -\u003e constructs File object with specified path\n* **`del()`** -\u003e removes file, takes a File object or a path\n* **`write()`** -\u003e takes a File object and it's contents as a String. Will write contents to a file\n* **`json()`** -\u003e Super primitive json parser that returns `Element` with the following methods: `key(String)` -\u003e retrieves an JSON object by the key; `at(int)` -\u003e retrieves a value from JSON array at give index; `get()` -\u003e returns JSON primitive. Please note that exception will be thrown if method called on wrong JSON type (for example `at` will be called on JSON object)\n* **`get()`** -\u003e executes GET http request, returns response as a String\n* **`post()`** -\u003e executes POST http request, returns response as a String\n* **`range()`** -\u003e creates a *range* of integers to be easily interated, `for (int i: range(10))`. Returns `int[]`. has 3 signatures: `range(end)`, `range(start, end)`, `range(start, end, step)`\n* **`dict()`** -\u003e returns object's meta info (class name, parent class name, implements), fields and values. Has ability to return also info about methods\n* **`type()`** -\u003e returns human readable Type info (redirects call to `str()`)\n* **`toMap()`** -\u003e converts an object into a `Map\u003cString, Object\u003e` (no recursive calls)\n* **`fromMap()`** -\u003e constructs an object from supplied `Map\u003cString, Object\u003e`\n* **`set()`** -\u003e tries to modify objects field given the name and value. Can modify `final` fields\n* **`ni()`** -\u003e simple call for `Class.newInstance()`\n* **`store()`** -\u003e stores a variable between script recompilations (actually can be useful only when removing previously retrieved value with `ret` call)\n* **`ret()`** -\u003e retirievs previously stored valiable or tries to obtain it if not present\n* **`copy()`** -\u003e copies supplied argument to the system clipboard and returns the value unmodified. `copy(exec(\"echo HelloThere | cowsay -e --\"))` will copy result value of the `exec` call and print it.\n\nAlso, by default `java.util.Math` is statically imported, so **`pow()`**, **`sin()`**, **`cos()`**, etc are available\n\n## Special commands\n* **`import `** -\u003e adds an import statement for a script, for example `import java.text.*`\n* **`clean`** -\u003e completely cleans script\n* **`quit`** -\u003e exits from REPL\n* **`bytecode`** -\u003e prints bytecode for current script (if `javap` is present in PATH)\n\n\n## Substitutions\n* **`map(key1: 1, key2: 2, key3: \"some string\")`** this (invalid in Java syntax) will be substituted new a call to build-in function map: `map(new String[] { \"key1\", \"key2\", \"key3\" }, new Object[] { 1, 2, \"some string\" })`\n* **`ret(\"key\", someCommand())`** -\u003e will be sustituted with: `ret(\"key\", new Provider() { Object provide() { return someCommand(); } })`. This is done to skip initialization command if value is already defined, else the Provider object will be called to retrieve the value\n\n## Default imports\nExcept build-in functions these packages are imported by default:\n* **`java.util.*`**\n* **`java.text.*`**\n* **`java.io.*`**\n\n## Custom libraries\nRight now if there is a folder called `libs` inside execution directory, all jars that are inside it will be added to the script class path. Anyway you still need to import desired class first (here is no surprices).\n\n## Drawbacks\nAs we cannot achive completely *scripting* behavior from Java, there are certain things to be aware of. First, everytime you hit `Enter` the whole script is compiled from scratch. That's why all the variables will be set anew. For example:\n```java\nDate d = date() // hit Enter\nd // will print the *current* date, not the date value, that we obtained earlier\n```\nThere is a woraround for this: build-in function `ret`:\n```java\nDate d = ret(\"d\", date())\nd // will print the first obtained value\n```\n\n## Installation\nBefore launching the executable make sure, that there is `javac` in the system PATH. Most likely that you already have it there. Also, if you wish to use `bytecode` function (that prints Java bytecode of compiled script) make sure that there is also a `javap` in the PATH.\n\n\n## Building\nThe main project has no dependencies, so it's a straight-forward process. However, building from IDE (when project is created from the source code)\nwon't work, as current functionality requires the project's jar in the execution directory (due to build-in imports \u0026 IScript)\n\nTests require `Gson` (for json testing)\n\n\n## License\n\n```\n  Copyright 2016 Dimitry Ivanov (cr@dimitryivanov.ru)\n\n  Licensed under the Apache License, Version 2.0 (the \"License\");\n  you may not use this file except in compliance with the License.\n  You may obtain a copy of the License at\n\n      http://www.apache.org/licenses/LICENSE-2.0\n\n  Unless required by applicable law or agreed to in writing, software\n  distributed under the License is distributed on an \"AS IS\" BASIS,\n  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  See the License for the specific language governing permissions and\n  limitations under the License.\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoties%2Fscriptjava","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoties%2Fscriptjava","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoties%2Fscriptjava/lists"}