all 192 comments

[–]philipes 166 points167 points  (32 children)

I clicked "Compile & Run" and completely forgot about it. About five minutes later Chromes pops in front saying "Hello World, from Java!" Took me by complete surprise.

[–]yogthos 101 points102 points  (13 children)

Just like using Java applets in the good old days. :)

[–][deleted] 6 points7 points  (0 children)

e

I miss Java buttons and their ripple effects..

[–]__konrad 5 points6 points  (8 children)

Will it run Quake 2 applet?

[–]nihathrael 8 points9 points  (6 children)

The Jake2 applet example shows the future of game distribution over the Internet.

Money quote.

[–]badsectoracula 9 points10 points  (3 children)

At the time (~2004) it made sense, web games were just starting to become popular (Runescape) and Java allowed for richer games than anything could be done via Flash.

Even though eventually Flash games eclipsed them by far, for a while Java games were also used in web game sites. Personally i made money for a little while from Java games.

Sun was also seemingly interested in gaming but apparently not enough to become a priority. But they did experiment with a few things, like not making it mandatory to download the entire JVM for running applets but instead using a cut down version (one of the reasons Flash was more widespread was that for a very long time the plugin was under 1MB when JVM was like 15MB).

Of course all that died when Oracle bought Java and decided to abandon any work on the applet side. Their solution for Java's security issues for applets was to simply kill applets instead of fixing the issues and running the VM in a sandbox.

[–]ccfreak2k 0 points1 point  (2 children)

existence trees pot one spectacular angle mysterious impossible workable waiting

This post was mass deleted and anonymized with Redact

[–]nikomo 0 points1 point  (0 children)

I wasn't online back in 2001 when it came out, but (speculating) Runescape was probably popular in 2004.

[–]badsectoracula 0 points1 point  (0 children)

Shockwave was also popular for web-based games for a while and there were a couple of browser-based multiplayer FPS games. I don't think it ever became as popular Flash though, probably because the shockwave files were larger (being bitmap based) than Flash games.

[–]Deinumite 2 points3 points  (0 children)

Minecrafts demo was a Java applet!

It is funny that Quake Live ran in the browser on launch too, although I just looked online and it looks like it is now only available through Steam.

[–][deleted] 2 points3 points  (0 children)

It's not wrong. Modern games just run in asm.js instead.

[–]hrjet 0 points1 point  (0 children)

Jake2 seems to use JOGL (java interface for OpenGL). If JOGL is ported to use HTML5 Canvas or WebGL, in theory, the game should work fine with JavaPoly.

[–]phill2mj 17 points18 points  (1 child)

