Ganesh Hegde
Ganesh Hegde

Reputation: 227

How to run java program without 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.

Upvotes: 6

Views: 15172

Answers (4)

Wolfgang Grinfeld
Wolfgang Grinfeld

Reputation: 1008

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.

Upvotes: 1

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

@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.

Upvotes: 3

T.G
T.G

Reputation: 1921

If that program is a one file source code (not compiled in bytecode) then you can ;) Here or here. Here will be fine as well ;)

Just put it there and press compile and run.

But it will work only with simple stuff only, and you have to have source code.

I would not be surprised if there would be a site that would allowed to upload class from users PC

Upvotes: -1

SplinterReality
SplinterReality

Reputation: 3410

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.

Upvotes: 11

Related Questions