3

Is there a way to run a Java command line in a web page along the lines of this Ruby web page:

http://tryruby.org/levels/1/challenges/0

The aim is to be able to give complete newbies a very simple introduction to the language without having to worry about IDEs, compilation, etc.

Snowcrash
  • 80,579
  • 89
  • 266
  • 376
  • This is relatively easy in Ruby because it is an interpreted language, and doesn't have to worry about compilation anyway. In Java it would be harder. You could possibly compile in the background somehow (effectively making an IDE with a browser interface). Or you could make your own Java interpreter - but then I'm not sure if that would still be Java, strictly speaking. – raveturned Mar 29 '12 at 10:22

2 Answers2

2

Well, since Java is a compiled language and has no REPL, there is no such "command line". But I can think of theoretically possible ways to implement the idea.

  • An Applet might be not really a solution. I don't know how far you can get with limited permissions. Security concerns might allow you to only operate in a sandbox and / or not to compile / execute code.

  • A Java WebStart application might have such permissions. It would be a similar task to provide a slim IDE. Or making Bluej run from WebStart.

  • Provide a web application that simply files a request to a server which compiles and executes the code and returns the result. I assume (I'm not sure) many online REPL work like this. (By skimming the JavaScript of try python I think it files AJAX requests) But then there's still a security concern, like what if a programm begins to randomly remove files? Google Appengine has advanced security mechanisms to prevent missuse. To implement them for "try java" it would require advanced effort.

  • Next idea is to limit everything down to a subset of the Java language. For providing a small introduction, a little tutorial with predefined answers and maybe a bit basic math you could write some JavaScript on the client side to decide wether the learner's answers are correct or not.

  • Don't forget that web-based IDEs are currently developed, for instance Eclipse Orion. Maybe you could watch these projects evolve and use them for such purpose. Currently I've only seen JavaScript code edited in there, and executing JS is one of the webbrowsers natural capabilities. I don't know which programming languages they are going to support or if code execution will be supported.

f4lco
  • 3,728
  • 5
  • 28
  • 53
  • Update: Java 9+ comes with a REPL. See [*JEP 222: jshell: The Java Shell (Read-Eval-Print Loop)*](https://openjdk.org/jeps/222) – Basil Bourque Dec 16 '22 at 01:19
  • Update: [Java Web Start](https://en.wikipedia.org/wiki/Java_Web_Start) has been deprecated by Oracle, and no longer included in Java 11+. [OpenWebStart](https://openwebstart.com/) is an independent implementation of the same technology. – Basil Bourque Dec 16 '22 at 01:22
0

But usually java needs to be compiled to be useful to a JVM. So I am not sure you can do anything like that that would be useful for java. The key difference is in the interpreted (Ruby) vs compiled (java) implementation.

See What's the difference between compiled and interpreted language?

Community
  • 1
  • 1
Mark Fraser
  • 3,160
  • 2
  • 29
  • 48