Is there something like python's interactive REPL mode, but for Java?

前端 未结 28 1023
一个人的身影
一个人的身影 2020-11-29 16:38

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, a

相关标签:
28条回答
  • 2020-11-29 17:01

    Java-REPL by Albert Latacz works well.

    You can try it directly from your browser here: http://www.javarepl.com/term.html

    The source code is available here, and it has a decent Intelli-J plugin.

    https://github.com/albertlatacz/java-repl

    0 讨论(0)
  • 2020-11-29 17:03

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

    0 讨论(0)
  • 2020-11-29 17:07

    Clojure provides a REPL you can use.

    0 讨论(0)
  • 2020-11-29 17:07

    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]
    
    0 讨论(0)
  • 2020-11-29 17:07

    If you already know Groovy (which I assume you do, since you mentioned the Groovy Console), then just use groovysh or groovyConsole, which are included in the Groovy distro. If you have custom jars that you want to import, you can either write a batch file that starts up groovysh/groovyConsole with those added to the classpath. You can also do this

    this.class.classLoader.rootLoader.addURL(new URL("file:///path to file"))
    

    from within the shell to load other jars.

    I used to use Jython several years ago to do just what you're asking. As part of my build script, I generated a custom jython.bat and .py file that included the full classpath for the project I was working on. That way when I started Jython, it would have all the code available, and it would bring up Spring to let me twiddle things in the live system. You can do the same thing with Groovy, JRuby, BeanShell, etc.

    0 讨论(0)
  • 2020-11-29 17:09

    Java 9 is providing the JShell.

    jshell> println( "Print me!")
    jshell> Print me!
    
    0 讨论(0)
提交回复
热议问题