0

Python has a nice feature where I just type "python" in sh and I get an interactive way of testing Python code.

How do I do that with Java?

I'm on Mac 10.9 if that makes any difference.

Trindaz
  • 17,029
  • 21
  • 82
  • 111

4 Answers4

1

Java does not have a "CLI mode" like Python.

There's BeanShell which is probably similar to what you mean, but it seems that has not been updated for a long time.

Jesper
  • 202,709
  • 46
  • 318
  • 350
1

There is no such thing built into Java. However Beanshell might be what you are looking for:

You can use BeanShell interactively for Java experimentation and debugging as well as to extend your applications in new ways.

http://www.beanshell.org/intro.html

It however doesn't look like there's much development going on these days and other approaches (such as the mentioned Jython one) might be more practical.

reto
  • 9,995
  • 5
  • 53
  • 52
0

What you are looking for is called a "REPL" which is short for "Read–eval–print loop".

Since Java 9 there is a REPL bundled with Java, it is called jshell.

$ jshell
|  Welcome to JShell -- Version 11.0.2
|  For an introduction type: /help intro

jshell> int x = 4
x ==> 4

jshell> int y = 9
y ==> 9

jshell> x + y
$3 ==> 13

jshell> /exit
|  Goodbye
$
MichielB
  • 4,181
  • 1
  • 30
  • 39
-2

Python is scripting language means that it is not compiled language but it is interpreted language.

Wikipedia definition for Interpreted language

While Java is compiled language and you will never find such a feature link in python interactive mode, becuase you must write the whole application structure main class to make your app run. So instead you can use an IDE like eclipse , or netbeans.

Compiled Language

And also you can refere to stackoverflow link Compiled vs. Interpreted Languages

Community
  • 1
  • 1
Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46
  • 1
    Slight digression, but related question to this point: If it is impossible as you say, then how is it possible for this answer to exist? http://stackoverflow.com/a/2605051/193601 – Trindaz Jan 23 '14 at 09:01
  • 3
    This distinction is irrelevant. Scala is *also* a "compiled language" and yet provides a [REPL](http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) - other counter-examples to this reply include SML, Haskell and even C# in an "Interactive Window" when debugging. Conversely, Perl is a "scripting language" and has no core REPL support. There is also [BeanShell](http://www.beanshell.org/) for Java which is sorta like a Java REPL. – user2864740 Jan 23 '14 at 09:04