Click here to Skip to main content
Click here to Skip to main content

EXE Launcher for Java Desktop Application

, 11 Oct 2010
Rate this:
Please Sign up or sign in to vote.
A useful launcher for JAR executable files, with Splash Image function

Introduction

Many users asked how to make an EXE file from a Java SE project. First of all, we must say that creating an EXE build file from a JAVA source is possible, but it is out of purpose and it is a very bad idea!

As may be evident, although JAR is an executable file, EXE file for more users (end-users) can be more comfortable. So the idea is to create an EXE reference file that can run “java -jar” command, or “javaw -jar” command for Windows usage. More articles on the web explain how to create a BAT file with the same scope, furthermore other tools can easily convert BAT -> EXE. Now, why don't you create a simple EXE automated file that can perform these tasks?

This is a simple work, reusable for small and medium projects, a work that wants to be a starting point for creating personal EXE application launcher for your custom desktop Solutions.

Prerequisites

  • Java Runtime Bin Directory must be in your system class path.

Background

In the background, I explain the use of certain methods, like strtok() a tokenizer function:

char* token = strtok(result, "\\.");
    while (next != "exe") {
        name = next;
        token = strtok(NULL, "\\.");
        next.assign(token);
    }

This is a piece of source code. The char* token variable is where the strtok() stores pieces of main string.

strtok(result, "\\."); 

This gets the first piece of result string dividing by two characters "\" and "." ; So why "\\." ? Because "\" causes formatting error. Why these two characters are explained in the next section, however result string is the simple path of EXE application, so it iterates each piece up to the last "AppName.exe", it discards "EXE" and keeps only a simple name of app.

The strtok() function has a particular use as you can see. The next iterations of that function will be:

strtok(NULL, "\\."); 

until the end of string.

Using the Code

Anyway a picture is better than a thousand words (also if this picture is a piece of code). Smile | :)

#include <iostream>

#include <fstream>

#include <windows.h>


using namespace std;

string getAppName() {

    // Creates char array with maximum needed length
    char result[MAX_PATH];
    // Uses <windows.h> method to retrieve app name. Only Windows OS
    std::string(result, GetModuleFileName(NULL, result, MAX_PATH));

    char* token;
    string name, next = "";

    //Iterates until end of tokens of Name string, divided by char -> '\' and '.'
    //The only '.' in Filename must be that for ".exe" , error otherwise.
    token = strtok(result, "\\.");
    while (next != "exe") {
        name = next;
        token = strtok(NULL, "\\.");
        next.assign(token);
    }
    return name;
}

int main(int argc, char** argv) {

    ifstream file; // file stream
    string value = "";
    string jar_name = "";
    string image_name = "splash.gif";
    file.open("run.ini");
    if (!file) { // file does not exist, so use standard name
        jar_name = getAppName();
        jar_name += ".jar";
    } else {
        do {
            file >> jar_name;
            file >> image_name;
        } while (!file.eof());
        file.close();
    }

    //Launch JAVAW command on "jar_name" file with "image_name" splash image
    string exec_command = "start javaw -splash:" + image_name + " -jar " + jar_name;
    system(exec_command.c_str());

    return 0;
}

The application works in two way. First, it seeks for a file named “run.ini” in the same folder, when found, it retrieves from file two string values (jar_name, image_name) delimited by RETURN character (or rather each string per line). Its job is to build a simple exec String like this:

  • javaw -splas:image_name -jar jar_name

and it invokes system(command) to execute it like you are in command window.

The second way of application, if “run.ini” does not exist in folder, it retrieves (jar_name, image_name) by default parameters, that is jar_name = ApplicationName.jar ( where ApplicationName is the name of EXE file ) and image_name = “splash.gif”. As mentioned earlier, it is a starter point for a custom application launcher capable of reading a custom configuration file.

For the uninitiated, the splash image is the Loading Image that appears when application is loading. From JAVA 6, the “java” command is able to load directly an image for splash screen. So valuable uses for this little application is just to load a splash screen for our application.

Considerations

  • In the source code, we have the execution command preceded by "start" key. This is one way to avoid console window show.
  • EXE implies that we are on Windows Platform (strong implication) so this code works only on Windows. Why? Because:
    std::string(result, GetModuleFileName(NULL, result, MAX_PATH));

    is the way to retrieve app name in Windows. It may not work on Linux for example.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

b4rc0ll0
Engineer n
Italy Italy
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralProcessname is still jawaw.exe PinmemberReto7018-Oct-10 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 11 Oct 2010
Article Copyright 2010 by b4rc0ll0
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid