1
0
mirror of https://github.com/quinton-ashley/java2js synced 2024-12-29 10:11:54 +01:00
java2js/README.md

103 lines
3.9 KiB
Markdown
Raw Normal View History

2022-02-18 18:35:09 -05:00
# Java to JavaScript (java2js)
2021-09-23 01:41:44 -05:00
2022-02-21 12:13:53 -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-21 12:13:53 -05:00
[Try it out!](https://quinton-ashley.github.io/java2js/) The demo files are located in the docs folder of this repository, its a barebones implementation that shows what is possible with java2js.
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
2022-02-23 11:26:23 -05:00
- primitive type casting
2022-02-20 13:52:18 -05:00
- static classes
- static methods
- array literals
- arrays with initial size
- two dimensional arrays
- lambda arrow functions
2022-02-21 11:11:18 -05:00
- triple quote strings
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-23 11:26:23 -05:00
- root: (optional) path to the JS JDK folder, by default it is `./jdk` (the java2js JS JDK), for online use on codepen or similar code sharing sites you can use this link as the root path: 'https://unpkg.com/java2js/jdk'
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-23 11:26:23 -05:00
- javaFile: 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-23 11:26:23 -05:00
Runs the main method
- jvmArgs: (optional) String array of 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
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!
2022-02-21 11:32:58 -05:00
## Commercial Use
This project is licensed under the copyleft GNU GENERAL PUBLIC LICENSE 3.0 (only) license. If you'd like to request legal permission to use this code in a commerical product or closed source project please email me (qashto@gmail.com) with a proposal of how you would like to use `java2js`.