Is there an interactive interpreter for Java?
Asked Answered
T

6

32

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

Tidings answered 4/5, 2011 at 22:36 Comment(19)
uhuh? "Write the method itself in cmd"? Do you mean you want an interactive interpreter?Inchoation
In a way similar to the REPL of Python of Lisp?Barabarabarabas
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);'Thigpen
@ninjagecko: yeah, but it doesn't make even the slightest sense to do such thing in Java, given its verbosity...Inchoation
For that you write a program in Java, compile, execute it in the command line. And that will behave has you expected.Vinasse
@Inchoation "you do not want to" != "doesn't make sense"Ploughman
@Isaac Truett: in programming they are pretty much the same thing: if something is fugly, then it indeed doesn't make senseInchoation
@Inchoation Great. Get back to me when you actually find a programming language that some significant portion of the developer world doesn't consider "fugly."Ploughman
@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.Inchoation
@Inchoation The whole point of having an interactive shell that interpreted Java snippets is so that you wouldn't have to do all that. It would be like watch expressions in an IDE; individual statements evaluated immediately in a persistent JVM. What you seem to be envisioning is a real-time compiler/automatic Java executor.Ploughman
@Isaac Truett: yeah, but this pretty much implies defining a new language which is closely tied to Java but isn't Java. BeanShell makes a lot of sense, but it isn't just java, just something (closely) related.Inchoation
@Inchoation Why would you need a new language? Start Eclipse, set a breakpoint, then write Java in a watch expression, and see it executed. Now take that functionality and make an interactive shell out of it (that's the non-trivial part, of course). Why do you need a new language?Ploughman
@Isaac Truett: we need a new language because we were talking about writing stuff in an interactive shell, not in the execution window of an IDE. The point is that, in a "pure" interactive shell, you need to write something which is well-defined in itself and not given a "context". Since interactive shells are something which are used to experiment on-the-fly, you need something comfy - hence the need of something less "heavy" on syntax.Inchoation
@Inchoation You're confusing preference with need again. And your comment about context makes no sense. A shell environment is a context.Ploughman
@Isaac Truett: I'm not confusing anything: programming is all about comfort, otherwise we'll just stick with assembly. And a "shell environement" is a context, but with a whole different meaning (it doesn't define any "structure" relevant to Java, while the command executed in a window is executed inside a method of a class already defined, with a given state. Bad example...).Inchoation
I use this for one liners: gist.github.com/davethomas11/c6a9f0f75ff7cd64f993ca289de060c0 When I want to test a java api. Note the limitations of this, only code you'd want to run in one main function. Not intended to be anything more.Cerement
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/…Gervase
JShell linkMelidamelilot
as the two comments above state, that exactly JShell (docs.oracle.com/en/java/javase/17/jshell/…) answers the OP's question, it should be reopened and answered properly, as nobody searches the whole comments in that list. Therefore I vote to reopen the question and let someone answer this.Botchy
T
7

The closest thing I'm aware of is BeanShell.

Tetany answered 4/5, 2011 at 22:48 Comment(0)
N
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.

Nonjoinder answered 4/5, 2011 at 23:21 Comment(0)
T
3

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

Tauto answered 4/5, 2011 at 22:51 Comment(2)
Handy tool. You do have to stub out a basic console app, though.Insomniac
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.Pursuance
P
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.

Ploughman answered 4/5, 2011 at 22:52 Comment(0)
T
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]

Thigpen answered 4/5, 2011 at 22:49 Comment(0)
B
1

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

Baksheesh answered 15/3 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.