views:

1930

answers:

7

I'm writing a java app using eclipse which references a few external jars and requires some config files to be user accessable.

  1. What is the best way to package it up for deployment?

  2. My understanding is that you cant put Jars inside another jar file, is this correct?

  3. Can I keep my config files out of the jars and still reference them in the code? Or should the path to the config file be a command line argument?

  4. Are there any third party plugins for eclipse to help make this easier? I'm using an ant build file at the moment but I'm not sure I know what I'm doing.

  5. Is there an equivelent of the deployment projects in Visual studio, that will figure out everything you need and just make an installer? I've used install4j before, and it was powerful if no where near as automated as .Net deployment projects.

Cheers.

A: 

Ant. It's not the best thing in the world, but it's standard, it's apache, and it works. There's some good examples on the web how to make a simple build.xml for any, and it's got some features like the 'war' task that knows how to put all the basic stuff (classes, web.xml etc) in the jar file for you. You can also tell it to pick up other config files and jars and it will happily do it. It's also really smart about what to compile. You give it a directory, and it finds all the java files and builds them only if their classfile is out of date, so you get some of the traditional make functionality for free without much effort.

stu
If you are going to vote the guy down at least comment to tell me why this is bad advice?
Omar Kooheji
+4  A: 

There is no one 'best way'. It depends on whether you are deploying a swing application, webstart, applet, library or web application. Each is different.

On point 2, you are correct. Jar files cannot contain other jar files. (Well, technically they can, its just that the inner jar file won't be on your classpath, effectively meaning that jar files do not contain jar files)

ON point 3, you certainly can reference config files outside the jar file. You can typically reference a config file as a File or a resource. If you use the resource approach, it typically comes from the classpath (can be in a jar). If you use a File, then you specify the filename (not in a jar).

In general, most Java developers would use Apache Ant to achieve deployment. Its well documented, so take a look.

JodaStephen
On Point 5, Maven (though requires some configuration, but not a lot), manages your dependencies.
Spencer K
+5  A: 

(1) An alternative to ant that you may wish to consider is maven.

A brief intro to maven can be found here.

For building a JAR, maven has the jar plugin, which can automate the process of ensuring all dependent jars are listed in your jar's manifest.

If you're using eclipse, then download the maven integration as well.

(2) Another alternative is to use OneJar (disclaimer: haven't tried this myself).

toolkit
Given that you are running a standalone app from the sounds of it OneJar would be my definate recommendation. One of the advantages is that it will allow you to run the app in Windows by double-clicking on the jar.
Michael Rutherfurd
A: 

You could look at other java projects (e.g. JMeter, SquirrelSQL, JEdit, Cernunnos, etc.). Each package their applications slightly differently, so consider your goals when you review these.

Andy Gherna
+3  A: 

The answers vary depending on what kind of thing you're building.

If you're building a library, it's best to distribute your work as a jar file. It's possible to refer to your jar dependencies via the Class-path attribute in your jar manifest, although I generally think that's uncool. That attribute was designed for applets and it's used infrequently enough in libs that when this technique pulls stuff into the classpath (particularly common stuff the user might already be using), you can get unexpected version conflicts. And it's hard to track down why you're seeing them.

Publishing a jar to a Maven repo with pom info to track dependencies is an excellent choice for libraries as well. If you do that, please publish your Maven coordinates in your docs!

If you're building an app, the two popular choices are to distribute a zip/tar/whatever of a deployment structure OR to use an installer program. If the program is a server-ish kind of thing, the former is far more common. The latter is more common for clients. Generally, the installer program is just going to lay out the deployment structure and maybe do some extra tasks like installing in OS-specific locations.

To build your deployment structure (aka "kit") you'll want to create a repeatable process in whatever build system you're using. Ant has copious examples of this and Maven has the assembly plugins that can help. Generally you'll want to include a jar of your code, any dependencies, scripts to start the program, maybe a JRE, and any other resources you might need.

If you want to create an installer, there are many options both free and commercial. Some folks I know have recently had good experiences with the free IzPack but check out your options.

Alex Miller
A: 

Well, if you are speaking of deployment of a standalone desktop application:

Before we switched to web start we have been creating three deployment archives, one for windows, one for mac and one for other platforms.

On windows we have successfully used the Nullsoft Scriptable Install System (known for it's usage by the older winamp versions) and its ant task, although some drawbacks are:

  • It is only usable on windows AFAIR
  • You have to do some work by hand, i.e. customizing the wizard-created script AFAIR

It can create a windows installation with start menu entries on the other hand. There also exists an eclipse plugin for integrated NSIS shell script editing.

On Mac OS X there is an ant task to create an .app file from your java files so that you can start it like a native os x application. But beware of not writing any setting to your home dir and using the the application dir instead.

For others you have to expect they are in a un*x env and deploy your app with a shell script to start the application.

In any case you may have to deploy your custom policy file to get access rights for your application.

If you want to get rid of all the packaging and stuff you sould seriously consider using web start. We have saved much time since switching to it, i.e. simplified our deployment process, takes care of updates etc.

dhiller
+2  A: 

You should try FatJar. It's an Eclipse plugin that with just a right click at the Project can build a JAR file with all you need to run the application, including the necesary third party JAR.

We use it everyday, in conjuction with JSmooth to create the executables, to deploy our software packages to our customers, and works like a charm.

Saiyine