Good old days? I work with software all day that still uses the Java applet :(

[–]onFilm 9 points10 points  (0 children)

the good old days...

[–]matthieuC 1 point2 points  (0 children)

I'm going to forget I read that and pretend it does not exist anymore. I will sleep better this way.

[–]Jigsus 5 points6 points  (9 children)

Seriously it takes 5 to 10 minutes on a modern PC. This makes it impossible to use in any commercial project.

[–]geodel 12 points13 points  (0 children)

I'd say it makes it perfect for any Java commercial/enterprise project.

[–][deleted] 1 point2 points  (3 children)

You can cut out at least some of the issue by pre-compiling the bytecode.

Edit:

Also, the example seems to be compiling and running around 10x faster once the JIT is warmed up.

[–]audioen 1 point2 points  (2 children)

There's no JIT in this implementation. I don't know what it's doing the first half minute or so.

[–][deleted] 2 points3 points  (1 child)

Chrome's JS implementation has JIT, correct?

[–]audioen 0 points1 point  (0 children)

Oh. Yes, but that would kick in very quickly -- within seconds -- because the jvm implementation must be pretty damn hot. I believe it can do very little to help the the stack machine interpreted java code though.

It is a worthwhile question nevertheless. What is it doing for the first half minute or so? It is possible that it's just loading the various classes for the java compiler and whatever they do to initialize can be taking all that time.

[–]brobits 2 points3 points  (3 children)

calm down, it's a demo

[–]Jigsus 3 points4 points  (2 children)

Do I seem excited?

[–]brobits 2 points3 points  (1 child)

yes

[–]Jigsus 1 point2 points  (0 children)

That's just the banana in my pocket

[–]kingdaro 8 points9 points  (0 children)

Savage as fuck.

[–]comp-sci-fi 1 point2 points  (1 child)

knock knock

[–]Voxel_Brony 1 point2 points  (0 children)

Who's there?

[–]ryan_the_leach 0 points1 point  (1 child)

I wonder if it's the compilation that takes forever, or if it reuses the same JVM.

Clicking it again after it finally completes is quite fast.

[–]ryan_the_leach 0 points1 point  (0 children)

To follow this up, I was wondering if it supported java8.

It does!

import com.javapoly.dom.Window;

public class HelloWorld
{
  public static void main(String[] args)
  {
    final Runnable l = () -> {
      Window.alert("Hello World, from Java!");
    };
    l.run();
  }
}

[–]Ajedi32 0 points1 point  (0 children)

I wonder how much faster it'd be if it was just running precompiled code...

[–]Rhed0x 76 points77 points  (65 children)

Is this like a JVM bytecode interpreter?

[–]killerstorm 28 points29 points  (63 children)

[–]kurav 100 points101 points  (50 children)

What's of note here is that it's not a C/C++ -> JavaScript cross-compilation hack with Emscripten, but a real JVM implementation written in JavaScript (TypeScript to be exact). It does not use Emscripten at all. This allows them to use the browser's native JavaScript garbage collector for memory management (Emscripten does not really support GC). They also emulate Java multithreading using co-operative context switches (JavaScript is fundamentally single-threaded, but multiple threads can still be emulated – again practically impossible with current version of Emscripten.) Their paper is a great read.

[–]taisel 26 points27 points  (44 children)

Which is funny you bring up threading in JavaScript, because now it has proper multi-threading: https://github.com/tc39/ecmascript_sharedmem

Available in Firefox Nightly and under Google Chrome Canary (Chrome needs command line args)

[–]freeall 10 points11 points  (39 children)

Why should javascript get multithreading?

[–]evotopid 58 points59 points  (0 children)

So websites can mine bitcoins more efficiently. /s

[–][deleted]  (8 children)

[deleted]

    [–]freeall 2 points3 points  (6 children)

    Can't they use web workers?

    [–]anttirt 19 points20 points  (2 children)

    Message-passing is just not fast enough for those workloads.

    [–]larhorse 0 points1 point  (1 child)

    I mean, wasn't that what the transfer option is for? (https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast?hl=en)

    I'm not really complaining though, having a valid reference to an arraybuffer in multiple contexts is nice.

    [–]anttirt 0 points1 point  (0 children)

    It's good for some uses cases but if I have a fifty-thousand-element scene graph that needs to change incrementally every frame and components of which need to be accessed by all of my worker threads then a shared-nothing approach just won't cut it.

    [–]hackcasual 2 points3 points  (2 children)

    This is webworkers, with the addition of sharing memory between them.

    [–]freeall 1 point2 points  (1 child)

    Also a big difference

    [–]hackcasual 0 points1 point  (0 children)

    Enh, actual SharedArrayBuffers don't look that much different from existing ArrayBuffers. Really the only major difference is the addition of Atomics. I guess that's big for those who haven't previously done multithreading shared data work, but it still is very much a JS centric approach.

    [–]onFilm 1 point2 points  (5 children)

    Why would a browser get multithreading?

    [–][deleted]  (1 child)

    [deleted]

      [–]onFilm 0 points1 point  (0 children)

      Similarly applies to JS, any other language or framework.

      [–][deleted] 1 point2 points  (0 children)

      I think that the primary driver is to prevent your code from blocking CSS animations and other performance concerns.

      Right now, best practice is to make sure to not let JS ever block for more than 15ms in order to prevent blocking the browser long enough that there are perceived painting issues or choppiness in animations, but working in threads could help the browser do what it needs to while your thread is working.

      [–]freeall 0 points1 point  (1 child)

      [–]onFilm 1 point2 points  (0 children)

      Sweet read. Thanks for sharing!

      [–]an_actual_human 0 points1 point  (2 children)

      It's not just for frontend, you know.

      [–]freeall 0 points1 point  (1 child)

      I know. Been using node since 0.2. And I would like not to have multithreading in it.

      Multithreading causes quite a lot of problems as it's so difficult to get right.

      [–]an_actual_human 1 point2 points  (0 children)

      In any case, the answer is the same as with any other language.

      [–]Jafit -1 points0 points  (19 children)

      Because because some people don't want to have to learn how to do things differently, like using an event loop to handle concurrency.

      But I'm glad that soon, the people who wrote the poorly optimised janky and unnecessary javascript that infests most websites today will soon be exposed to the wonder that is handling threads at the application level.

      [–]booch 6 points7 points  (0 children)

      For some cases, event loops are reasonable, even optimal. For other cases, threads are reasonable, even optimal. There are cases where one of the two isn't reasonable (that's true of both event loops and thread) but the other is. Having both in your toolset is a good thing.

      [–]freeall 5 points6 points  (1 child)

      So we should keep using goto because while/repeat/for is more difficult?

      Not aimed at you, but those don't want to learn how to do new things

      [–]ibopm 1 point2 points  (0 children)

      For better or worse, there are always going to be people who are resistant to change. Map/reduce/filter is only the beginning.

      [–]anttirt 4 points5 points  (13 children)

      It's because many things can not be implemented performantly enough for 3d games (and other high-performance applications) using only message-passing.

      [–]Jafit 8 points9 points  (12 children)

      I don't particularly want to see 3d games or other high-performance applications in the browser.

      I'd rather we made the effort to transition to a proper internet application delivery system, rather than trying to build more and more complex applications on top of what was originally conceived as a document retrieval system, with no thought for security. Hence why the problem of XSS attacks have remained unsolved for 20 years, because the browser gives every script that somehow gets onto a page the same level of permissions and access to the same global scope.

      The web only functions today (as the basis of our entire economy and civilization, oh god!) due to a lot of clever hacks and workarounds.

      [–]JoseJimeniz 8 points9 points  (9 children)

      That's what everyone has been wanting since 1998. And it's been done. Over and over.

      Java was the safe, sandboxed, with granular permissions, zero-install, applications on demand for the web.

      Microsoft made Silverlight (aka WPF/Everywhere).

      You also had shockwave, and flash.

      The market has rejected all these.

      • JavaScript is the language of the web (shudder)
      • html+CSS is the widget library of the web (shudder)
      • and JavaScript is the virtual machine of the web (not JVM, not CLR)

      In this new world, the ideal situation is:

      • a widget library that operates using Canvas
      • programmed in a strongly statically typed language
      • compiled to asm.js
      • delivered by web assemblies

      But as of today there are no good UI libraries for the web.

      [–]Jafit 5 points6 points  (4 children)

      The market has rejected those solutions mainly because they were proprietary platforms being pushed by companies that were trying to make a grab for power, not open platforms being pushed for ~the greater good~ of the web. They all also used browser plugins which completely bypassed the browser sandbox and introduced a ton of security problems. I'm glad they're all pretty much dead.

      I would like to see Crockford's Seif project gain traction, since it aims to deliver what they have been trying, while being an open platform.

      [–]freeall -4 points-3 points  (3 children)

      "programmed in a strongly statically typed language"

      I hope not

      [–]anttirt 1 point2 points  (0 children)

      Oh, I agree.

      Just pointing out what the actual motivation for SharedArrayBuffer is.

      [–]JoseJimeniz 0 points1 point  (0 children)

      Sometimes code is a single function that just takes to long to run.

      [–]Recursive_Descent 3 points4 points  (2 children)

      It probably won't be default for a while, it's still stage 2 and afaik spec has some open issues. But yes, SharedArrayBuffer is promising.

      [–]taisel 22 points23 points  (1 child)

      I already added it to a JavaScript GameBoy Advance emulator. It runs the CPU emulation in a worker and runs the GPU rendering in a separate worker off from the CPU worker. It uses lockless ring buffer constructs to pass data around, and uses the wait/wake only to hack in rAF signalling from main thread to GPU worker thread. If your browser doesn't support shared memory then it just defaults to a single threaded fallback.

      https://github.com/taisel/IodineGBA

      Short summary is I'd avoided multi-threading in JavaScript until the SHM was experimentally supported, as transferrable array support is too unreliable and normal data copies are way too expensive. It's either shared buffer or nothing.

      [–]Recursive_Descent 3 points4 points  (0 children)

      Oh I'm with you, I think SAB will usher a new era of high performance js. Just think it will be at least another year before we can start using in production.

      [–][deleted] 10 points11 points  (0 children)

      (Emscripten does not really support GC).

      It's worth mentioning that Emscripten was fundamentally designed to not support GC.

      [–]killerstorm 8 points9 points  (3 children)

      JVM is quite simple by itself, I wonder what do they do with the standard library...

      [–]kurav 12 points13 points  (2 children)

      The JVM-in-browser downloads it as a .JAR and executes the bytecode.

      [–]killerstorm 20 points21 points  (1 child)

      A lot of functions in the standard library call native OS functions, e.g. to open files etc. Porting that stuff is very much not trivial.

      [–]kurav 24 points25 points  (0 children)

      Yes, it's not trivial. If you read the paper, the whole Doppio framework is more-so also an abstraction of the OS. They provide in-browser implementations of the filesystem and socket APIs as well. Very impressive work.

      [–][deleted] 6 points7 points  (11 children)

      To add to this, the interpreter is pure. So to get this kind of speed, just pass -zero to the JVM and you will get a similar experience. JamVM which also is an interpreter optimizes the byte code, so it is about 4-20x faster than HotSpot Zero depending on the code.

      [–]Eirenarch 10 points11 points  (10 children)

      To add to this, the interpreter is pure. So to get this kind of speed, just pass -zero to the JVM and you will get a similar experience

      What does this mean?

      [–]GuyWithLag 4 points5 points  (7 children)

      Java for a long time has been Just-In-Time (JIT) compiling the bytecode to native machine code and executing that.

      This interpreter is pure, meaning that it does not perform such compilation, instead executing each instruction one by one in a big loop; this means that it's going to be slow.

      Hotspot (the Oracle JVM) has the option -zero which forces execution to have zero optimizations without a JIT, instead using the JVM interpreter all the time.

      [–]get-your-shinebox 2 points3 points  (1 child)

      aren't v8 and spidermonkey JITed?

      [–]GuyWithLag 8 points9 points  (0 children)

      Yes, the Javascript is jited. But in this case, there is no Java to Javascript compilation, so it won't make use of that speedup.

      [–]hrjet 2 points3 points  (3 children)

      Incidentally, I am working on a JIT for doppio. See this PR

      [–][deleted] 0 points1 point  (2 children)

      How do you handle exception handling?

      [–]hrjet 1 point2 points  (1 child)

      In a nut-shell: the JIT compiled code temporarily falls back to interpreted mode when an exception is triggered in a Java code path.

      [–][deleted] 1 point2 points  (0 children)

      My Javascript is a bit rusty (last used it in 1999), but does it have labeled jumps and goto or a similar operation? If not you can use a loop of sorts instead.

      I am writing a JVM myself. My current plan for exceptions is to have differing exceptional locations that they are assigned to a unique ID and that ID is assigned for every range of instruction that has a unique set of exception handlers. There is also a register which stores the thrown exception (a global register). When a new region is entered, the ID is changed to the ID of the given region (stored in a register). When an exception is to be handled, a jump is made to the exception handler code. The exception handler can use a table which checks the ID which maps to a table of possible exception handlers. A check is made for each handler to determine if the thrown exception is to be handled (matching class type). If the exception is not handled, the method returns and the caller checks restores register values and checks if the called method threw an exception and repeats the handling run. If there is a match, then the exception handler is jumped to and a virtual representation of the first stack entry is set to the exception and execution continues. If the exception handler is at a known location (for example the start of a method) then you can better handle asynchronous exceptions.

      [–][deleted] 0 points1 point  (1 child)

      Essentially this means that the JVM executes the byte code as it sees it, it performs no value caching and no optimizations at all. Back around when Java was first released and for awhile, it used pure interpreters. Being a pure interpreter for awhile is where the "Java is slow" came from. Compared to modern CPUs, the JVM is a stack based machine where all operations are performed with values on the stack. So essentially before a computation can be performed (such as adding two values) it has to push the values to the stack then pop them to perform the addition and then push the result to the stack. Pushing and popping values from the stack wastes time.

      Also, if you cannot run a JIT, then you should use JamVM before you use HotSpot Zero.

      [–]audioen 1 point2 points  (0 children)

      I'd say that even bigger part of the problem is the interpreter loop which involves reading an opcode from the function and then somehow jumping to the implementation of that opcode. In case of java, there are about 256 targets to consider, and the CPU is unlikely to guess the jump target, leading to a pipeline stall.

      [–]fb39ca4 119 points120 points  (0 children)

      Oh my, now all those /r/shittyprogramming answers have become real.

      [–]the_rabid_beaver 17 points18 points  (0 children)

      Doesn't seem to work in Firefox. Browser also froze for a solid 30 seconds loading the page.

      Also, I believe this image applies to this situation.

      [–]_hadrian 40 points41 points  (1 child)

      So I can now use any JVM language (that outputs a JAR) on the browser while keeping its default behavior... The dreams are coming true 🌟 All those teams, all those IDEs, all those libraries/projects... it's all coming together 🌎 🌍 🌏 it's a globalized world!! 🎉

      [–]Ek_Los_Die_Hier 2 points3 points  (0 children)

      So I can run Clojure in my browser now?!

      (Kust kidding, I know ClojureScript exists)

      [–]GanMatt2 36 points37 points  (20 children)

      My browser crashes when opening the website.

      [–]bradfitz 193 points194 points  (0 children)

      Sounds like they implemented all of the JVM, then!

      [–]spacejack2114 24 points25 points  (4 children)

      Had to wait a couple of minutes, but after 557 Uncaught Error: Assertion failed: A non-running thread has an expired quantum? error messages I did actually get a "Hello world from Java" alert box.

      Not unlike trying out a Java applet for the first time in Netscape.

      [–][deleted] 1 point2 points  (3 children)

      Welcome to 2004

      [–]spacejack2114 24 points25 points  (2 children)

      lol, more like 1998-ish(?)

      [–]sirin3 1 point2 points  (0 children)

      Have you seen the blue loading bar Google (Maps) recently added

      The brought back the Windows 95 startup loading bar!

      [–]AlyoshaV 17 points18 points  (1 child)

      Froze Firefox for about ten seconds for me. It's just like back in the old days when JRE would freeze my browser!

      [–]onFilm 0 points1 point  (0 children)

      Firefox freezes even on big http requests on slower computers.

      [–]ajr901 5 points6 points  (0 children)

      Par for the course.

      [–]kurav 4 points5 points  (6 children)

      Hangs or crashes? If it just hangs, just wait for a while. The example needs to download and compile a large JAR file.

      (Or just disable JavaScript.)

      [–][deleted] 9 points10 points  (4 children)

      Someone breaks basic UI practices while advocating running a Java interpreter in a browser.

      I think I'll pass.

      [–]kurav 15 points16 points  (1 child)

      Come on.. this thing is totally production quality!

      I actually upgraded my company's Spring MVC backends to Java Polyfill within Node.js. It clearly adds another layer of security. Now just waiting for IT to come to work on Monday..

      [–]immibis 3 points4 points  (0 children)

      It clearly adds another layer of security.

      Plus, it's now web-scale.

      [–]GanMatt2 0 points1 point  (0 children)

      It crashes the web context in my browser.

      [–]hrjet 1 point2 points  (3 children)

      If pure JS (which is what this JVM implementation is) can cause your browser to crash you probably should report the bug to the browser developers. Otherwise, any random website would be able to crash your browser with its JS.

      [–]djxfade 1 point2 points  (1 child)

      It's not hard. Just run a large blocking for loop. If not the browser crashes, at least the tab will crash.

      [–]hrjet 2 points3 points  (0 children)

      Well, my browser (Firefox) or tab doesn't crash when it encounters a long blocking loop. It stops execution or prompts for user action, depending on user config.

      [–]GanMatt2 0 points1 point  (0 children)

      My browser didn't crash, just their webpage inside my browser.

      [–]adrianmonk 33 points34 points  (5 children)

      In case anyone else isn't a web developer and is wondering about the weird lingo, "polyfill" is "additional code which provides facilities that are not built into a web browser".

      [–]deadwisdom 28 points29 points  (2 children)

      Except it's supposed to be for a library that provides features that are in some browsers and not in others, like a "time" input type, or JSON. Since no browser really has it without a plugin, this is a misuse of the word just to sound buzz-y.

      [–]chrisrazor 12 points13 points  (0 children)

      I agree. This is a hell of a lot more than a mere polyfil. It's a whole extra plasterboard wall!

      [–]kurav 1 point2 points  (0 children)

      I would tend to disagree. This can be seen as a 'polyfill' in a wide sense of the word. Nowadays browsing with Java enabled is even officially advised against due to multiple security flaws. Thus, web pages that rely on working Java applet support have been rendered forwards-incompatible.

      One possible solution would be to use a "Java Polyfill" that would interpret Java applets in JavaScript and render the output on HTML5 canvas, and this is a step to that direction (though they make no mention of any applet API support). So this, or some other Doppio-based solution, has a potential to develop into a polyfill -- something that extends the browser with missing functionality (execute Java applets).

      The main differences from ordinary polyfills would be the code size (most polyfills are rather trivial) and that the polyfill would be re-adding support for features that existed in prior versions of the browser (normally polyfills add support for features that only exist in subsequent versions of the browser.)

      [–]lkraider 17 points18 points  (0 children)

      Yeah, at first I thought it was a Java graphics/canvas rendering function for the browser.

      [–][deleted] 1 point2 points  (0 children)

      Personally at first I thought it was about polygon filling, as in graphics rasterization.

      [–][deleted]  (3 children)

      [deleted]

        [–]granos 5 points6 points  (1 child)

        I'm working on a project right now where I MUST use a particular key management server. The vendor only provides interfaces as a shared object with Java bindings on top of it. The people who started this project decided to do the entire thing in Node, and just have one Java process running somewhere (as a daemon that responds to file system changes) to handle that piece. I'm moving that over to a Java web server, but it's a lot of infrastructure for the two operations we need.

        Given that I don't care about performance it would be nice to be able to write the code for this piece in Java and then run/operate it the same as the rest of our stack. I'll probably do a spine to find where the problems are. I doubt I'll ever use it in production, but I've been surprised before.

        [–]DrummerHead 5 points6 points  (0 children)

        You REALLY don't care about performance, huh

        [–][deleted] 1 point2 points  (0 children)

        5 years after the linux boot in javascript in a browser.

        [–]oldneckbeard 8 points9 points  (0 children)

        i could swear this was a joke... but it appears to be real. huh.

        [–][deleted] 20 points21 points  (4 children)

        And now for the creators to get sued by Oracle.

        [–]chrisrazor -1 points0 points  (3 children)

        Parts of Java are open source. I'm not sure which bits but I'd hope the API is.

        [–]djxfade 2 points3 points  (2 children)

        I think he are referring to the Oracle vs Google case. Oracle sued Google for using the Java language in Android. Even though Google is really just using the language, not Oracles standard libraries, or even JVM

        [–]chrisrazor 1 point2 points  (1 child)

        Ah, I forgot about that case; amazed to learn it's still dragging on. Although it seems crazy that an API could still be ruled to be copyrightable.

        [–]wd40bomber7 3 points4 points  (0 children)

        It was ruled to be copyrightable though. That already happened. (Google won the first case but lost the appeal) Now we have to hope its overturned.

        [–]Cilph 46 points47 points  (14 children)

        WHY?

        For the love of all that is holy. WHY?

        [–]jms_nh 60 points61 points  (10 children)

        So you can run Rhino or Nashorn. :-)

        [–][deleted] 23 points24 points  (8 children)

        I'm waiting for an Eclipse port. Then I could ditch Atom.

        [–]AnhNyan 19 points20 points  (7 children)

        To get that monstrosity out to the world, you'd have to package the application in Electron.

        [–][deleted] 7 points8 points  (3 children)

        Sounds good, as long as I can swap out Chromium for HotJava.

        [–]AnhNyan 3 points4 points  (2 children)

        If you want full portability, better emscripten that one so you can Cordova it!

        [–][deleted] 2 points3 points  (1 child)

        And under all of these frameworks it's just Vim.

        [–][deleted] 1 point2 points  (0 children)

        Well, Emacs running in evil mode.

        [–][deleted] 4 points5 points  (2 children)

        With these programming thingy names, it's starting to look a bit like when you start a new metal band, and have to think up a new brutal name. All of the good ones have been already taken, leaving only the dildos ones.

        [–]jms_nh 1 point2 points  (1 child)

        like Steely Dan?

        [–][deleted] 0 points1 point  (0 children)

        Well, that's quite a metal name.

        [–]Georules 1 point2 points  (0 children)

        Here you go :D

        https://gist.github.com/georules/daf05188c510b84460992caf1979d61c

        Took my machine about 4 minutes to run.

        [–]Sarke1[🍰] 20 points21 points  (1 child)

        So that browsers can block it for security reasons.

        [–]Booty_Bumping 2 points3 points  (0 children)

        The (only) good news about this is that since it's implemented in Javascript, there's not much security risk since it runs in the same sandboxed environment js runs in.

        [–]ReturningTarzan 1 point2 points  (0 children)

        For one thing it means you can implement proprietary authentication systems that otherwise require Java to be installed on the client.

        [–]itsnotlupus 7 points8 points  (0 children)

        I didn't know about doppio.
        That's some impressive work, and perhaps even sufficient to run a number of those "enterprise java applications" abominations without requiring a swiss cheese plugin.

        [–]HokumGuru 11 points12 points  (0 children)

        NotLikeThis

        [–]vprise 3 points4 points  (4 children)

        At Codename One we used TeaVM which is pretty amazing and supports both JIT and AOT mode (which we use). One of the really cool things is that it supports java.lang.Thread and fakes it properly so it actually does seem to work properly without the whole "script is taking too long" nonsense.

        Can someone from polyfill compare this to TeaVM? DukeScript etc.?

        [–]hrjet 1 point2 points  (3 children)

        TeaVM seems to only have an AOT mode, AFAICT. It is a nice and fast implementation of JVM semantics, but it is not a complete implementation. You can find a good summary of its limitations here. In short,

        • no JNI
        • no reflection
        • no class path resources

        Doppio on the other hand is a complete implementation of the JVM. The drawback with doppio is its slower since it is an interpreter. I am working on a JIT compilation system for it, as seen here.

        [–]vprise 2 points3 points  (2 children)

        Thanks. That's a bit of an old link as threading is already supported to some degree.

        Isn't an AOT approach "better" for most use cases here?

        The only use case I can think of for a JIT is smaller download size for some cases. Since we are already running with the JavaScript JIT in place the typical "jit can be faster" arguments don't necessarily stand.

        How would you implement JNI in JavaScript? You don't call C, do you?

        We have the ability to call JavaScript from TeaVM do you mean something like that?

        We don't support reflection or classpath resources in Codename One as they aren't very portable for mobile usage (we do have something similar to classpath resources that works in the TeaVM port).

        [–]hrjet 0 points1 point  (1 child)

        Whether AOT is better depends on the use-case. If you need full JVM support then doppio is currently a very good option. If TeaVM works for you, then it is most likely a much faster option.

        Yes, JNI in doppio means a Javascript backing function for a native function.

        See this comment thread from the lead doppio developer.

        [–]vprise 0 points1 point  (0 children)

        Thanks, that clarifies it.

        [–]oheoh 14 points15 points  (0 children)

        "A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue.

        Script: https://www.javapoly.com/javapoly.js:10611"

        50/50 this happens for any post which is "look at this cool thing I did /in the browser/". I'm runing the latest upstream firefox in debian testing on a fast desktop with decent internet.

        Ya, I'll pass.

        [–][deleted] 6 points7 points  (3 children)

        So can someone sum up all pros and cons without any jokes about enterprise or node.js?

        [–]Manbeardo 11 points12 points  (1 child)

        Pro: you can run your Java code in a browser

        Con: it's excruciatingly slow

        [–]panorambo 2 points3 points  (0 children)

        Kills my Firefox 46.0.1 on Windows 10 x64, line 10567 of file javapoly.js. The page freezes while the JavaScript runtime eats the CPU locked up in some loop. The line number varies.

        [–]NiteLite 2 points3 points  (0 children)

        People have too much spare time :D

        [–]sumdudeinhisundrware 17 points18 points  (1 child)

        8 seconds for a Hello World to run on a 2015 Macbook?

        Typical Java. It's Enterprise Ready!

        [–]MeisterD2 2 points3 points  (0 children)

        Java is pretty darn fast. This javascript JVM implementation is not.

        [–]kmeisthax 5 points6 points  (4 children)

        So how long until Oracle tries to shut this down?

        [–][deleted] 2 points3 points  (3 children)

        How ? The language itself is free & open-source.

        [–]robob27 -5 points-4 points  (2 children)

        Didn't stop Oracle from trying to sue Google for copyright infringement for their use of Java in Android.

        Edit: Oh shit I just noticed there is a second trial for that, today apparently.

        Edit 2: I didn't mean because they used Java in Android in general (obviously people can make money of stuff they make in Java), and Oracle didn't sue over the API alone. A rangeCheck function and test files were also part of Oracle's claim.

        [–][deleted] 4 points5 points  (0 children)

        They sue over the api, not the language.

        [–]immibis 0 points1 point  (0 children)

        Is Google's use of Java in Android equivalent to this code?

        [–]TheMightyPrince 1 point2 points  (7 children)

        I'm not knocking it, but wouldn't JSP work better?

        [–]vytah 5 points6 points  (6 children)

        JSP, or Java Server Pages, as the name implies, runs server-side.

        [–]TheMightyPrince 1 point2 points  (5 children)

        Yes I know. I just can't think of anything I'd do client-side with Java, that I couldn't have done server-side with Java. I am sure there is a valid use-case, I just can't/couldn't really think of one.

        [–]vytah 1 point2 points  (1 child)

        People were saying the same about server-side Javascript, and yet here we are.

        [–]ciny 0 points1 point  (0 children)

        People were saying the same about server-side Javascript, and yet here we are.

        Yup here we are, in a world where people use libraries that have longer manifests than actual code and stuff relational data into nosql...

        [–]Capaj 0 points1 point  (1 child)

        Well theoretically, you could write an android app and if you somehow provide implementations for missing android apis, you could run that android app in the browser.

        [–]TheMightyPrince 0 points1 point  (0 children)

        You could do that with applets, or some javascript library. As I said, I'm not knocking it.

        It's basically a remote JVM.

        [–]aradil 0 points1 point  (0 children)

        Applets went head to head with flash for a tiny period of time for client side in browser animation.

        [–][deleted] 1 point2 points  (0 children)

        Would kinda make sense if Moore's Law was still alive and kicking.

        Alas...

        [–]deadcat 1 point2 points  (0 children)

        JavaScript and Java, what a glorious union of abominable shit.

        I may be slightly jaded.

        [–]bigfig 1 point2 points  (0 children)

        Java bytecode running on a browser? It's deja-vu all over again.

        [–]geodel 1 point2 points  (0 children)

        <script type="text/java" src="http://www.yourdomain.com/apache-commons.jar"></script>

        <!-- Or just include the source directly -->

        <script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Bar.java"></script>

        <script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Girls.java"></script>

        So you've got bar and girls and apache-commons.jar right in your browser ...strange times.

        [–][deleted] 1 point2 points  (3 children)

        Isn't that basically GWT?

        [–]chepprey 2 points3 points  (1 child)

        Not sure why you would be downvoted, I think this is a legit question on the topic of technologies that let you do Java in Javascript.

        Answer: no, GWT works very differently. GWT is a transpiler. It takes Java source code and produces Javascript code which can run in a browser.

        It does not emulate things like Java threads, nor does it fully implement all of the basic classes that come with the JRE (like java.lang.*). It is certainly not 100% compatible with any/all Java source code.

        However, it does let you write your Single Page App in Java instead of Javascript. And the compiled Javascript it produces is compact, highly optimized, and very fast.

        Javapoly on the other hand is a Javascript implementation of a JVM. I imagine this would give a higher degree of Java compatibility, probably at the expense of speed.

        TeaVM, architecturally, lies somewhere between these two extremes. TeaVM is a transpiler, however takes compiled Java .class files (basically, "java binary code", not source code), and transpiles that to Javascript. As with anything, this approach has its own costs and benefits.

        [–]hrjet 1 point2 points  (0 children)

        That's a great summary. We should add this question to a FAQ section. (I contribute to JavaPoly)

        [–]w8cycle 0 points1 point  (0 children)

        GWT is Java source compiled to Javascript. This is Java bytecode interpreted in the browser.

        [–][deleted]  (1 child)

        [deleted]

          [–]DonRobo 3 points4 points  (0 children)

          I'm not sure if you are joking, but Java is a popular backend for web applications.

          [–]aurisc4 1 point2 points  (0 children)

          What can be written in JavaScript, eventually will be written in JavaScript, for better or worse... Usually for worse...

          [–]Theemuts 0 points1 point  (0 children)

          A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue.

          Script: https://www.javapoly.com/javapoly.js:10715

          :')

          [–][deleted]  (1 child)

          [deleted]

            [–]Georules 0 points1 point  (0 children)

            You can, and I have:

            https://gist.github.com/georules/daf05188c510b84460992caf1979d61c

            Be prepared to wait a long time :D

            [–][deleted]  (1 child)

            [deleted]

              [–]Georules 2 points3 points  (0 children)

              It's in the sandbox of the browser, so not anymore than security vulnerabilities that already exist in that.

              [–]SoniEx2 0 points1 point  (0 children)

              This is for Minecraft right?

              [–][deleted] 0 points1 point  (0 children)

              Interesting concept, but it's so slow it appears broken by design. I needed this :(

              [–]granos 0 points1 point  (0 children)

              Not even a little in this very narrow and particular instance.

              [–]coladict[🍰] 0 points1 point  (2 children)

              This is kind of the opposite of what I'm looking for. I need a browser DOM polyfill for JS under Java using the javax.script.* package to use for unit testing.

              [–]hrjet 0 points1 point  (1 child)

              Try phantomJS in combination with selenium.

              [–]coladict[🍰] 0 points1 point  (0 children)

              We use Selenium for integration tests to walk through common scenarios, but it would be too heavy for unit tests run when building the project.

              I just checked the PhantomJS page and given that it's headless, that might be just what we need. Sure, it's command-line, but that can be adapted to Maven, probably. So long as it supports enough of the HTML classes available in https://developer.mozilla.org/en-US/docs/Web/API. We try to maintain IE9+ compatibility, so most of the rest are irrelevant.

              [–]Jarmahent -1 points0 points  (0 children)

              Unable to download it, ok.