Browse DevX
Sign up for e-mail newsletters from DevX


Build "Win-tuitive" Java Applications : Page 2

You can make your cross-platform Java application more intuitive for Windows users. Learn how enabling access to the tray bar and programming your application to act as a Windows Service provides this functionality.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Exercise 1: Develop a Simple Java Application with Its Own .exe
As an example for using J2Exe, take the following steps to write a simple application called MyServer that shows a mainframe with text.
  1. Create a directory called "c:\J2ExeDemo".
  2. Either open the downloaded example or just copy and paste the following code (MyServer application) for the Java file:

    package server; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyServer { public static void main(String[] args) { JFrame frame = new JFrame(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); String message = "Ready!"; if (args.length > 0) message = args[0]; JLabel label = new JLabel(message, SwingConstants.CENTER); frame.getContentPane().add(label, BorderLayout.CENTER); Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frm = new Dimension(200, 100); int x = (scr.width - frm.width )/2; int y = (scr.height - frm.height)/2; frame.setSize(frm); frame.setLocation(x, y); frame.setTitle("MyServer"); frame.setVisible(true); } }

    This is a simple application that shows a mainframe with text. It takes the text from the first program parameter. If such a parameter doesn't exist, the default text is "Ready!".
  3. Save the above Java file as "c:\J2ExeDemo\lib\server\MyServer.java".
  4. Open the command prompt in this directory and compile the file by typing: "javac MyServer.java".

Now your application is ready. You can write the starting script, "MyServer.bat":

cd ..\bin java -classpath . server.MyServer cd ..\lib

Save the .bat file as "c:\J2ExeDemo\bin\MyServer.bat". You can click on it to see whether it works properly. The directory structure of the MyServer project should look like Figure 1.

Figure 1: Directory Structure of the MyServer Project
Now you are ready to start using J2Exe. Figure 2 shows the default "Input" options.

Figure 2: MyServer Project Input Options
First, pay attention to your startup script, which has four main components:
  • The Virtual Machine arguments (e.g., classpath, etc.)
  • The main class name (server.MyServer)
  • The program parameters (parameter1, parameter2)
  • The working directory (c:\J2ExeDemo\lib)

You should enter the same information in the "Input" dialog's text fields. The "Java Parameters" are Virtual Machine arguments, and the "Application parameters" are actually program parameters. The rest of the fields have the same designation as the startup script.

Pay particular attention to the working directory. Very often it is set with a relative path because users install their applications in different places. With this in mind, use the reserved word "%APP_HOME%" instead of an absolute path. "%APP_HOME%" represents the location from where the generated .exe file launches. In this case, "%APP_HOME%" is the same as "c:\J2ExeDemo\bin". In the starting script, the working directory is "c:\J2ExeDemo\lib". Therefore, you can specify "%APP_HOME%/.../lib" as a working directory, which is the same as "c:\J2ExeDemo\lib".

You are almost ready to produce your .exe file. Go to "Output" options (see Figure 3), and choose one of the three possible .exe file types: "JDK with Console", "JDK with Managed Console", or "JDK without Console".

Figure 3: MyServer Project Output Options
"JDK with Console" is like starting with "java", and "JDK without Console" is like working with "javaw". The other file type, "JDK with Managed Console" (invented by Yours Truly), is also like starting with "javaw" but with a big difference—it enables you to show or hide the application console whenever you want to (see Figures 4 and 5).

Figure 4: Show Java Console

Figure 5: Hide Java Console
I recommend using "JDK with Managed Console" for all kinds of Windows applications. The console is invisible to the user, but the developer can show it to receive a full thread dump or to check for available exceptions. Of course, you can redirect the available exceptions to a log file that the user can send you, but you cannot redirect full thread dump to this log file.

Dress It Up with an Icon
If you want to specify an icon for the .exe file, click on the 'café' icon. It will show the "Open file" dialog from where you can select your desired icon. The last action is to specify an output location in the "File:" text field. Enter "c:\J2ExeDemo\bin\MyServer.exe" and click the "Generate" button. You'll find the .exe file in that directory. Start it!

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date