{"id":1050,"date":"2017-01-15T21:40:54","date_gmt":"2017-01-15T21:40:54","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=1050"},"modified":"2017-01-15T21:40:54","modified_gmt":"2017-01-15T21:40:54","slug":"command-line-fun-finding-the-right-jar-file","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/01\/15\/command-line-fun-finding-the-right-jar-file\/","title":{"rendered":"command line fun &#8211; finding the right jar file"},"content":{"rendered":"<p>Where did I set down my mobile phone is quickly answered by a quick telephone call or a sms from another phone. \u00a0If everything were only that simple.<\/p>\n<p>I receive a bit of code from a buddy at work that showed how to do a special calculation with the vendors software using one of the undocumented classes. \u00a0I knew I would need it one day so it was cast into my Download directory waiting for that day.<\/p>\n<p>By the time I actually needed it, my friend had long left the company. \u00a0It was literally only 15 lines long including all the necessary class declarations and comments. \u00a0The only thing that she didn&#8217;t tell me was which jar file(s) I would need in my classpath.<\/p>\n<p>This was a real problem as the vendor has slightly under 700 jar files and only about six of them are intended to be used by the customer. \u00a0The good thing about Java is that it is really easy to see which classes are the jar file. \u00a0This is easy because the jar file is the same structure as a zip file. \u00a0This means we can use unzip or winzip to look at the contents of the library.<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; unzip -tv special.jar\r\nArchive: special.jar\r\n    testing: META-INF\/                      OK\r\n    META-INF\/MANIFEST.MF                    OK\r\n    powerfulclasses\/                        OK\r\n    powerfulclasses\/myMath.class   OK\r\n    powerfulclasses\/bobsCode.class   OK<\/code><\/pre>\n<\/div>\n<p>The best course of action would be to write up a script to list the contents of each jar and do a quick grep to find the class in my example. \u00a0If I find the class then I have found the jar, well or you might also find a jar that contains an interface for that class or even a jar that contains a similarly named class.<\/p>\n<p>Despite the name of this post, there is really not so much effort for finding the jars. \u00a0The jar files are in a series of subdirectories grouped by different subsystems. \u00a0It is however much easier to use the find command to gather up the list of jar files.<\/p>\n<pre><code>find \/opt\/vendorsoft\/support\/lib -name \"*jar\" \r\n<\/code><\/pre>\n<p>I wish I could have created a single line command to gather up the jars, extract the contents and find the special class but with an eye towards expediency I simply created two small scripts.<\/p>\n<p>The first script just gathers up all jar files using the find command and passes the jar files one at a time to the checkit.sh script. \u00a0The find command gathers the jars while the xargs command processes them one at a time.<\/p>\n<p><strong>findit.sh<\/strong><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#searchpath=\/opt\/vendorsoft\/support\/lib\r\nsearchpath=.\r\nsearchfor=Gen2CatsCompany\r\nfind $searchpath -name &quot;*jar&quot; -print | xargs -L 1 .\/checkit.sh $searchfor\r\n<\/pre>\n<p>Each file is passed to the checkit.sh script. \u00a0For this particular example each call can be thought of like this.<\/p>\n<pre>checkit.sh $searchfor &lt;file here&gt;\r\n<\/pre>\n<p>The checkit script is even simpler because it is essentially just doing an unzip and piping the output through grep. \u00a0That part could have been done using the &#8220;-exec&#8221; option for the find command. \u00a0The only problem is it would find the class but not the name or path of the jar file.<\/p>\n<p><strong>checkit.sh<\/strong><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\necho $2\r\nunzip -tv $2 | grep $1 &amp;gt;\/dev\/null 2&amp;gt;&amp;amp;1\r\nif [ $? -eq 0 ]\r\nthen\r\n echo .\r\n echo found $1 in file $2\r\n echo .\r\nfi\r\n<\/pre>\n<p>The requirements are simple &#8211; find the jar &#8211; and the output is also just as simple.<\/p>\n<div class=\"sbody-code\">\n<pre><code>&gt; .\/findit.sh\r\n.\/one\/NCSO.jar\r\n.\/one\/mqTransferGenesis.jar\r\n.\r\nfound ExceptionIFGenesis in file .\/one\/mqTransferGenesis.jar\r\n.\r\n.\/one\/jconn2.jar\r\n.\/one\/jconn3.jar\r\n.\/one\/jdom.jar\r\n.\/one\/jhall.jar\r\n.\/one\/junit-4.4.jar\r\n.\/one\/jython.jar\r\n.\/one\/jythonlib.jar\r\n.\/one\/log4j-1.2.14.jar\r\n.\/one\/ojdbc14.jar\r\n.\/one\/surf.jar\r\n.\/two\/mail.jar\r\n&gt;\r\n<\/code><\/pre>\n<\/div>\n<p>I did use this small helper to go through the list of all the vendor jar files. Despite having a relatively slow development environment the answer came back in under a minute.<\/p>\n<p>With the necessary jar files I was able to compile my code sample.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Where did I set down my mobile phone is quickly answered by a quick telephone call or a sms from another phone. \u00a0If everything were only that simple. I receive a bit of code from a buddy at work that &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/01\/15\/command-line-fun-finding-the-right-jar-file\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,20],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1050"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=1050"}],"version-history":[{"count":8,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1050\/revisions"}],"predecessor-version":[{"id":1669,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1050\/revisions\/1669"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}