JAR

Including external JARs in a JAR using Netbeans

The task of including external JARs when exporting a Java project into a JAR file should have been easy and straight forward. However, Java demands extra effort for doing this and Eclipse IDE does not provide it automatically (maybe there is a plugin for this, which I did not come along).

The solution is simple and it is a standard computer science solution: use another IDE... and the winner is NetBeans! In general, Netbeans (especially version 6.x) is in a way a smarter IDE than Eclipse. I like the interaction with the user and the ease of customization. The developers have made a good job on the HCI part, which is something great for a non commercial platform.

HOW TO INCLUDE JARs while exporting your Java classes in a JAR?

  • Just create a new Java Project with Netbeans.
  • Go to its Properties -> Sources -> Compile tab and add all the JARs that you want to be included.
  • Go to the Compiling options and enable Track Java Dependencies. This may be optional in some cases.
  • At the Run options you have to define a Main class for your program.
  • When you build your project there would be a JAR file in the dist folder which is ready to be used with no ClassNotFound error messages. Netbeans does that by default. Great! Be careful - do not move the file from this directory without the rest of the contents of the folder because the links with the external JAR files will be broken.
  • It is nice that you will not have to care about the Manifest file or to create any Ant scripts.
  •  

Executing a JAR file using Windows Task Scheduler

Do you want to implement a cron job for a JAR file? With high probability you will not even care about this, but since it is really something easy I would like to share this information with you!

You do not have to do something incredible, just use the Windows Task Scheduler. You will realise that Windows Task Scheduler is able to schedule the execution of (executable in Windows) files at any time and with many (interesting) preferences (it is a nice but somewhat hidden application of Windows).

Since a JAR file is not a Windows executable file, all you need to do is:

  • Open Windows Task Scheduler (in Vista it is located under system32 folder OR you may find it in Start >> Accessories >> System Tools.
  • Create a New Task.
  • After editing your scheduling parameters (Triggers), go to Actions and choose Start a Program Action.
  • Set script to
    C:\Program Files\Java\jdk1.6.0_02\bin\java.exe
    or wherever your java.exe file is located in.
  • Set add arguments option to something like
    -jar C:\Users\YourUsername\PathToJARFile\JARfile.jar
    .

  You are ready to go!

Syndicate content