31

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

Basel Shishani
  • 7,735
  • 6
  • 50
  • 67
Mohamed Khamis
  • 7,731
  • 10
  • 38
  • 58
  • 2
    uhuh? "Write the method itself in cmd"? Do you mean you want an interactive interpreter? – akappa May 04 '11 at 22:37
  • 2
    In a way similar to the REPL of Python of Lisp? – David Weiser May 04 '11 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);'` – ninjagecko May 04 '11 at 22:43
  • @ninjagecko: yeah, but it doesn't make even the slightest sense to do such thing in Java, given its verbosity... – akappa May 04 '11 at 22:46
  • For that you write a program in Java, compile, execute it in the command line. And that will behave has you expected. – CRM May 04 '11 at 22:49
  • @akappa "you do not want to" != "doesn't make sense" – Isaac Truett May 04 '11 at 22:57
  • @Isaac Truett: in programming they are pretty much the same thing: if something is fugly, then it indeed doesn't make sense – akappa May 04 '11 at 23:00
  • @akappa Great. Get back to me when you actually find a programming language that some significant portion of the developer world doesn't consider "fugly." – Isaac Truett May 04 '11 at 23:03
  • 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. – akappa May 04 '11 at 23:06
  • @akappa 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. – Isaac Truett May 04 '11 at 23:13
  • @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. – akappa May 04 '11 at 23:20
  • @akappa 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? – Isaac Truett May 04 '11 at 23:24
  • @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. – akappa May 04 '11 at 23:29
  • @akappa You're confusing preference with need again. And your comment about context makes no sense. A shell environment is a context. – Isaac Truett May 04 '11 at 23:32
  • @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...). – akappa May 04 '11 at 23:36
  • I use this for one liners: https://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. – Dave Thomas Oct 24 '16 at 22:40
  • 2
    What you want my friend is jshell. just type `jshell` in terminal that should do.. you can read more on jshell here: https://docs.oracle.com/javase/9/jshell/snippets.htm#JSHEL-GUID-614373AD-8D94-462A-B05E-DFA35DF098C9 – lordvidex Oct 28 '20 at 17:26
  • JShell [link](https://docs.oracle.com/javase/10/jshell/introduction-jshell.htm#JSHEL-GUID-630F27C8-1195-4989-9F6B-2C51D46F52C8) – chance Nov 05 '20 at 08:11
  • as the two comments above state, that exactly JShell (https://docs.oracle.com/en/java/javase/17/jshell/introduction-jshell.html) 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. – math Feb 23 '22 at 13:19

5 Answers5

7

The closest thing I'm aware of is BeanShell.

McDowell
  • 107,573
  • 31
  • 204
  • 267
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.

Paolo
  • 20,112
  • 21
  • 72
  • 113
3

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

jajo87
  • 1,388
  • 2
  • 11
  • 15
  • Handy tool. You do have to stub out a basic console app, though. – Precipitous May 10 '12 at 16:11
  • 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. – zhaorufei May 18 '13 at 09:13
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.

Isaac Truett
  • 8,734
  • 1
  • 29
  • 48
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]

ninjagecko
  • 88,546
  • 24
  • 137
  • 145