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

79 lines
3.2 KiB
Markdown
Raw Normal View History

2022-02-01 21:32:29 -05:00
# Java to Javascript
2021-09-23 01:41:44 -05:00
2022-02-01 21:32:29 -05:00
I've built on top of the [java-to-javascript](https://github.com/wyattades/java-to-javascript) transpiler by @wyattades and got inspiration from the barebones JDK (Java Development Kit) implementation in [java2javascript](https://github.com/java2script/java2script) by @BobHanson, @zhourenjian, @abego, and others.
2021-09-23 01:41:44 -05:00
2022-02-01 21:32:29 -05:00
I created this project 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. Yes, I went to all this trouble just so my students don't have to run their programs in a boring Java console! To achieve this I've been working on a JDK implementation in modern Javascript.
## Java Classes Included in the JS JDK
| 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
## API
### jdk.init(root)
2022-02-01 21:32:29 -05:00
- root (optional) can be a local path or url to the parent folder of the JS `jdk` folder, by default it links to 'https://unpkg.com/java2js' (this package) so you don't need to change it
2021-11-18 15:01:15 -05:00
This function imports much of the standard Java lang classes into the global scope. You must use it before translating or running files.
### jdk.translate(javaFile)
- javaFile is a string with contents of a Java file
returns the Java file translated into JavaScript
2022-01-17 08:04:32 -05:00
### jdk.load(translatedJSFile)
2021-11-18 15:01:15 -05:00
- translatedJSFile is the translated JS class to run
2021-09-23 01:41:44 -05:00
2022-01-17 08:04:32 -05:00
Loads the JS class file but doesn't run the main method.
### 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-01 21:32:29 -05:00
- casting to int truncation workaround 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
- not much error checking
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!