I want to execute java commands interactively from shell: is there a way to do so?
8 Answers
Not an interactive interpreter or a shell, but consider Eclipse scrapbook pages a possible option.
The Java development toolkit (JDT) contributes a scrapbook facility that can be used to experiment and evaluate Java code snippets before building a complete Java program. Snippets are edited and evaluated in the Scrapbook page editor, with resultant problems reported in the editor.
From a Java scrapbook editor, you can select a code snippet, evaluate it, and display the result as a string. You can also show the object that results from evaluating a code snippet in the debuggers' Expressions View.
Bonus: the scrapbook is an Eclipse default feature, so it's not required to install anything you don't already have.
Comments
JShell
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. It was introduced in JDK 9. JShell is a Read-Evaluate-Print Loop tool (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line.
See Java Shell User’s Guide by Oracle. See Read–eval–print loop at Wikipedia.
The IntelliJ IDE offers a JShell console.
JBang is a scripting tool for Java that uses the JShell API under the hood for its simplified syntax and REPL features, allowing script execution with .java or .jsh files.
Comments
I would recommend using DrJava http://drjava.org/. It could serve your purpose
2 Comments
No, it isn't possible (as far as I have found) to write and run arbitrary java snippets interactively from the command line.
I was looking for something similar a few years ago. There's BeanShell, and JDistro which have some elements of a pure-Java shell. The closest I ever found was jsh which was somebody's university project, as I recall, never met with any popularity, and was abandoned.
1 Comment
I think this person would like to be able to do something like the following hypothetical example java --cmd 'int x=5; System.out.print(x);'
You can write your own program, let's call it java-snippet, which has a single command-line argument string called code. Insert the code snippet into the of temporary file.
...main(...) {
//vvv your program inserts code here vvv
//INSERT_CODE_MARKER
//^^^ ^^^
}
Then your java-snippet program compiles the temporary file and immediately runs it.
[edit: it seems the original poster did not in fact want this, but wants an interactive java interpreter -- leaving this answer here for posterity]
Comments
The Java Playground
Oracle provides free-of-cost an interactive website where you can type and execute snippets of Java code. Visit: The Java Playground.
java --cmd 'int x=5; System.out.print(x);'jshellin terminal that should do.. you can read more on jshell here: docs.oracle.com/javase/9/jshell/…