2022-02-18 18:35:09 -05:00
# Java to JavaScript (java2js)
2021-09-23 01:41:44 -05:00
2022-02-18 18:35:09 -05:00
`java2js` can translate simple Java programs to JavaScript and runs them using a JavaScript based JDK.
2021-09-23 01:41:44 -05:00
2022-02-20 13:52:18 -05:00
Try out the `demo.html` and `ide.js` , a barebones implementation.
2022-02-18 18:35:09 -05:00
2022-02-20 13:52:18 -05:00
## Java classes included in the java2js JDK
2022-02-01 21:32:29 -05:00
| java.io | | |
| ------- | ----------- | ----------- |
| File | InputStream | PrintStream |
| java.lang | | |
| --------- | ------------- | --------- |
| Boolean | Byte | Character |
| Double | Exception | Float |
| Integer | Long | Short |
| String | StringBuilder | System |
| Thread | Throwable | |
| java.security | | |
| ------------- | --- | --- |
| MessageDigest | | |
| java.time | | |
| --------- | --- | --- |
| Instant | | |
| java.util | | |
| ------------------ | ---------------------- | ----------- |
| AbstractCollection | AbstractList | AbstractMap |
| AbstractSet | ArrayList | Arrays |
| Collections | Formatter | HashMap |
| HashSet | IllegalFormatException | Itr |
| LinkedList | Random | Scanner |
| Set | Stack | |
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
## Features
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
The java2js transpiler supports:
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
- imports
- static classes
- static methods
- array literals
- arrays with initial size
- two dimensional arrays
- lambda arrow functions
- triple quotes
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
## API
### await jdk.init(root)
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
This function imports the standard java.lang classes into the global scope. You must use it before translating or running files.
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
- root (optional) path to the parent folder of the JS `jdk` folder, by default it is `.` (the current directory) you could also link to this package online 'https://unpkg.com/java2js'
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
### await jdk.transpile(javaFile)
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
Translates a Java class and loads it.
2021-11-18 15:01:15 -05:00
2022-02-20 13:52:18 -05:00
- javaFile is a String with a Java class in it
2021-09-23 01:41:44 -05:00
2022-02-20 13:52:18 -05:00
returns a String with the JavaScript transpilation of the Java class
2022-01-17 08:04:32 -05:00
### jdk.run(jvmArgs)
2022-02-01 21:32:29 -05:00
Runs the main method with optional JVM arguments.
2022-01-17 08:04:32 -05:00
2021-09-23 01:41:44 -05:00
## Known limitations
2022-02-18 18:35:09 -05:00
- not very good error reporting
- casting to int (truncation) requires parenthesis around the number being cast `int x = (int) (Math.random() * 100);`
2021-09-23 01:41:44 -05:00
2021-11-18 15:01:15 -05:00
- no support for method overloading, though a workaround might be possible by making a function with the og name route to each of the variations of the overloaded function
2021-09-23 23:26:42 -05:00
2021-11-18 15:01:15 -05:00
- no support for private/public methods yet, though this could be done since they are included in modern JavaScript classes
2021-09-23 23:26:42 -05:00
2022-02-01 21:32:29 -05:00
- no three dimensional arrays
- no third level static classes
2022-02-18 18:35:09 -05:00
- no method can be named `.length()` because it will be replaced with `.length` for getting the length of Strings in JS
2022-02-01 21:32:29 -05:00
2022-02-20 13:52:18 -05:00
## Why did you make this?
I created this package so intro level computer science students could write Java code but still use my [QuintOS ](https://github.com/quinton-ashley/quintos ) retro game engine library, which is web based.
## Credits
This project builds upon the [java-to-javascript ](https://github.com/wyattades/java-to-javascript ) transpiler by @wyattades . I also did a modern JS implementation of the fundamental classes in the JDK (Java Development Kit), which I got the inspiration to do from [java2javascript ](https://github.com/java2script/java2script ) by @BobHanson , @zhourenjian , @abego , and others.
2021-09-23 23:26:42 -05:00
## Contribute
2022-02-01 21:32:29 -05:00
I've only done a partial implementation of the Java 17 JDK in JavaScript, so if you're interested in adding more please go for it and submit a pull request!