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). :)
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
string getAppName() {
char result[MAX_PATH];
std::string(result, GetModuleFileName(NULL, result, MAX_PATH));
char* token;
string name, next = "";
token = strtok(result, "\\.");
while (next != "exe") {
name = next;
token = strtok(NULL, "\\.");
next.assign(token);
}
return name;
}
int main(int argc, char** argv) {
ifstream file; string value = "";
string jar_name = "";
string image_name = "splash.gif";
file.open("run.ini");
if (!file) { jar_name = getAppName();
jar_name += ".jar";
} else {
do {
file >> jar_name;
file >> image_name;
} while (!file.eof());
file.close();
}
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.