compile both Java and Scala at runtime - java

I'm trying to compile some interdependent java and scala code at runtime in scala. I thought scala runtime compiler(reflection API) would compile Java as well just like scala's compiletime compiler, but it turns out it does not recognize Java's syntax.
Then I tried compiling java code with Java reflection and add them to the class path, then compile the scala code that depends on the java code compiled earlier.
Is there a way to do this more robustly for multiple files mixed with both Java and Scala? Java code might depend on the Scala code and some scala code might depend on some Java code as well...

Related

How to compile Java to WASM (WebAssembly)?

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

How to compile and run scala code at runtime from a java program?

I already have a program that uses JavaCompiler api for compiling Java code at runtime. Can I use the same compiler for scala code? If not, what is the best way to compile scala code in a Java program?
Thanks
See the following answer, and in it, also the answer linked by #mastov. You may, however, be able to use the new process handling capabilities in Java 9 instead of ProcessBuilder.
Compile Scala code to .class file, in Java
On Java 9 Process API:
https://www.javaworld.com/article/3176874/java-language/java-9s-other-new-enhancements-part-3.html

Bootstrapping java and python compilers

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.

Is it possible to compile to machine code in Java without an external program?

First of all, this is not your standard "I want to compile Java code to machine code" question.
I'm working on a compiler written in Java, that will translate a certain language (in my case: Brainfuck) to x86 Assembly, after that I'm currently planning to use NASM and GCC to produce machine code.
Seeing as the HotSpot JVM can compile Java bytecode to machine code, I assume there is some mechanism available to compile source code of type A to machine code.
Is there any way to use this in a compiler written in Java? My main goal is to explore the possibility of writing a compiler in Java without relying on external programs, for example GCC and NASM, being available on the path. I do need a C Compiler because I'm linking with the cstdlib as I'm using those functions in my x86 Assembly code.
To clarify, I'm doing the following currently:
Write x86 Assembly to a bf.asm file.
Transform Assembly to Object code with nasm -f win32 bf.asm.
Link the Object code with Windows OS and cstdlib libraries with gcc -o bf bf.obj.
I'm searching for the possibilities of replacing the need of using nasm and gcc in steps 2 and 3 and instead do those with Java code.
Seeing as the HotSpot JVM can compile Java bytecode to machine code, I assume there is some mechanism available to compile source code of type A to machine code.
This does not follow.
The JIT compiler compiles Java bytecodes to native code. It does not understand anything other than Java bytescodes. And bytecodes are not "source code". (They are actually a form of machine code ... for an abstract computer ... a Java virtual machine.)
In short, there is no mechanism available as part of the JVM for compiling source code to machine code.
And, as it turns out, the JIT compiler is not designed for generating native code in files that something else could use. The native code is in the form of raw machine instructions in blocks of memory. No symbol tables. No relocation information. Probably full of hard-wired calls into other parts of the JVM. Basically it is designed for execution in the currently running JVM, not for anything else.
Is there any way to use this in a compiler written in Java?
The JIT compiler is not applicable to your problem ... unless you write your compiler to generate valid Java bytecodes. And if you did that, then the JVM could run your code, and the JIT compiler would at some point compile your bytecodes to native code.
Bottom line: if your goal is to generate native code that can be run as or linked to a separate executable,
the JIT compiler is of no use to you, but
you could use the JVM including the JIT compiler as your execution platform, by generating bytecodes, and
you could use also ordinary Java programming to implement your compiler or assembler, including a component that generates and emits native code in a format that is appropriate to your needs.
Is it possible to compile to machine code in Java without an external program?
Yes. Write an x86 assembler in Java.
If you're generating x86 assembly, the next step is obviously to assemble it.
Seeing as the HotSpot JVM can compile Java bytecode to machine code, I assume there is some mechanism available to compile source code of type A to machine code.
Just because HotSpot can convert Java byte code to x86 machine code, doesn't mean it can convert any other input to the same.
You're essentially asking if one can use a Java JITter to assemble x86 asm. It makes no sense.
I do need a C Compiler because I'm linking with the cstdlib
No, you need a linker. Nothing about linking necessitates a compiler.

Why Eclipse doesn't have path to java compiler?

As I know, java source code is compiled into class files by the java compiler (javac.exe); then these class files are put into JVM to interpret using java.exe
But Eclipse is only using javaw.exe. So, I think javaw.exe is equivalent to javac + java
But some references say javaw.exe nearly equivalent java.exe. thus Eclipse only has the interpreter progress. Compiler process takes place where, how?
How should I understand this?
Eclipse does not compile files using javac. It has its own, independent implementation of a Java compiler, complete with its own set of error messages—and bugs.
Eclipse still needs access to all the standard library classes against which it compiles Java code.
The Java runtime which runs the Eclipse IDE process is not related to the JDK used to compile the code against. For all that matters, Eclipse could be a native application written in C++ and that wouldn't stop it from being able to compile Java.

Categories

Resources