tags:

views:

2012

answers:

5

Hi,

I am trying to create a jar file which includes some class and java files needed, but I also would like to include some extra xml, xsl, html, txt (README) files.

I am using Eclipse on Windows XP.

Is there an easy way for me to set up a directory structure and package all my files into a jar?

A: 

A .jar is nothing but a ZIP archive, so you can use any program capable of creating ZIPs. Just make sure that you include the manifest and all the class files.

Roman Plášil
Are you suggesting I can use WinRar to create a zip file and then rename to .jar?
+1  A: 

Add the files to a source folder and they can be included in the jar.

One common way is to have, at the root of your project, a src folder. Within that, folders for java files, and others. something like:

src/
    css/
    java/
    html/
    images/

Then you can make each of those subfolders a source folder (Right click, Use as Source Folder) and they should be available to add to the jar.

Chris Marasti-Georg
+1  A: 

If you move to an ANT (or Maven, for you Maven fans) then you can automate the Jar building very nicely, and also use it outside of Eclipse (e.g., in an automated build environment). All you need to do is copy the files from your src, jsp, foobar and resources locations into a build staging folder, then Jar the resulting files using ANT's Jar task.

<target name="makejar" depends="compile, copyfiles">

 <jar destfile="${jars.dir}/myjarfile.jar" index="true" basedir="${build.dir}" />

</target>

One thing I look down on is including non-source (except package.html files for Javadoc) within the src folder. If you feel you have to do this to achieve something, then you are doing it wrong.

JeeBee
A: 

If you're using ant, you can use the jar task (see the examples section for how to include/exclude certain files, etc.):

http://ant.apache.org/manual/CoreTasks/jar.html

CapnNefarious
A: 

I just added all the files into my Eclipse project (including the txt, html, xml, etc files).

Then I used Eclipse to File->Export->Jar File->Next Check the "Export Java source files and resources" box.

Done.