tags:

views:

937

answers:

11

Is there something like python's interactive REPL mode, but for Java? So that I can, for example, type InetAddress.getAllByName( localHostName ) in a window, and immediately get results, without all this public static void nightmare() thing?

Thanks

A: 

Most IDE's have a window called something like "immediate mode" that will allow you to evaluate java code on the fly.

krosenvold
Eclipse has "display" view which is close to what I want. Problem is, it has to be in the context of running application. I have to run an app, stop on breakpoint and only then I can type what I want.
Yoni Roit
+4  A: 

Jython is a python implementation which lets you inspect and interact with Java objects.

>>> from java.net import *
>>> InetAddress.getAllByName("google.com")
array(java.net.InetAddress,[google.com/209.85.171.100, 
                            google.com/74.125.45.100,
                            google.com/74.125.67.100])
speciousfool
+6  A: 
OscarRyz
+1  A: 

The groovy console allows you to do that. It actually was meant to try and test groovy code, but since groovy is a superset of Java, it allows plain Java stuff as well.

I just entered this into the console:

InetAddress.getAllByName('localhost')

and hit CTRL-R, then it returned:

groovy> InetAddress.getAllByName('localhost')

Result: [localhost/127.0.0.1]
Ole
+2  A: 

Clojure provides a REPL you can use.

_ande_turner_
+7  A: 

As an alternative to Groovy, try Beanshell: http://www.beanshell.org/

It is more Java-like and allows you to use Java-syntax directly.

Tim Jansen
is beanshell maintained anymore?
rogerdpack
A: 

Scala also offers an interactive console. I was able to use it to get a result for the expression in your question by fully qualifying InetAddress, as in:

java.net.InetAddress.getAllByName("localhost")
joel.neely
+1  A: 

For folks with access to Mathematica, JLink lets you access Java and script with Mathematica code:

Needs["JLink`"]
LoadJavaClass["java.net.InetAddress"]
InetAddress`getAllByName["localhost"]

Hit Shift-Enter to evaluate, and you get

{<<JavaObject[java.net.Inet4Address>>}

Then you can use Mathematica's Map function to call toString on the returned objects:

#@toString[]& /@ %

to get the result (or to use the less obscure syntax, Map[Function[obj, obj@toString[]], %]):

{"localhost/127.0.0.1"}

If you start to get serious with this, you'll want to read Todd Gayley's tutorial at http://reference.wolfram.com/mathematica/JLink/tutorial/Overview.html.

jfklein
+2  A: 

Eclipse has a feature to do this, although it's not a loop. It's called a "Scrapbook Page". I assume the analogy is supposed to be that you have a scrapbook where you collect little snippets of code.

Anyway, to make it work, open a project in Eclipse (your Scrapbook Page is going to be associated with a project -- Eclipse likes it when projects own things).

Then:

  1. In the project navigator window, select a folder that exists somewhere in your project.
  2. Either select the menu File -> New -> Other, or hit Control-N.
  3. Select Java -> Java Run/Debug -> Scrapbook Page.
  4. Hit "Next", then give it a filename, then hit "Finish".

Now you have a scrapbook page. Type some code, like maybe this:

System.out.println(System.getProperties());

Then select the text with the mouse, and either hit Control-U or select "Execute" from the context menu. The code will run and the output will appear on the console.

You can also type an expression, select it, and select Display from the context menu. It'll evaluate the expression and print its type. For example, running Display on 1 + 2 will print (int) 3.

Logan
Thanks for that. Beanshell was timing out on the splash for me, but this works perfectly.
Bayard Randel
A: 

You could take a look at BlueJ which is an interactive Java development environment meant for teaching OOP rather than as full IDE like Eclipse or NetBeans. It's kind of fun to have a play with anyway.

You can see it in action on YouTube in a series of Java tutorials.

Trevor Tippins
A: 

you can script java using jruby http://kenai.com/projects/jruby/pages/CallingJavaFromJRuby

rogerdpack