Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run java program without JVM?

Tags:

java

jvm

I have simple java programm that will just print Hello World.Can it be possible to print without using JVM installed in a machine ?But compiler is there.

like image 230
Ganesh Hegde Avatar asked Sep 02 '14 07:09

Ganesh Hegde


3 Answers

You can compile Java Byte Code to Machine Code using something like this: http://en.wikipedia.org/wiki/GNU_Compiler_for_Java

Or you can use any of the MANY Java to C/C++ converters out there to make a C/C++ version, and compile that. (Search Google for "Java C Converter")

Warning: I have never tried any of these, including the linked GNU Compiler, so I cannot make any attestations to their usefulness or quality.

like image 144
SplinterReality Avatar answered Sep 19 '22 15:09

SplinterReality


@SplinterReality already pointed you to some compilers (googling "java bytecode to native code compiler" will show you some more options).

I will just expand on that seeing some people are a bit confused:

The JVM is a program that takes java bytecode, which you get by running javac on your java source code (or you generate it in some other fashion). Bytecode is just a set of instructions that the JVM understands, it's there to give you a consistent set of opcodes which are platform independent. It's the JVM's job to then map those opcodes to native instructions, that's why JVMs are platform dependent. That's why when you compile your java source code to java bytecode you can run it on every platform.

That's why, whether you have java source or bytecode, you can take it and just compile it directly to native code (platform dependent, actually sometimes that's exactly what the JVM, and to be more precise the JIT, does - it compiles stuff to native code when it sees the need to). Still there's more to the JVM than just bytecode interpretation - for instance you need to take care of garbage collection.

So the answer is: yes, but do you really want to do it? Unless you want to be running it on some embedded systems I don't really see any reason to.

like image 45
Mateusz Dymczyk Avatar answered Sep 20 '22 15:09

Mateusz Dymczyk


Yees, it is possible to run a java program without a JVM, albeit with limitations. Aside from the http://en.wikipedia.org/wiki/GNU_Compiler_for_Java , there is the GraalVM native-image generator, which could be used : https://www.graalvm.org.

like image 22
Wolfgang Grinfeld Avatar answered Sep 19 '22 15:09

Wolfgang Grinfeld