?
Solved

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

Posted on 2005-05-16
12
Medium Priority
?
2,210 Views
Last Modified: 2011-10-03
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 :)
0
Comment
Question by:pipinana2002
  • 5
  • 4
  • 3
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 280 total points
ID: 14008814
>> have created a class in java to create Jar/War/Ear file in java.

Why? Why not use jar or Ant?
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 120 total points
ID: 14008898
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
 

Author Comment

by:pipinana2002
ID: 14010337
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 14010385
1. You can do most anything with Ant ;-)
2. Jar, War  and Ear are core tasks in Ant
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14010401
Unjar and Unwar are also core tasks
0
 

Author Comment

by:pipinana2002
ID: 14015535
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 14015570
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 14015659
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017002
>>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
 

Author Comment

by:pipinana2002
ID: 14024197
Hi all,

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

Thanks! :)
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 14024211
Hi,

Glad i could help.

Regards
Dave
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14025211
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question