Showing posts with label jarfind. Show all posts
Showing posts with label jarfind. Show all posts

Friday, December 30, 2016

Poor man's version of FindClasses

We already spoke here on how to find a class in an ocean of JAR files.

Here is a brutally simple utility to write all the JAR contents to a file:


import java.io.BufferedWriter;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class FindClasses {
 static boolean DEBUG = true;
 static String rootFolder = "C:\\Apps\\Pippo\\";
 static List<File> jarFiles = new ArrayList<File>();
 public static void main(String[] args) throws Throwable {
  Path outputFilePath = Paths.get("allfilesPippo.out");
  BufferedWriter writer = Files.newBufferedWriter(outputFilePath);
  File rootFolderDir = new File(rootFolder);
  scan(rootFolderDir);
  System.out.println("END SCAN");
  if (DEBUG) {
   for (File jar : jarFiles) {
    System.out.println(jar.getAbsolutePath());
   }
  }

  for (File jar : jarFiles) {
   writer.write("BEGIN JAR: " + jar.getAbsolutePath() + "\n");
   ZipFile zipFile = new ZipFile(jar);
   Enumeration<? extends ZipEntry> entries = zipFile.entries();
   while (entries.hasMoreElements()) {
    ZipEntry entry = entries.nextElement();
    writer.write(entry.getName() + "\n");
   }

   zipFile.close();
   writer.write("END JAR: " + jar.getAbsolutePath() + "\n\n");
  }
  writer.close();
 }
 private static void scan(File folderToScan) {
  if (DEBUG)
   System.out.println("scanning " + folderToScan.getAbsolutePath());
  for (File file : folderToScan.listFiles()) {
   if (file.isDirectory()) {
    scan(file);
   } else {
    if (file.getName().toLowerCase().endsWith(".jar")) {
     jarFiles.add(file);
    }
   }
  }
 }
} 

 



Wednesday, February 9, 2011

Jar Finder home grown

I republish here my beloved utility to dump the content of all ZIP and JAR files....

I change and use it often, so better have it as a post, rather than in SVN.




package com.pierre.jar;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class JarSearcher {
 private static final String BASEDIR = "C:\\ac,e\\repository\\central\\libs";
 //private static final String BASEDIR = "C:\\Temp";
 static FileWriter fstream ;
 static BufferedWriter out;

 public static void main(String[] args) throws Exception {
  fstream = new FileWriter("c:\\out.txt");
  out = new BufferedWriter(fstream);
  JarSearcher jarSearcher = new JarSearcher();
  jarSearcher.findTest(BASEDIR, "");
  out.flush();
  out.close();
 }

 private void findTest(String dirString, String text) throws ZipException, IOException {
  File dir = new File(dirString);
  for (File f : dir.listFiles()) {
   if (f.isDirectory()) {
    findTest(f.getAbsolutePath(), text);
   }
   else {
    String name = f.getName();
    if (name.endsWith(".jar") || name.endsWith(".zip")) {
     System.out.println("SEARCHING " + f.getAbsolutePath());
     out.write("===" + f.getAbsolutePath() + "===\n");
     try {
      ZipFile zipFile = new ZipFile(f);
      Enumeration e =  zipFile.entries();
      while (e.hasMoreElements()) {
       ZipEntry nextElement = e.nextElement();
       InputStream is = zipFile.getInputStream(nextElement);
       if (text.length() == 0 || findInInputStream(is, text)) {
        System.out.println("FOUND ENTRY " + nextElement.getName() + " in file " + f.getAbsolutePath());
        out.write(nextElement.getName() + "\n");
       }
      }
     }
     catch (Exception e) {
      e.printStackTrace();
     }
     out.write("\n\n");
    }
    else {

    }
   }
  }
 }



 private boolean findInInputStream(InputStream is, String text) throws IOException {
  boolean result = false;
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  String line = null;
  while ( (line = br.readLine()) != null && result == false) {
   if (line.contains(text)) {
    result = true;
   }
  }
  br.close();
  is.close();
  return result;
 }
}




Tuesday, May 11, 2010

Java class to search for text in jars

http://javatoolsforweblogic.googlecode.com/svn-history/r24/trunk/JARSearcher/src/com/pierre/jar/JarSearcher.java

it's VERY quick and dirty but it seems to work...

Here http://javatoolsforweblogic.googlecode.com/svn/trunk/JARSearcher/allWeblogicJarFiles.zip is the list of classes in a Weblogic OSB installation.... much quicker to search the text file than running every time the JavaClassFinder utility!