I want to execute java commands interactively from shell: is there a way to do so?
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.
I would recommend using DrJava http://drjava.org/. It could serve your purpose
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.
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]
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.
https://docs.oracle.com/en/java/javase/11/jshell/introduction-jshell.html
© 2022 - 2024 — McMap. All rights reserved.
java --cmd 'int x=5; System.out.print(x);'
– Thigpenjshell
in terminal that should do.. you can read more on jshell here: docs.oracle.com/javase/9/jshell/… – Gervase