Kashyap Kansara
Kashyap Kansara

Reputation: 161

Compiling java source code to native exe

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.

Upvotes: 12

Views: 11244

Answers (5)

Shiv
Shiv

Reputation: 93

  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.

Upvotes: 2

Alim Özdemir
Alim Özdemir

Reputation: 2624

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)

Upvotes: 7

BullyWiiPlaza
BullyWiiPlaza

Reputation: 19195

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.

Upvotes: 2

Richard Critten
Richard Critten

Reputation: 2145

"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/

Upvotes: 4

T.J. Crowder
T.J. Crowder

Reputation: 1074335

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.

Upvotes: 7

Related Questions