Sugary goodness

Add some candy to your code: JSweet transpiles from Java to JavaScript

Sarah Schlothauer
© Shutterstock / Veronika Vankova

JSweet is a transpiler that takes code from Java to JavaScript with over 1,000 available JavaScript libraries. See what it can do and unwrap something sweet with its Maven repository.

From Java to JavaScript, it might be easier than you thought. The dream of translation is innate. We all wish to be able to speak to people from across the globe without having to learn a new, complex language, and this is true even with programming. Are we at the “click a button and go” level of transcription yet? Well, no. But JSweet is a helpful tool that’s almost just as good as being multilingual.

JSweet is a Java to JavaScript transpiler. Time to see what it can do!

Let’s unwrap

JSweet uses source-to-source compiles code to TypeScript and JavaScript for availability in browsers, mobile web, or Node.js. All you need is Maven, Git, and Node.js and you can get started with cloning the project from GitHub.

From GitHub, here’s the reasons why JSweet should be on our radar:

  • JSweet is safe and reliable. It provides web applications with type-checking and generates fully type-checked JavaScript programs. It stands on Oracle’s Java Compiler (javac) and on Microsoft’s TypeScript (tsc).
  • JSweet allows you to use your favorite JS library (JSweet+Angular2JSweet+threejsIONIC/Cordova, …).
  • JSweet enables code sharing between server-side Java and client-side JavaScript. JSweet provides implementations for the core Java libraries for code sharing and legacy Java migration purpose.
  • JSweet is fast, lightweight and fully JavaScript-interoperable. The generated code is regular JavaScript code, which implies no overhead compared to JavaScript, and can directly interoperate with existing JavaScript programs and libraries.

There is also a helpful debug mode to prevent any mistakes from going live. And for those wondering, JSweet is not just an emulator. You can use the newly generated code as your source code. It works like JavaScript because it is JavaScript, no secret Java to be found underneath.

SEE ALSO: TypeScript: Everyone’s type of JavaScript


Not just a sugary treat

Candies aren’t just an impulse buy in the grocery store line. In JSweet, candies are how the transcription works.

Let’s take a look at the Getting Started guide and find out what these snacks are: “A JSweet candy is a Maven artifact that corresponds to a Java/JavaScript library/framework/API, which you can use in your JSweet application simply by adding a dependency to it in your pom.xml.”

A candy is a Maven artifact with three ingredients:

  1. Java API and/or implementation,
  2. the corresponding TypeScript definitions (necessary for the TypeScript to JavaScript compilation),
  3. and, optionally, the compiled JavaScript implementation, to be used for running the library/framework/API.

SEE ALSO: Freelancer Fast 50: Blockchain, JavaScript and AI skills on the rise

There are already available candy libraries and if those aren’t enough, get cooking and build your own candy and add it to the library. There are several repos on GitHub of JSweet candies as well ready to deploy.

Of course, there are some limitations to JSweet. After transcription, some parts of the code may have to be rewritten and some Java libraries are not supported.

Getting started

Try JSweet live sandbox and view how it works, such as transcribing a 3-D logo rotate, quick start, or basic jQuery. There are also many examples you can run on your browser to see more of JSweet’s capabilities, including canvas drawing, form input, Angular example, and Backbone todos.

Have you used JSweet? What do you think of this transpiler?




Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is the editor for She received her Bachelor's degree from Monmouth University, West Long Branch, New Jersey. She currently lives in Frankfurt, Germany with her husband and cat where she enjoys reading, writing, and medieval reenactment. She is also the editor for Conditio Humana, an online magazine about ethics, AI, and technology.

1 Comment
Inline Feedbacks
View all comments
Anna Martinez
Anna Martinez
3 years ago

Thank you for sharing an amazing article @Sarah Schlothauer.

I was not aware of this Jsweet Candy. Again thanks Sarah for sharing this article, Nowadays Node.js is also a trending JS for a programmer you should also add in this in the article.