12

Is it possible to compile java source code into native exe like C++? Like C++ all headers files are included during compilation, all java library files that are required should be attached in that exe, and this exe should not be a bytecode but native exe instead and run without jvm.

So all I want to know is something like.. if I can replace all C++ syntax with Java syntax and compile to an exe file like one created by C++ compiler which run directly.

Note: I am not talking about packers that wraps java classes in exe and ultimately requires jvm.

Kashyap Kansara
  • 161
  • 1
  • 1
  • 5
  • And separately, make the first sentence actually your question, as that's what shows up on the front page. – T.J. Crowder Jan 31 '15 at 08:55
  • No you can't that defeats the purpose of platform independence of Java. Why don't you use C++ instead? – Pradeep Simha Jan 31 '15 at 08:57
  • *"...this exe should not be a bytecode but native exe instead and run without jvm..."* ***Why***? As opposed to the very good wrappers that make it *seem* that way to your users? (About which there are a couple of dozen questions and answers here on SO.) A difference that makes no difference *is* no difference. – T.J. Crowder Jan 31 '15 at 08:58

5 Answers5

7

The only to-native-code Java compiler that I'm aware of is The GNU Compiler for the Javatm Programming Language.

But it's extremely unlikely you really need a to-native-code compiler. Packers like the ones you've dismissed can make your program entirely self contained, including installing a private JVM, seamlessly. I strongly recommend you check out the options in this question and its answers.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 3
    [see this SO answer](https://stackoverflow.com/a/147230/9878263) > "GCJ is dead. It was officially removed from the GCC project in 2016." See comments on @RichardCritten's answer (> [documentation](https://gcc.gnu.org/onlinedocs/gcc-6.3.0/gcj)) (> [statement about deletion](https://tromey.com/blog/?p=911)). – Mathieu CAROFF Feb 09 '19 at 15:36
7

As of 2020-05 the current options include:

  • javapackager (in openFX, add slimmed down JRE to executable)
  • launch4J (35kb external dependency, option to check for and download or include JRE)
  • GraalVM native image for real slim executables (but a bit more configuration for use of reflection)
Alim Özdemir
  • 2,396
  • 1
  • 24
  • 35
4

"GCJ is a portable, optimizing, ahead-of-time compiler for the Java Programming Language. It can compile Java source code to Java bytecode (class files) or directly to native machine code, and Java bytecode to native machine code."

see: https://gcc.gnu.org/java/

Richard Critten
  • 2,138
  • 3
  • 13
  • 16
2

I tried Excelsior JET recently and it worked really well for compiling JARs into native Windows binaries. The JetpackII utility allows you to deploy your application on a target machine without needing a Java installation. It may be a bit complicated to get used to but overall a brilliant choice.

spongebob
  • 8,370
  • 15
  • 50
  • 83
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
2
  1. Create a runnable jar from the source code.
  2. To make an executable file, we will need a separate tool for it called Launch4j. The download link for this software is down below.

http://launch4j.sourceforge.net/

  1. This tool adds a JRE(Minimum supported version) to the jar and creates an exe for windows machines.
Shiv
  • 93
  • 5