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 :)
pipinana2002Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>> have created a class in java to create Jar/War/Ear file in java.

Why? Why not use jar or Ant?
0
 
suprapto45Connect With a Mentor Commented:
Hi,

CEHJ is right, you should use either jar or Ant.

No Ant
--------
http://neptune.netcomp.monash.edu.au/JavaHelp/howto/jar.htm


With Ant
----------
http://java.sun.com/webservices/docs/1.0/tutorial/doc/GettingStarted5.html

Regards
Dave
0
 
pipinana2002Author Commented:
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 :)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
CEHJCommented:
1. You can do most anything with Ant ;-)
2. Jar, War  and Ear are core tasks in Ant
0
 
CEHJCommented:
Unjar and Unwar are also core tasks
0
 
pipinana2002Author Commented:
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 :)
0
 
suprapto45Commented:
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
0
 
suprapto45Commented:
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
0
 
CEHJCommented:
>>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
0
 
pipinana2002Author Commented:
Hi all,

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

Thanks! :)
0
 
suprapto45Commented:
Hi,

Glad i could help.

Regards
Dave
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.