4 Replies Latest reply on May 17, 2006 4:51 AM by pikumishra

    Problem in Creating a jar file using java.util.jar and deplo

    pikumishra

      Dear Techies,
      I am facing a very peculiar problem. I have a irectory named "bin" , this directory contains several compiled java classes with their packages , META-INF directory contains ejb.jar and jboss.xml file etc. I am creating a jar file from this bin directory programmatically using java.util.jar API(ie JarOutPutStream,JarEntry etc). The jar file is created, but when I am trying to deploy this jar file in Jboss , the server is unable to identify it or it can not undeploy it. Please help me in this regard. One more thing, when I am creating a jar file using jar command, the Jboss server is accepting, where it is not the case for jar file created programmatically. Both the jar file sizes are same . Please help me in this regard.


      PIKU MISHRA
      Bangalore

        • 1. Re: Problem in Creating a jar file using java.util.jar and d
          jaikiran

          I have never tried creating a jar programatically, but if you post the exception that you are seeing then may be i (or someone else here) will be able to help you.

          • 2. Re: Problem in Creating a jar file using java.util.jar and d
            pikumishra

            Dear Sir,

            I am really very happy to get a response from uour side. For your acknowledgement, I am not getting any eroor or exception in this regard. The jar file is created successfully but jboss is unable to deploy it.

            Piku

            • 3. Re: Problem in Creating a jar file using java.util.jar and d
              jaikiran

              What is this jar file? Is it part of some .war or .ear file? Where is this jar file placed? Also, execute the following command on the command prompt, on both the jars(the one which you created manually and the one created programatically) to see the contents of the same. You can even post the contents.

              Command:

              jar -tf yourJarName.jar




              • 4. Re: Problem in Creating a jar file using java.util.jar and d
                pikumishra

                Dear JaiKiran,
                I am sending my code that will create a jar file from the directory "bin". Please copy my java file and test how it is creating a jar file. Please modify the package name. This program will create a jar file. If you create a jar file using jar -cvf command, the contents wil be same as that of my created jar file. Please help me in this regard. I am unable to proceed. If you find any problem in the code, please modify it and let me know. I am eagerly waiting for your response.

                package com.rrs.corona.solutionsacceleratorstudio.solutionadapter;

                import java.io.File;
                import java.io.FileInputStream;
                import java.io.FileOutputStream;
                import java.util.jar.JarEntry;
                import java.util.jar.JarOutputStream;
                import java.util.jar.Manifest;
                import com.rrs.corona.solutionsacceleratorstudio.SASConstants;

                /**
                * @author Piku Mishra
                *
                */
                public class JarCreation
                {
                /**
                * File object
                */
                File file;
                /**
                * JarOutputStream object to create a jar file
                */
                JarOutputStream jarOutput ;
                /**
                * File of the generated jar file
                */
                String jarFileName = "rrs.jar";
                /*
                *To create a Manifest.mf file
                */
                Manifest manifest = null;
                //Attributes atr = null;

                /**
                * Default Constructor to specify the path and
                * name of the jar file
                * @param destnPath of type String denoting the path of the generated jar file
                */
                public JarCreation(String destnPath)
                {//This constructor initializes the destination path and file name of the jar file
                try
                {
                manifest = new Manifest();
                jarOutput = new JarOutputStream(new FileOutputStream(destnPath+"/"+jarFileName),manifest);
                }
                catch(Exception e)
                {
                e.printStackTrace();
                }
                }

                public JarCreation()
                {

                }

                /**
                * This method is used to obtain the list of files present in a
                * directory
                * @param path of type String specifying the path of directory containing the files
                * @return the list of files from a particular directory
                */
                public File[] getFiles(String path)
                {//This method is used to obtain the list of files in a directory
                try
                {
                file = new File(path);
                }
                catch(Exception e)
                {
                e.printStackTrace();
                }
                return file.listFiles();
                }
                /**
                * This method is used to create a jar file from a directory
                * @param path of type String specifying the directory to make jar
                */
                public void createJar(String path)
                {//This method is used to create a jar file from
                // a directory. If the directory contains several nested directory
                //it will work.
                try
                {
                byte[] buff = new byte[2048];
                File[] fileList = getFiles(path);

                for(int i=0;i<fileList.length;i++)
                {
                if(fileList.isDirectory())
                {
                createJar(fileList
                .getAbsolutePath());//Recusive method to get the files
                }
                else
                {
                FileInputStream fin = new FileInputStream(fileList);
                String temp = fileList
                .getAbsolutePath();
                String subTemp = temp.substring(temp.indexOf("bin")+4,temp.length());
                // System.out.println( subTemp+":"+fin.getChannel().size());
                jarOutput.putNextEntry(new JarEntry(subTemp));
                int len ;
                while((len=fin.read(buff))>0)
                {
                jarOutput.write(buff,0,len);
                }
                fin.close();
                }
                }
                }
                catch( Exception e )
                {
                e.printStackTrace();
                }
                }
                /**
                * Method used to close the object for JarOutputStream
                */
                public void close()
                {//This method is used to close the
                //JarOutputStream
                try
                {
                jarOutput.flush();
                jarOutput.close();
                }
                catch(Exception e)
                {
                e.printStackTrace();
                }
                }

                public static void main( String[] args )
                {
                JarCreation jarCreate = new JarCreation("destnation path where jar file will be created /");
                jarCreate.createJar("put your source directory ie full path of bin");
                jarCreate.close();
                }

                }