33

I want to execute java commands interactively from shell: is there a way to do so?

19
  • 2
    uhuh? "Write the method itself in cmd"? Do you mean you want an interactive interpreter? Commented May 4, 2011 at 22:37
  • 2
    In a way similar to the REPL of Python of Lisp? Commented May 4, 2011 at 22:39
  • 1
    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);' Commented May 4, 2011 at 22:43
  • 3
    @Isaac Truett: huhu? I love Java, I'd just hate to write a bunch of "class X { public static void main(...) { ... } } in an interactive shell just to evaluate a command. Commented May 4, 2011 at 23:06
  • 4
    What you want my friend is jshell. just type jshell in terminal that should do.. you can read more on jshell here: docs.oracle.com/javase/9/jshell/… Commented Oct 28, 2020 at 17:26

8 Answers 8

7

The closest thing I'm aware of is BeanShell.

Sign up to request clarification or add additional context in comments.

Comments

6

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

4

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

3

I would recommend using DrJava http://drjava.org/. It could serve your purpose

2 Comments

Handy tool. You do have to stub out a basic console app, though.
The Interactions pane is a little useful, actually what I want is something just like SnippetCompiler for C# and LinqPad for LINQ. The interactions pane is not very handy to write a chunk of code.
3

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

Outdated Answer now.
1

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

0

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.

Comments

-2

(for futurs viewers)
if you need evaluate a function or check a value !
Probably you have to make a unit test with this info / function / evaluation
and later you will can test value or you will be able to adapt solution in a new unit test
(sorry for my poor english)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.