Creating JAR Files with Eclipse

     

This process is somewhat simpler using the Eclipse IDE. Any reasonable IDE should give you the ability to create a JAR for your application; we use Eclipse here because it is free and pretty reliable and we've been using it a lot in this book.

  1. With an open project, click File > Export .

  2. Select the JAR file and click Next.

  3. Click on the application in the Select the Resources to Export window, because you may not want to include everything. Choose the classes directory (and the source code if you want to distribute it).

  4. In the Select the Export Destination text field, choose where you want your JAR to end up, and give it a name .

  5. Click Next, and Next again.

  6. Click the Use Existing Manifest from Workspace radio button and browse to the location of your manifest. Click Finish and you're done.

Executing the JAR file

graphics/fridge_icon.jpg

FRIDGE

If you have problems running this command, as many people do, try the following hack, which seems to do the trick sometimes: Put the line with the Main-Class entry immediately following the Manifest-Version: 1.0 line (if you create the complete manifest manually). Also, make sure there is a carriage return after your Main-Class entry, or it won't work properly. Sheesh.


To execute your JAR file, type the following command at the console:

 

 java -jar MyApp.jar 

If you need to pass arguments into your application, you can pass them in just as you normally do with the java command:

 

 java jar MyApp.jar myFirstArg mySecondArg 

This passes your executable JAR file to the java program for execution.

Alternatively, in Windows , you can double-click the JAR file.

If you get an error message saying, "Could not find the main class," this is one time you shouldn't believe what you read. There obviously is a problem; it just might not be the problem you're told it is. It could mean that there is a dependency that your application has that is unfulfilled. For example, if your application sends an e-mail using the javax.mail package, you need to put the J2EE library in your application folder and add an entry for it in the manifest. Specify that you need the j2ee.jar file on your classpath this way:

 

 Class-Path: j2ee.jar 

Then, make that file accessible to your application.

Note too that JARs can store other JARs. This is commonly done to deploy a complete application. Say you have an executable JAR, and then some other files that you want to distribute with that executable JAR (like a README.txt, a properties file, etc.). That you can do, as long as you are okay with your users un-JARing that distribution JAR in order to get to your executable JAR.

But say you have a JAR file that is a library of classes that do some particular thing, such as make PDF files. You've downloaded this JAR, and your application code relies on it. You need to un-jar (extract) that JAR so that the classes in it are all flat with your application classes. Then you can re-JAR the whole thing all together. Otherwise, your executable code won't know how to find the classes it needs.

Making your Java Program Execute Automagically on Startup

So there I was on the corner of Central and Jefferson, just holding up a lamppost, if you got to know, when up comes this little sassafras grousin' for a light and just like that he says a me, "I got a Java program, see. And it needs to start when my OS starts, automatically." Here's how to do it in both Windows and Linux.

In Windows

Write a batch file that executes your program using the java command, and create a task that executes the batch file. Easy!

  1. Navigate Windows Explorer to C:\WINDOWS\Tasks . Click Add Scheduled Task.

  2. Click Next and browse to the batch file and click on it to select it.

  3. Click the button that indicates when you want to run the program, type your password, and click Finish.

    Here is an example so you can get the feel for it.

    First, I create a file called MyStartupMessage.bat. It contains the following command:

     

     rem eben was here java net.javagarage.packaging.MyStartupMessage 

This batch file is in a folder called C:\apps . Also in that folder is the folder net, which contains the folder javagarage, which contains the folder packaging, which contains the Java program MyStartupMessage.class. That class contains the following important method:

 

 public void helpSelfEsteem(){     JOptionPane.showMessageDialog(null,         System.getProperty("user.name").toUpperCase() +         " BABY, YOU ROCK!!!");}       //run the program public static void main(String[] args) {       helpSelfEsteem(); } 

Just create the Windows task as indicated in the preceding , and you're good to go. Figure 34.1 shows the output I get.

Figure 34.1. The application executing automatically when the system starts up.
graphics/34fig01.jpg

In Linux RedHat 7.3 and Later

The steps are similar if your target OS is Linux. If you've got a Linux box with a graphical user interface like KDE, you can just navigate to System Tools > Task Scheduler and add the task.

But say we have installed Linux as a server and don't have any graphical user interface libraries installed (in which case we can't run the preceding message dialog program). The corresponding function is called cron. Here's how to do it. Let's make a lame-brained Java program called MyStartupMessage that writes out a file telling you what time the server started.

It looks like this:

 

 //do whatever work you want to perform after startup private static void doWork(){     //on my windows system this is     //C:\Documents and Settings\eben     String fileName = System.getProperty("user.home");     //on windows this is \, on unix it is /     String slash =                 System.getProperty("file.separator");     fileName += (slash + "startupMessage.txt");     File file = new File(fileName);     try {         BufferedWriter out = new BufferedWriter(new FileWriter(file));         out.write("The server restarted at " +                                       new Date());         out.close();     } catch (IOException e) {     System.err.println(e.getMessage()); } 

Now check your user home folder; the file is in there with the timestamp.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net