A simple way to turn a Java program into an executable file

Source: Internet
Author: User
Tags config ini

Running a Java program (a swing or SWT desktop program) can directly execute a. class file or compress all. class files and related other files into a. jar file, and then use the Javaw-jar My.jar to run the program (where My.jar is a jar file name, which can be any of the specified jar files). But under Windows, the user is accustomed to running an EXE directly to execute the program. There are many programs on the web that can convert My.jar or. class files directly into EXE files. But a lot of it is not convenient to put the jar or. class file into an EXE. Some tools, such as Javalauncher, can use an EXE program (written in c) to invoke the jar file. Sometimes, however, there are other tools that need to be done between running programs, such as downloading the latest version of a program from the server dynamically from the client. Therefore, in this article will use delphi2009 to implement a simple can call the jar file program, readers can be any extension of this program, this article also provides the program source code and EXE files.

Use Delphi to build a VCL project. You do not need a form in this program, so you can delete the default generated form. In fact, we only need to invoke the ShellExecute method to execute the jar file through the JAVAW command, as follows:

shellexecute(GetActiveWindow, 'open','javaw', '-jar my.jar', '', SW_NORMAL);

Where the GetActiveWindow method gets the handle of the current window. In this program, a config.ini file is used to specify the jar file to execute, and the complete code for implementation is as follows:

program launcher;
uses
  Forms,
  windows,shellapi, inifiles, sysUtils;

{$R *.res}
var
  ini: tinifile;
  JarFile: String;
begin
  Application.Initialize;
  ini := tinifile.Create(extractfilepath(application.ExeName) +  'config.ini');
  JarFile := ini.ReadString('config', 'JarFile', '');
  shellexecute(GetActiveWindow, 'open','javaw', PWideChar('-jar ' + JarFile), '', SW_NORMAL);
  ini.Free;
  Application.Run;
end.

Create a config.ini file in the same directory as EXE, which reads as follows:

[config]
JarFile = jars\swt.jar

Where the Swt.jar file is the executing jar file, to be placed in the jars directory of the current directory. The current directory results are as follows:

launcher.exe
config.ini
jars\swt.jar

Where Launcher.exe and Config.ini are also included in the download file for this article. The reader only needs to modify the appropriate jar file name in the Config.ini file. You can also change the launcher.exe to another file name. There is to modify the Launcher.exe file icon, you can directly in the delphi2009 (lower version of the Delphi can also, preferably more than 7.0) in the change, you can also find some edit binary resources of the program, directly open Launcher.exe file, modify the icon.

Of course, this program can also use any programming language that can generate native executable files, such as C, C + +, etc.

OK, now you can call the jar package by double-clicking the Launcher.exe file. To extend the launcher, modify the source code directly. In fact, this implementation is very, very simple!!!

This article supporting source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.