Link to home
Start Free TrialLog in
Avatar of pipinana2002
pipinana2002

asked on

Creating EAR, JAR, and WAR file using java.util.jar.*

Hi all,

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)
{
    try
    {  
        System.out.println("Creating " + outfilename + "...");
        
         FileOutputStream os = new FileOutputStream(outfilename);
         JarOutputStream jos = new JarOutputStream(os);

         Iterator idirList = dirnameList.iterator();
              
         while (idirList.hasNext())
         {
             String dirname = (String) idirList.next();
                  
            //check whether valid directory
             File dirnameCheck = new File (dirname);
            if(!dirnameCheck.isDirectory())
            {
                System.err.println(dirnameCheck + " is not a valid directory.");
                  return;
            }
                    
            byte[] buf = new byte[4096];            
            int retval;                            
                  
            //get the list of files under the dirname
            ArrayList filesList = new ArrayList();
            FileProj.readContents (dirname, filesList, true, false);
           
            Iterator ifilesList = filesList.iterator();
            while(ifilesList.hasNext())
           {
      FileInputStream is;
      String filename = (String) ifilesList.next();                      
                                             
      is = new FileInputStream(filename);
      filename = FileProj.getRelativePath(dirname + File.separator, filename);

                //System.out.println("files included: " + filename);
                              
              jos.putNextEntry(new JarEntry(filename));

                  do
                  {                        
            retval = is.read(buf, 0, 4096);                
                      if (retval != -1)
                     {
                          jos.write(buf, 0, retval);
                     }
                }while (retval != -1);
                  
                  is.close();
                  jos.closeEntry();
            }  
         }
        jos.close();
       os.close();
     }
        catch(Exception e)
        {
            System.out.println("Exception occurred during createJarFile: " + e);
            System.exit(1);
        }
    }

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 :)
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pipinana2002
pipinana2002

ASKER

Hi CEHJ and Dave,

Thanks for your reply :)

Oh okie.. so it would be easier to use jar or Ant is it?

1. Can we extract JAR, WAR, and EAR file using Ant?
2. Can we use jar command to process WAR file? From what I understand, processing WAR file need to use zip instead of jar. Maybe you know how to handle this?

Thanks again :)
1. You can do most anything with Ant ;-)
2. Jar, War  and Ear are core tasks in Ant
Unjar and Unwar are also core tasks
Hi,

Can you show me example of how to unjar and unwar using Ant?

Also, same question as above: Can we use jar command to process WAR file? From what I understand, processing WAR file need to use zip instead of jar. Maybe you know how to handle this?

Mm.. actually why can't we use java codes to do this? The java codes should work fine right? I mean since I have finished making the java codes, just curious why the code is not working fine.. Can't seem to spot the mistake..

Thanks :)
Hi,

>>"Mm.. actually why can't we use java codes to do this?"
It is something like "why do you need to do something that is already done and easier" :)

I will try to find some samples regarding Ant

Regards
Dave
Hi,

You should go to
http://ant.apache.org/manual/

and choose the Ant Task > Core Task in the left panel.

I hope that helps.

Regards
Dave
>>Can you show me example of how to unjar and unwar using Ant?

It's essentially the same task:

http://ant.apache.org/manual/CoreTasks/unzip.html
Hi all,

Thanks for all your comment and helps. Now i can do what I want using Ant.

Thanks! :)
Hi,

Glad i could help.

Regards
Dave
:-)