Creating EAR, JAR, and WAR file using java.util.jar.*
Posted on 2005-05-16
I want to ask something about java.util.jar.* package in java. I have created a class in java to create Jar/War/Ear file in java. This resulting jar/war/ear file will contain whatever files under the directory specified in ArrayList dirnameList.
I run the class file to create the JAR file and after that creating EAR file that contain this new JAR file. Afterwards, I try to deploy this EAR file into Websphere App Server, and I always encounter error that said that the EAR file is not valid.
But I can't seem to find out what's the problem with the EAR file generated and also what's the problem in the code. Everything looks okie to me.
My questions is:
1. Can this package (java.util.jar.*) be used to create Jar, Ear, and War file? Or do I need to add something else to create Ear file using this package?
2. I tried to use usual command: "jar -xvf xxx.ear" to extract this EAR file and then "jar -cvf xxx.ear * " to create the ear file, and then try to deploy this file to Websphere. I can deploy without getting the above error, but when I tried to start the server, I always encountered failure in deploying EJB, even though I have done no changes to the WAR file.
Below is my code for your reference:
public static void createJarFile (ArrayList dirnameList, String outfilename)
System.out.println("Creating " + outfilename + "...");
FileOutputStream os = new FileOutputStream(outfilename);
JarOutputStream jos = new JarOutputStream(os);
Iterator idirList = dirnameList.iterator();
String dirname = (String) idirList.next();
//check whether valid directory
File dirnameCheck = new File (dirname);
System.err.println(dirnameCheck + " is not a valid directory.");
byte buf = new byte;
//get the list of files under the dirname
ArrayList filesList = new ArrayList();
FileProj.readContents (dirname, filesList, true, false);
Iterator ifilesList = filesList.iterator();
String filename = (String) ifilesList.next();
is = new FileInputStream(filename);
filename = FileProj.getRelativePath(dirname + File.separator, filename);
//System.out.println("files included: " + filename);
retval = is.read(buf, 0, 4096);
if (retval != -1)
jos.write(buf, 0, retval);
}while (retval != -1);
System.out.println("Exception occurred during createJarFile: " + e);
Some notes: this is the flow in my code:
- get the latest ear file
- extract the latest ear file (resulting in jar file)
- extract the resulting jar file
- replace the updated files in the extracted jar files
- create jar file
- create ear file
Thanks a lot :)