I wonder can I use Java and compile it to WASM (WebAssembly)?
The list https://webassembly.org/getting-started/developers-guide/ has no Java, Kotlin
GraalVM WASM project https://www.graalvm.org/reference-manual/wasm/ is for running wasm inside JVM, not for running Java projects within WebAssembly.
Here are a few compilers that can do this for you:
https://github.com/konsoletyper/teavm (most popular & my own reccomendation: https://teavm.org/)
https://github.com/i-net-software/JWebAssembly (only to webassembly, the others can do webassembly and javascript)
https://github.com/mirkosertic/Bytecoder
https://github.com/leaningtech/cheerpj-meta
Do note, that all of them have their limitations, most commonly that every Java class in Java's standard library won't work well with it (for example, TeaVM and others have problems with reflection).
They also require pipeline integration -- make sure to be using a build tool these compilers support, such as gradle or maven
Related
I was wondering why https://www.scala-lang.org/download/ says
Scala is unusual because it is usually installed for each of your Scala projects rather than being installed system-wide.
Why is it not the case for Java? Can this way work for Java projects?
Thanks.
If you use SBT, on the basis of scalaVersion specified in project's build.sbt, it will treat Scala like other regular library dependencies and download them under
.ivy2/cache/org.scala-lang/scala-compiler
.ivy2/cache/org.scala-lang/scala-library
.ivy2/cache/org.scala-lang/scala-reflect
similarly to regular library, say, cats
libraryDependencies += "org.typelevel" %% "cats-core" % "2.0.0"
which would end up under
.ivy2/cache/org.typelevel/cats-core_2.13
We can also have system-wide installation of Scala under, say /usr/local/bin, however SBT will not use that and will read from ~/.ivy2/
One thing is that, when talking about Java we need to distinguish JRE and JDK.
The JRE has JVM, so it's only the runtime platform for Java bytecode, JDK has compiler and other development tools.
Scala instalation already comes with compiler, interpreter, etc.
Both can run their own compiled code, but Java needs additional jars on classpath to run Scala programs (nicely described here).
Another and probably the main reason behind having separate installation of Scala for each project is that Scala is not fully compatible between its versions, i.e. particular version of Scala needs specific version of libraries.
(where Java is backward compatible)
The wikipedia page on compiler bootstrapping lists python and java among the languages whose compilers are bootstrapped. Aren't javac and cpython implemented in c?
Why are these languages listed on that page?
javac is written in Java, and compiles Java source to bytecode. The source is available online. So javac was bootstrapped. You may be confusing javac with the JVM as the latter is written in C and is used to compile bytecode to machine code and run it.
Same thing for Python. The compiler is not the same thing as the execution environment.
With Java, there is the distinction between the Java compiler (javac) and the Java Virtual Machine (java). Unless an operating system and/or CPU would have support to run Java applications natively, you will need a JVM to run any Java application.
On the other hand, tools such as Apache Maven are written in Java, and invoke the Java compiler as well. This is not done by invoking a Java package. The page on the Maven Compiler Plugin describes how this is done, and how you can configure the plugin to use the javac executable instead.
There will always be the need for some native code. But nothing is stopping you from writing all of the code that produces Java bytecode in Java itself. After all, what it does is convert text into bytes. You can do that in any programming language, including Java.
I've looked at several questions and read through a couple of tutorials but MakeFile is still a bit of a confusing concept to me.
From what I understand, it is essentially a set of rules for building up Unix commands to compile and run the code?
So far, I have been just running my Unix commands as such:
>> javac Main.java SomeClass1.java SomeClass2.java
>> java Main input_file.txt
because my Main function takes in an input_file.
I want to be able to make this more efficient by using Make, but I am having trouble with understanding the concepts.
Any help is much appreciated!
Thank you!
Make is a build tool - a piece of software to compile the source code of software projects into an executable.
When you are creating small, simple programs, you don't really need a build tool. You can just compile your code by running the compiler javac on the command line. But when you start working on a larger project with many source files, it's going to be too cumbersome to compile all the source files by hand. You'll want to use a build tool. Besides compiling your code, a build tool can help you perform other tasks, such as automatically running unit tests and automatically managing dependencies (libraries that your program needs).
For a Java project, consider using a Java build tool such as Apache Ant, Apache Maven or Gradle. Those are the de-facto standard build tools for Java projects, and the big Java IDEs (Eclipse, IntelliJ and NetBeans) have support for these tools.
Make is mainly used for C and C++ projects and is not very well suited for Java.
Maven Compiler Plugin documentation states:
The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse
And indeed when forceJavacCompilerUse is not specified in our build there are some build errors, for example when the code references the com.sun. packages (legacy, we know that its a bad idea...)
What are other differences between these two compile modes in general and with maven? Are there any output differences that one should know?
javac (as "java compiler") is an executable, which could be theoretically even a platform-dependent executable or a script. This is called to compile a .java to a .class.
On windows is its name javac.exe, and it is normally somewhere below C:\Program Files*\jdk*\bin.
This compiler was developed in java as well. That means, if we start this .exe, a new java virtual machine need to be started to run it. This is slow.
But, because it was written in Java, there is a much faster alternative to it: from our already running jvm, we simply import its main class (f.e. javax.tools.JavaCompiler or such) and call this. This doesn't need to start an unneeded jvm. That is what maven does. Simply 10 yrs was them enough to make this correctly. :-)
Of course it has some fallbacks as well. To most probable cause is that in the case of the internal compiler it needs to run from the same jvm and in the same namespace as the maven core. Also specifying an alternate jvm is impossible, and there could be some side effects as well resulting from the namespace collisions. But they are very improbable, because both of them is well-designed software.
I read some tutorials about making Eclipse plugins, but every text was just about Java coding. Does it really have to be Java or there is some way to write a plugin in some other JVM language such as Scala or Clojure?
You can use all languages that are based on JVM. You need to simply add a dependency jar (e.g. Scala dependency).
Tested live, so it must work!
Current version of Scala IDE is written in Scala