8 ms·
Jshell: The Java Shell (REPL)
- anon3_ 11y agoThis is big news. Java should have had this one from the start. My question is, what took so long? What prevented it?
- istvan__ 11y agoThe lack of interest. If you haven't used a language that has a REPL you don't know how powerful it is. Most of the Java devs I know haven't used REPL with any other language.
- ayanamist 11y agoIn fact, JavaREPL (http://www.javarepl.com/ http://www.javarepl.com/) with its standalone version exists for very long time.
- andor 11y agoI'd say that Java's verbosity doesn't lend itself well to interactive use. There are quite a few REPLs that allow calling Java code from another language, e.g. the integrated Javascript interpreters (run `jjs` in a terminal), BeanShell, Jython, JRuby, etc.
- aurora72 11y agoSeveral months ago I wanted to have an REPL for C and couldn't find anything satisfactory and I had the similar thought: Why weren't there any decent REPL for C ?
- userbinator 11y agoHave you looked at these? https://news.ycombinator.com/item?id=9641832 https://news.ycombinator.com/item?id=9641832 https://root.cern.ch/drupal/content/cling https://root.cern.ch/drupal/content/cling http://blog.pclewis.com/2010/03/tip-using-gdb-as-an-interactive-c-shell/ http://blog.pclewis.com/2010/03/tip-using-gdb-as-an-interact...
- LoSboccacc 11y agoBeanshell was there since 1999
- sam_lowry_ 11y agoYes, finally someone remembered it! Reinventing the wheel is so human...
- LoSboccacc 11y agoI know right? But if you say groovy and the like are just respin of age old stuff they treat you like an old fart And to think that when I advocated for writing business logic in a scripted language in 2003 everyone was looking at me like I was crazy (well italy is always quite behind last trends, so while in the states they were starting with scripting languages we were still writing ejb with xml - and we still do in many places)
- deleted 11y ago[deleted]
- copperx 11y agoThis is nothing new. When I was at uni (10+ years ago), we used DrJava, which includes a Java REPL.
- zak_mc_kracken 11y agoAll Java IDE's have offered an interactive shell for years so there is little need for a REPL. It's just a nice thing to have but nothing more.
- drtse4 11y agoIf someone is interested in using something like that (minus the API part), right now, try this: https://github.com/albertlatacz/java-repl https://github.com/albertlatacz/java-repl
- LoSboccacc 11y agoWhy everyone keeps forgetting beanshell? It is both a shell and a scripting language with context bridges to pass object back and forth from java, it was doing everything groovy became and then some but three or more year earlier
- pjmlp 11y agoBecause it was left to rotten? Last time I checked it, in 2010, it didn't had any commits.
- LoSboccacc 11y ago:( aw
- LukeShu 11y agoThe last release from the original author was 2.0b4 (2.0 beta 4) in 2009. However, there is a community fork "beanshell2"[1], which was created exactly because the original was left to rot. It looks like the most recent change to beanshell2 was February 2014, so it may be going the same direction. [1]: https://github.com/pejobo/beanshell2 https://github.com/pejobo/beanshell2 (formerly https://code.google.com/p/beanshell2/ https://code.google.com/p/beanshell2/
- snerd 11y agoI'm confused by that too. I use it for a personal project and it's pretty rad.
- github-cat 11y agoSo each programming language are becoming more and more alike. Many of them are not pure OOP or pure functional language anymore.
- NhanH 11y agoOOP and functional has never really been a meaningful axis to categorize programming languages. There are more difference between Smalltalk(OOP) and Java(OOP) than say, Smalltalk and Lisp(supposedly more FP). It's probably better to categorize things based on (pick one of) semantic, runtime (JVM vs Beam vs blablabla), productivity, correctness, readability etc.
- Elrac 11y agoMuch of the description of Jshell is in the future tense. Can someone give us an idea of the state of progress?
- badpenny 11y agoJDK stands for JDK Enhancement Proposal. You can get an idea about of the progress from the linked issue tracker entry.
- hmottestad 11y agoJEP
- anjanb 11y ago@Elrac : You can get the shell running if you have linux or mac. Yesterday, I tried on windows but couldn't get it working yet. Instructions are here : http://www.jclarity.com/2015/04/15/java-9-repl-getting-started-guide/ http://www.jclarity.com/2015/04/15/java-9-repl-getting-start...
- Elrac 11y agoCool, thanks!
- spacemanmatt 11y agoToo late. Learned Clojure.
- pjmlp 11y agoGreat if are allowed to use it at work. Many of us are not.
- mateuszf 11y agoIt's just a jar.
- _asummers 11y agoDo your coworkers never have to go in and maintain your code? What about after you leave? Do you not have to pull request the code before it's merged in? At the end of the day it IS just a jar, but that ignores all the non-runtime aspects of coding.
- dblarons 11y agoEven if you don't write Clojure code in your project, the Clojure REPL can be used to experiment with Java code in the same way the Groovy or Scala REPLs can.
- MrBuddyCasino 11y agoAlso, it is possible to attach to a running Java process with a Clojure REPL. You won't leave behind any evidence, or introduce technical debt.
- spacemanmatt 11y agoI'll try to hit those one by one... > Do your coworkers never have to go in and maintain your code? What about after you leave? I wrote an extension to our core app that is needed by 2 customers. Someone might need to touch it again one day, but it's more likely that a new spec will demand new engineering for a new solution in the future, at which point the most favorable technology will be used. That goes for me or the next person. > Do you not have to pull request the code before it's merged in? No. I'm a full-time employee and a core committer. We already have extensions in Java, Groovy, (very limited, nearly dead experiment) Jython, and now Clojure. Technically my extension is Java and it is a thin wrapper to a Clojure library. > At the end of the day it IS just a jar, but that ignores all the non-runtime aspects of coding. They are de minimis in this case. It survived review with nominal eyebrows raised. It's a Java shop but they're familiar with the family of other JVM languages. HTH.
- elmarschraml 11y agoIf you're looking for a REPL for Java, try the Groovy shell (comes standard with an install of Groovy). Meant, as the name implies, for Groovy, but since Java is a subset of Groovy, works fine for any Java code, too. It's great for quickly trying out an API or library. Also makes it easy to just drop a bunch of jar files onto the classpath.
- vinay427 11y agoWhat about, for instance, the difference in multi-methods presented here? I would assume you would get different results in the Groovy REPL than in a Java compiler. http://www.groovy-lang.org/differences.html http://www.groovy-lang.org/differences.html
- cle 11y agoCan second this. At work, we have a large legacy Java codebase, and our most useful ops tool is our entire codebase and Spring beans loaded into a Groovy shell. Extremely convenient. Groovy is largely compatible with Java, but it also has shorthand notations (splat operator, elvis operator, closures, optional typing) and collections that are much more expressive than Java.
- vorg 11y ago> Java is a subset of Groovy Java is not a subset of Groovy. Some syntax valid in both will behave differently in each, e.g. default visibility, and how == behaves.
- arturventura 11y agoI had to implement a Java REPL like this one in school. My experience of using it is that tells me that without a undefined type keyword it becomes very boring to use. Each time you have to define a variable you have to write all type info: Map<String,String> foo = new HashMap<>(); If java had a inferred type declaration, like Golang, Swift, Scala, etc. This would be much simpler: var foo = new HashMap<String,String>(); But from what I think there is nothing like that on Java's roadmap. In fact, I think it was proposed and rejected in the past.
- josho 11y agoWhat's wrong with simply: Object foo = ... Not quite var, and it would require the interpreter to do a little bit more work when invoking methods to avoid the necessary downcasting.
- Alphasite_ 11y agoBecause you're sacrificing consistancy and type safety for object to avoid increasing the syntax which feels like optimising for the wrong metric.
- jonathankoren 11y agoIt's a REPL! It's for debugging. If you're not optimizing for ease of use, you're optimizing for the wrong metric.
- lgas 11y agoYou could also just add support for the "var x =" notation into the REPL as a preprocessing step that translates it to the appropriate typed representation.
- 67726e 11y agoI get why that would be nice, but it feels kinda wrong for what Java is[0]. That's why if I need to quickly bang out or test something I use my Scala REPL. [0] - http://programmers.stackexchange.com/a/184183/7007 http://programmers.stackexchange.com/a/184183/7007
- russtrotter 11y agoI'd add my vote for using Groovy's shell (groovysh) for Java REPL. The shortcuts and conveniences the Groovy lang provides make it perfect for this kind of application.
- vorg 11y ago> The shortcuts and conveniences the Groovy lang provides make it perfect for this kind of application Groovy's original purpose, as frequently stated by its creator James Strachan, was exactly this kind of application. There's been a lot of other things going on with Groovy since then which anyone considering using it would have to evaluate. Although other JVM scripting languages existed at the time of its birth in 2003, e.g. Jython since 1997 and Beanshell since 1999, Groovy brought more to the JVM than Beanshell had and did it using Java's syntax style, unlike Jython. Groovy's also used a lot in scripts for manipulating and testing JVM classes. And it's this use of Groovy that made it a good match for Gradle, i.e. short 50-liners defining builds. Groovy's been extended to other use cases since then. Graeme Rocher had a meta-object protocol added to Groovy so he could bundle it as the scripting language of Grails (used just like Ruby is in Rails). The backers of Grails have an overbearing attitude towards Groovy, as they do all the software Grails bundles. They muscled in on the consulting market for Spring until they got bought out by SpringSource in 2008. Grails 3, released earlier this year, bundled Gradle for the first time, and the Grails backers will no doubt use that as justification to attack Gradleware's placing in the market for Gradle consulting, and so ultimately wrest control of Groovy via its primary applications from its neutral home at the Apache incubator. Groovy was also extended in version 2 by a static type-checking and compilation facility, which was duplicated from an addon created by Alex Tkachman. Making no headway into the overcrowded market for statically-typed JVM languages, last year the Groovy backers then repurposed that static-typing facility to make Groovy target the Android platform. This year, Groovy's sole full-time developer supporting it all was retrenched when the Groovy/Grails group moved from VMware/Pivotal to OCI Consulting. I'm picking Groovy will remain popular on the JVM for scripting and REPL work, including Gradle build scripts, for quite a while. It will never be used much for statically typed work on the JVM or Android, and eventually its use for scripting a web framework will slowly decline as Grails follows Rails into eventual obscurity perhaps 5 years down the line. Groovy will continue to suffer from leadership difficulties, perhaps with the Grails group taking control of Gradle via a sustained attacked on their consulting business, and through this eventually taking over the Groovy code base at, or from, the Apache incubator. I wouldn't want to pick the time frame for all this, though!
- arikrak 11y agoBlueJ [1] is geared at beginners, but it includes a codepad for trying out Java code and also provides a way to interact with objects. [1] http://www.bluej.org/ http://www.bluej.org/
- zak_mc_kracken 11y agoAll IDE's do. Please stay away from BlueJ, its compiler is terrible.
- jetpm 11y agoI developed something similiar but for the Android API, it goes together with an app, but it is also a Java Read-Eval-Print Loop where the code is executed on the device. It's in beta phase. https://runondroid.com https://runondroid.com
- jnbiche 11y agoOh very cool. I've used sl4a for this, but it would be very nice to have access to the original Java API for experimenting with new features for an actual native Android application. I look forward to trying it out.
- suprgeek 11y agoFor those of you in the Bay Area, there is a Hackathon (SVJUG Meetup) at Google on July 11th that is expressly dedicated to JShell - http://www.meetup.com/sv-jug/events/223263007/ http://www.meetup.com/sv-jug/events/223263007/ Drop by, check it out & please stop the Snarky "Should have had it 10 yrs ago" comments.
- theknarf 11y agoI read JS-Hell. Not the best name for a shell.
- cbsmith 11y agoAs they mention, BeanShell was already serving this purpose, and really would provide a great foundation for building what is needed. The fact that it hasn't been updated much kind of speaks volumes of the importance of this effort.