Main BLOGGER
Google
WWW THIS BLOG
Monday, May 08, 2006
 
java code to list jar files in a directory

    private URL[] listJar(String dir)

    {

      URL[] urls = null;

      String suffix=".jar";

      int total=0;

      try{

        File f = new File(dir);

        if (!f.exists() || !f.isDirectory())

        {

           System.out.println ( dir+" does NOT exist" );

           return null;

        }

        String path = f.getPath();

        String[] files = f.list();

        for (int i =0; i < files.length; i++)

        {

            File f2 = new File(path, files[i]);

            if (f2.isFile())

            {

               if ((files[i].lastIndexOf(suffix))==(files[i].length()-suffix.length()))

               {

                  System.out.println(files[i]);

                  total++;

               }

            }

        }

        urls = new URL[total+1];

        String fileSeparator = System.getProperty("file.separator");

        int index=1;

        for (int i =0; i < files.length; i++)

        {

            File f2 = new File(path, files[i]);

            if (f2.isFile())

            {

               if ((files[i].lastIndexOf(suffix))==(files[i].length()-suffix.length()))

               {

                  File f3=new File(path+fileSeparator+files[i]);

                  urls[index]=f3.toURL();

                  index++;

               }

            }

        }

 

        System.out.println("total:"+(new Integer(total)).toString());      

        return urls;

      }catch(Exception e)

      {

        System.out.println ( "Exception:" + e.toString());

        return null;

      }

    }




<< Home

Powered by Blogger

Google
WWW THIS BLOG