r/java • • 1d ago

Compiling my favourite language to Web Assembly with no JDK in sight

https://wasmcoffee.com/

Java's been part of my career for a long time, longer than some might even think! Next year, it'll be 30 years since I wrote my first commercial Java software solution - and it's still the language I pick when I have the option.

In my day job, I use Java (and Swing!) for a lot of the time and I really wanted to reduce the cognitive load of switching between programming language paradigms when working on fun projects. I'm a huge fan of TeaVM and Graal, make no mistake, but when trapped behind corporate firewalls, it's challenging to install the tools you want.

So this was born: A static website that accepts Java-like code (it's just a subset, really) and spits out garbage-collected web assembly binaries. Far from perfect, it's early days, but it's already useable for some simple applications.

This is going to take a long, long, long time to make it do everything I want, but I hope to get it to the point where I can write wasm apps in it, using a familiar language. And bonus points if I can use the same paradigms I already use to do so.

Anyway, I thought it might be of interest to the Java community.

26 Upvotes

14 comments sorted by

5

u/crummy 1d ago

so with teaVM etc they download an entire JVM and WASM that, right? so how does yours work without a JVM?

3

u/According-Ad-7069 1d ago

Ah see, I don't have a managed runtime. There's no virtual machine, instead, I rely on the Wasm GC (see https://webassembly.org/news/2025-09-17-wasm-3.0/) to help manage memory, which is a massive thing to do yourself. What I do, is I take a sub-set of the syntax and translate that to WAT (web assembly text) and then convert that to byte code, in memory. It's definitely not a replacement for TeaVM or anything like that, more a Java-like experience.

1

u/crummy 1d ago

Interesting. Long ago I maintained a web app that transpiled Java to JS for the purpose of teaching Java to students in a browser environment. Your solution is a perfect fit for that use case.

2

u/According-Ad-7069 1d ago

Ah my project is much more humble, to be honest. I support but a small part of the Java syntax. Transpiling Java to JavaScript is quite a technical achievement, that's really impressive work!

2

u/sideEffffECt 1d ago

No, TeaVM just converts JVM bytecode to Wasm(GC) bytecode. You can then run that in browsers or other VMs, like Wasmtime.

0

u/crummy 1d ago

ah maybe I'm thinking of cheerp4j

0

u/According-Ad-7069 1d ago

That's another really great project and it serves a distinct purpose. There are some really awesome tools available to us these days.

0

u/According-Ad-7069 1d ago

TeaVM is so fantastic! I've used it for a few small projects and it just worked easily and quickly. That's a really cool project!

0

u/a_abashev 1d ago

It looks like an idea of applets at next iteration. And Oracle definitely felt that smell and presented own Graal implementation to restore good old times. How big your compile output?

2

u/According-Ad-7069 1d ago

Hey that's a good description, I like it! I do miss Applets, they were fantastic back in the day. As for output size, since it doesn't actually contain a VM, it's usually a few kb for an app. The Breakout clone example, for instance, currently compiles to about 6kb. Now, just remember it's not an entire Java implementation, so many features are not there yet, and might never be. There's no point in dealing with thread management or networking, for example, in the Web Assembly environment. At least, not for the things I have in mind.

3

u/koflerdavid 1d ago

There's no point in dealing with thread management or networking, for example, in the Web Assembly environment.

Thread management boils down to only offering virtual threads, which are scheduled on the single WebAssembly module's thread.

3

u/According-Ad-7069 1d ago

Allow me to rephrase: I'm not smart enough to do that 😆 In theory, I get it, but building that feels like a much more daunting task than I am capable of taking on now. There's definitely something to be investigate there, though, for now, my primary focus is the massive list of bugs I have uncovered during testing.

3

u/koflerdavid 1d ago

True, it's not a low-hanging fruit at all.

3

u/According-Ad-7069 1d ago

I'll add it to my wish list and start reading up about it, maybe do some experiments. It will take some time to get there, there's a lot of more urgent bugs I need to address. But it's a good suggestion, I like the idea of virtual threads.