I would like to convert these Java functions to JavaScript: CircleFitter.java. I don't need the file reading portions though.
JSweet seems like a good option. So, I tried pasting the contents of the java file into the online JSweet sandbox, but I get these errors:
Line 58: module 'java.io' has no exported member 'FileReader'
Line 58: property 'FileReader' does not exist on type 'typeof io'
Line 58: module 'java.text' has no exported member 'DecimalFormat'
Line 58: property 'DecimalFormat' does not exist on type 'typeof text'
Line 58: module 'java.text' has no exported member 'DecimalFormatSymbols'
Line 58: property 'DecimalFormatSymbols' does not exist on type 'typeof text'
Line 58: module 'java' has no exported member 'awt'
Line 58: property 'awt' does not exist on type 'typeof java'
Line 69: property 'in' does not exist on type 'typeof System'
Line 76: property 'exit' does not exist on type 'typeof System'
Line 125: property 'exit' does not exist on type 'typeof System'
Line 128: property 'exit' does not exist on type 'typeof System'
These errors seem to fall into 2 categories:
- module __ has no exported member __
- property __ does not exist on type __
I don't use Java, so I have no idea how to get around these. I appreciate the help.
JSweet has limited support for Java APIs. The sandbox uses the j4ts emulation library, which does not provide any implementation for the java.text package, nor java.io. It also does not support System.exit (because it does not make sense in a browser most of the time). Finally, any support to java.awt is extremely difficult and can only be very partial. So the best way, really, it to avoid using unsupported Java APIs and replace them with JavaScript APIs (JSweet provides access to them).
However, with JSweet, it is possible to compile some Java libraries to TypeScript/JavaScript. The J4TS organization provides some examples on how to compile and support some Java libs. If your program uses unsupported libraries, you have several alternatives to fix the problems:
JSweet v2 is not released yet, but it provides an API to extend the transpiler and support more APIs natively. I advise to use it when available.