43

are there any interpretors available online? Where I could just throw a line or two of java into it and it would output the result?

Joe
  • 46,419
  • 33
  • 155
  • 245
Skizit
  • 43,506
  • 91
  • 209
  • 269
  • 5
    Hav you looked into BeanShell (http://www.beanshell.org/)? It's not online but might do what you want. – FrustratedWithFormsDesigner Aug 17 '10 at 15:55
  • Bear in mind that Java is a staticaly typed compiled language so it's not possible to interactively interpret strict Java syntax. BeanShell is great, but it's not strict Java. But then, any online interpreter sandbox even for an interpreted language like Python is going to have massive limitations. http://try-python.mired.org/ – Simon Hibbs Aug 17 '10 at 16:02
  • 2
    @SimonHibbs It actually is possible to write an interpreter for a statically compiled language, such as C or Java. There are some interpreters for C described here: stackoverflow.com/questions/584714/… – Anderson Green Mar 1 at 4:28 – Anderson Green Mar 15 '13 at 00:24
  • 1
    Try [jshell](https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm) -- available since Java 9 – ovo May 14 '19 at 17:12

10 Answers10

31

ideone.com which supports Java and many other languages.

Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
31

Don't forget Jython

Edited answer:

True, it is not an interpreter as in the sense of the question, so a fair downvote. But IMO, it's possible to throw "some" Java code towards it, and it'll output the result:

>>> from java.lang import System
>>> System.out.println("Hello world")
Hello world
>>> from java.util import Random
>>> r = Random()
>>> r.nextInt()
572839857
>>> r.nextInt(2)
0

I just believe that a smart developer would be able to make quite efficient use of this. I know it's helped me in the past :)

Noel M
  • 15,812
  • 8
  • 39
  • 47
9

Your best bet is probably http://www.javarepl.com/ but there are several other options:

If you require the interpreter to support Java syntax exactly, then ideone.com will let you evaluate arbitrary Java code.

If you want to evaluate Java snippets, you could use a Groovy interpreter. Java code is generally valid Groovy too, and Groovy doesn't force you to define a whole class. There's an online Groovy interpreter.

If you don't require the interpreter to be online, there's also the Eclipse scrap book, DrJava, and BeanShell2 which all allow Java to be interpreted.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
5

Use bsh. It's a java-based shell.

Vladimir
  • 9,913
  • 4
  • 26
  • 37
3

If you have Groovy installed, the Groovy Console also works with Java Code.

Johannes Wachter
  • 2,715
  • 17
  • 17
  • 1
    just a warning: I like the Groovy Console but Groovy is not Java. There are some syntactical parts, which behave differently or don't work in Groovy – Hachi Feb 24 '13 at 19:49
  • Example: ```groovy -e "System.out.println('hello world');"``` – Dave Thomas Oct 24 '16 at 22:47
2

jGRASP contains a very useful interpreter that I've used to check small portions of code and do minor string processing.

This may still be the recommended IDE for beginning CS students at Auburn University.

http://www.jgrasp.org/

dm78
  • 1,570
  • 1
  • 16
  • 28
  • When I was still in uni, I always coded Java in Netbeans with jGRASP open in the background for exactly this usage. – Dave Oct 03 '13 at 18:57
2

Check out Ideone.com. You can type in a Java program and it'll compile it and then run it for you and display the output. Here's a hello world example I just typed in.

Code:

public class Main {
    public void main(String[] arguments) {
        System.out.println("Hello world!");
    }
}

Output:

result: success   time: 0.1s   memory: 213376 kB   returned value: 1

input: no
output: no
stderr:
Exception in thread "main" java.lang.NoSuchMethodError: main

Oops, I forgot to declare main as static!

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
2

Eclipse's built-in feature "scrap book" could be pretty close to what you want.

anothernode
  • 5,100
  • 13
  • 43
  • 62
Enno Shioji
  • 26,542
  • 13
  • 70
  • 109
1

https://repl.it/languages/java

It's a nice interpreter and works fine, but might lag a little sometimes

0

Use xbsh, install it in ubuntu with:

$ sudo aptitude install bsh

that is a graphical and enhanced interface to bsh i.e. BeanShell

run it with:

$ xbsh

lsaavedr
  • 11
  • 2