This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes creating jar file programatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating jar file programatically" Watch "creating jar file programatically" New topic
Author

creating jar file programatically

srinivas krishnan
Greenhorn

Joined: Mar 03, 2004
Posts: 13
Hi folks,I am trying to create a jar file programatically using the api..I am wondering if some one can help me change the code below to..
1)Include all the files along with the folders and sub-folders found in the directorypath in the jar file.(Currently does only the files found in the directorypath)
2)Write the jarFile created to a specific path.
3)Method should return the jarFile created.

/**
Creates a Jar file from a given directoryPath.
@param jarName is the name of the jar to Create.
@param jarPath is the path where jar has to be created.
@param directoryPath is the path where the files to be jarred are located.
Returns the jar File created.
*/
public boolean createJar(String jarName, String jarPath, String subDirectoryPath)
throws IOException
{
//String jarName = jarName;
boolean status = false;
JarFile finalJarFile = null;
JarOutputStream jar = new JarOutputStream(new FileOutputStream(jarName),new Manifest());
System.out.println(jarName + " created.");
try {
// Allocate a buffer for reading the input files.
byte[] buffer = new byte[1024];
int bytesRead;

File folder = new File(subDirectoryPath);
String fileList[] = folder.list();
// Loop through the file list.
for (int i = 1; i < fileList.length; i ++)
{
// Get the file name.
String fileName = fileList[i];
System.out.println("filename is "+fileName);
try
{
// Open the file.
FileInputStream file = new FileInputStream(subDirectoryPath + fileName);
try
{
// Create a jar entry and add it to the jar.
JarEntry entry = new JarEntry(fileName);
jar.putNextEntry(entry);
// Read the file and write it to the jar.
while ((bytesRead = file.read(buffer)) != -1)
{
jar.write(buffer, 0, bytesRead);
}
System.out.println(entry.getName() + " added.");
}
catch (Exception ex)
{
System.out.println(ex);
}
finally
{
file.close();
}
}
catch (IOException ex)
{
System.out.println(ex);
}
}
}
finally {
jar.close();
status = true;
System.out.println(jarName + " closed.");
}
return status;
}
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
What's wrong with your code as-is?
It looks like it does all the things you need:
1) Directories are created on the filesystem as the jar fiel is unjarred somewhere. Creating entries "something/something/filebytes" will create the directory something/something when it is unjarred.
2) The jar file is automatically created through the FileOutputStream,
3) Unless you actually wanted to return the jar file as an array of bytes, it's unnecessary - and the user already knows the file path of the jar since it is a parameter to the function.
It doesn't really look like there's much to do here. Unless I'm wrote .. which has been known to happen.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
srinivas krishnan
Greenhorn

Joined: Mar 03, 2004
Posts: 13
No actually it doesnt, in some respects..
say i have c:/jartest folder inside which i have
a.txt
b.txt
jartest1(folder) --->has some more files..
1)If I give the path c:/jartest/ as the source for the directorypath I want all the files and folders in that path to be included. cURRENTLY It only does the files and skips the directories in jartest.I know that I have to make a check for isDirectory() and iterate or something like that..but couldn't get it right..so ideally if i open the jar i should have a.txt, b.txt and the folder jartest1 with the files in it.

2)The jar file is automatically created through the FileOutputStream,
but I want to create it at user specified argument jarpath..
jar.write(buffer, 0, bytesRead); How do I change this??

3)I want to return the user a reference to the newly created jar file?
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
1) I don't think you can jar "directories" since there's no substance to directories, as far as jar is concerned. So, if you have an empty directory, there is no way to "jar" it -- only files (which may be located in directories).
Whether a file is unjarred into a directory is dictated by the name of the entry, eg. "/something/file".
2) I'm not sure what you mean by this, whether you want to have the user be able to specify some alteration to the jar entries, or merely the location of the jarred file. I assume you mean the location of the jarred file, in which case you just need to give the user some means of specifying the value that will eventually get sent as the jarpath argument to your function.
3) To do this, you will need to replace the boolean return type for the function to that of the JarFile and return this after it is successfully completed.
Does this make more sense now? There are still a few points I'm not sure about as far as what you mean by your questions, but just keep asking away and we'll get to the bottom of it.
srinivas krishnan
Greenhorn

Joined: Mar 03, 2004
Posts: 13
Thanks for being patient so far :-)
1)Basically, I would like to give a rootPath and jar all the directories and subdirectories(which have files in them)to be included int the jar.When I extract the jar I would like to see the subdirectories too in the jar and not just the files.
2)I would like the user to give the option to specify where he wants the generated jar file to be saved...So how should I use this jarPath to save the jar..? I couldn't find anything in the api..instead of this jar.write(buffer, 0, bytesRead) I want to use the jarPath to save the jar file.
3)I know I have to change the boolean to JarFile but how would I get a reference to the JarFile. jar is object of JarOutputStream in the code.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

When you create your JarEntry you can set the folder(s) in front of the filename like:

You can browse the contents of your folder you want to jar with the File method listFiles() witch return a File array of the files in the curent folder - you then need to iterate down the folders when isDirectory() is true and the file/folder name do you get with getName() on the File object.
Ren´┐Ż
[ March 09, 2004: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating jar file programatically