LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-16-2009, 11:08 AM   #1
wakeboarder3780
Member
 
Registered: Mar 2006
Distribution: Ubuntu 6.10
Posts: 112

Rep: Reputation: 15
searching contents of .class file within jar (for strings)


This may sound a bit odd, but at work we are having some difficulty figuring out what code is the culprit of printing to stdout. Our console is flooded with info lets say it's this:

System.out Tag read -> myTag
System.out Tag name -> myName

I want to be able to search in every jar, in every class file (they may be binary, but the strings will still be readable) to see if "Tag read" is in the .class file. For instance if it werent for jars I could do this:

find -iname "*.class" -print0 | xargs -0 -n1 grep Tag\ read

I don't know how to handle this when they are also jar'd up in an archive.


find . -iname "*.jar" -print0 | xargs -0 -n1 ??what goes here??

It would be nice if I didn't actually have to extract files, but if I could specify a tmp location to extract to, so it doesn't muddy up the repository directories that would also be acceptable.
 
Old 02-16-2009, 01:50 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

if you only want to know first, in which files the string appears, you can use the following sample (works only with GNU find!):
Code:
find . -iname '*.jar' -printf "unzip -c %p | grep -q 'Tag read' && echo %p\n" | sh
The find prints a command line like this: unzip -c /path/to/something.jar | grep -q 'Tag read' && echo /path/to/something.jar for each found file. This command is piped to a shell. The -c option of unzip prints the content to stdout, this output is piped to the grep. It's -q option suppresses the normal output and returns immediately with exit code zero, if a line matches.

Jan

EDIT: If you want to know, in which class within the archive the string appears, try this:
Code:
find . -iname '*.jar' -print | while read jar; do
  echo "$jar:"
  unzip -qq -l $jar | sed 's/.* //' | while read cls; do
    unzip -c $jar $cls | grep -q 'Tag read' && echo "   "$cls
  done
done

Last edited by jan61; 02-16-2009 at 02:04 PM.
 
Old 02-17-2009, 07:42 AM   #3
wakeboarder3780
Member
 
Registered: Mar 2006
Distribution: Ubuntu 6.10
Posts: 112

Original Poster
Rep: Reputation: 15
wow I really wish I knew more about how this line worked because it works flawlessly and is disgustingly fast. It looks like you are printing a custom command out to the shell (sh). I understand everything except the %p. What command does %p belong to? Is it a built in shell variable? I realize its whatever is found by the find command.. but I'm very confused as to how it's ever stored there.

Also, does 'sh' always default to whatever shell is currently in use?
 
Old 02-17-2009, 07:52 AM   #4
wakeboarder3780
Member
 
Registered: Mar 2006
Distribution: Ubuntu 6.10
Posts: 112

Original Poster
Rep: Reputation: 15
Well I found out where the %p lives, I should have read the manual harder before jumping on here. (I thought typing '/%p' while in the man page would jump to it, but I guess not)

For anyone else reading this:
%p is part of the -printf option within the find command. It will be resolved to the name of the file.

Furthermore 'sh' is actually the command for the gnu bourne again shell, and here I thought it was 'bash'. Apparently you can run either?
 
Old 02-17-2009, 01:49 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

Quote:
Originally Posted by wakeboarder3780 View Post
Furthermore 'sh' is actually the command for the gnu bourne again shell, and here I thought it was 'bash'. Apparently you can run either?
on many systems sh is a link to bash:
Code:
jan@jack:~/tmp> ls -li `which bash`
40908 -rwxr-xr-x  1 root root 447256 2004-04-24 20:57 /bin/bash
jan@jack:~/tmp> ls -li `which sh`
40812 lrwxrwxrwx  1 root root 4 2004-04-28 22:19 /bin/sh -> bash
For the commands used in this case it doesn't matter, to which shell you are piping the output - there are no bash specific commands.

Jan
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to set class path of a jar file priyapratheep Programming 1 08-07-2008 03:12 AM
Modify the contents of a jar file. niks27 Linux - Newbie 2 09-05-2007 10:09 AM
Searching through strings in StarBasic/OpenOffice BASIC scuffell Programming 1 03-31-2006 09:50 AM
Package .jar file for BalancerFilter class minil Programming 0 12-20-2005 04:20 AM
Searching files for strings tmoorman Linux - Software 4 01-08-2004 01:46 PM


All times are GMT -5. The time now is 01:25 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration