0

I have created jar file in java. It uses some library files. so, the final structure I have is,

  • example.jar
  • lib
    • library files

How to convert them into a single file, like an exe for windows? It should run in linux also, like (.sh files - NetBeans installer).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • I doubt you can write an executable that runs under Linux and Windows. Why not creating a jar file containing your codes and the libraries? – MrSmith42 Jan 12 '13 at 21:41
  • *"No negative votes for this please."* Do some research in future, please. 1) Many of the 'Java to EXE' questions mention [Java Web Start](http://stackoverflow.com/tags/java-web-start/info) as a better alternative for any app. with a GUI. Did you read any of those, does the app. have a GUI? Do you have reasons for not wanting to use the Oracle technology that makes this possible for Windows, *nix & OS X? 2) You don't avoid down-votes by politely requesting none, instead you avoid them by asking a good question! – Andrew Thompson Jan 13 '13 at 05:04

4 Answers4

3

Just make a JAR packed with all the library dependencies.

  1. If Maven is your build tool, check this SO question. There are similar solutions for other tools (Gradle etc).

  2. Some IDEs are also capable of this, for instance Netbeans and IntelliJ IDEA are.

Of course be sure, that the libraries you want to pack have a license that permits this.

Community
  • 1
  • 1
emesx
  • 12,555
  • 10
  • 58
  • 91
2

If you use Netbeans check this for join all jars in one, now when you have 1 jar you can create the exe with launch4j.

Jefferson
  • 794
  • 10
  • 24
1

Although this is possible it is against Java nature.

Java was designed as a portable language (write once, run anywhere); your .jar file contains Java bytecode, which is to be interpreted by the Java Virtual Machine at runtime; you can copy it to any architecture and it will run there.

An .exe file actually contains machine code specific language which is to be directly ran by the processors; this will not work on another architecture.

However there are different tools for your need like JarToExe or JSmooth.

Random42
  • 8,989
  • 6
  • 55
  • 86
  • The architecture isn't really the issue here, virtually any processor used for Desktop/Laptop PC's will understand x86 machine code. – Cubic Jan 12 '13 at 21:47
1

You have a few options here:

  • Compile to a native application with something like gcj. This isn't an option in many cases though (you'll see why if you look into it)

  • Have a modified java executable that runs your jar

  • Have an executable that runs your jar with java

If you can reasonably assume your clients have java installed, it should be fine to just deploy an executable jar though - on most Desktop OS'es the java distribution is set up to run executable jars in the expected fashion (i.e. by double clicking in Windows), and on those where it's not - well, people will likely know what java is and how to run jars.

Cubic
  • 14,902
  • 5
  • 47
  • 92