title: How to correct a mess java .class file set or generate a proper .jar archive from a mess .class set?
body: <h1>Background</h1>
  <p>I have to contact different kind of Java project with various of build system. Sometimes the directory structure is different from the package hierarchy. So it is difficult to package.</p>
  <p>Even if the build system like Maven and Gradle have its own function to pack .jar but it need a qualified internet connection and a giant size of local repository. Hence I usually build the library I need on the desktop in my office. However I spend more time on my laptop which doesn't have such good connection and storage.</p>
  <h1>Question</h1>
  <p>Is there a stable, compatible and secure way to make .jar package? Or move those .class file to appropriate directory? (An official and mature open source tool is better)</p>
  <p>I am a beginner of Java Language, so any point may be helpful for me.</p>
  <h2>In order to make it clearly, here I put some examples.</h2>
  <h3>Case 1: If there is a tool which can moving (moving or copying) the .class file in correct directory it should behave as below:</h3>
  <p>Before operation:</p>
  <pre><code>from_root/
  ├── a.class
  ├── b.class
  ├── c.class
  ├── d.class
  ├── e.class
  └── f.class
  </code></pre>
  <p>Note in each source file(.java) of .class file, there is package announcement(like <code>package org.hello.world;</code> or <code>package org.hello;</code>)</p>
  <p>when typing <code>the_tool ./from_root ./to_root</code> in shell</p>
  <p>it will become:</p>
  <pre><code>to_root/
  └── org
      └── hello
          ├── a.class
          ├── b.class
          └── world
              ├── c.class
              ├── d.class
              ├── e.class
              └── f.class
  </code></pre>
  <h3>Case 2: If there is a tool which can packing correct .jar file from mess .class file directory it should behave as below:</h3>
  <p>Before operation:</p>
  <pre><code>from_root/
  ├── a.class
  ├── b.class
  ├── c.class
  ├── d.class
  ├── e.class
  └── f.class
  </code></pre>
  <p>when typing <code>pack_tool ./from_root ./to_root/pack.jar</code> in shell</p>
  <p>A <code>pack.jar</code> will be generate in <code>./to_root</code>.</p>
  <p>When add it in Java Build Path of Eclipse, it should be imported and called properly, instead of wrong name-space hierarchy .</p>
  <h1>What I have tried</h1>
  <h2>For some famous 3rd part library (e.g. apache tika)</h2>
  <p>It is build by maven, and I typed <code>cd /path/to/tika-1.18-src/tika-1.18/</code> then I typed <code>mvn package</code>.</p>
  <p>Unfortunately, it was failed.</p>
  <p>However when I typed <code>mvn compile</code>, everything went right.</p>
  <p>Then in order to build .jar package, I typed <code>jar cvf ./build/tika-1.18.jar $(find ./ -name org | grep target)</code> in terminal.</p>
  <p>However, the structure inside the .jar package was totally wrong. I couldn't use it in my project.</p>
  <p>Then I tried <code>find ./ -name org | grep target | parallel cp {} ./build/ -R -f</code> then <code>cd ./build</code> and finally <code>jar cvf ./tika-1.18.jar org</code>.</p>
  <p>It worked. However there is some disadvantage of this method.</p>
  <h3>Shortcoming:</h3>
  <ol>
  <li><p>If the some source file(.java file)'s name include 'target', it will cause big trouble.</p>
  </li>
  <li><p>If different subproject contain same file it will cause conflict. For example here is the conflict information:
  cp: cannot create directory './build/org/apache/tika/batch': File exists
  cp: cannot create directory './build/org/apache/tika/language/translate': File exists</p>
  </li>
  <li><p>This method can only handle the situation which the path of those .class file is partly correct. If all the .class file are located in same directory flatly, it will failed to work.</p>
  </li>
  </ol>
  <h3>Another attemption</h3>
  <p>I tried to put the .class file in correct directory according to its binary content.</p>
  <p>For example, <code>org.apache.tika.detect.AutoDetectReader</code></p>
  <pre><code>$ hexdump -C /path/to/here/EncodingDetector.class
  00000000  ca fe ba be 00 00 00 33  00 0e 07 00 0a 07 00 0b  |.......3........|
  00000010  07 00 0c 01 00 06 64 65  74 65 63 74 01 00 54 28  |......detect..T(|
  00000020  4c 6a 61 76 61 2f 69 6f  2f 49 6e 70 75 74 53 74  |Ljava/io/InputSt|
  00000030  72 65 61 6d 3b 4c 6f 72  67 2f 61 70 61 63 68 65  |ream;Lorg/apache|
  00000040  2f 74 69 6b 61 2f 6d 65  74 61 64 61 74 61 2f 4d  |/tika/metadata/M|
  00000050  65 74 61 64 61 74 61 3b  29 4c 6a 61 76 61 2f 6e  |etadata;)Ljava/n|
  00000060  69 6f 2f 63 68 61 72 73  65 74 2f 43 68 61 72 73  |io/charset/Chars|
  00000070  65 74 3b 01 00 0a 45 78  63 65 70 74 69 6f 6e 73  |et;...Exceptions|
  00000080  07 00 0d 01 00 0a 53 6f  75 72 63 65 46 69 6c 65  |......SourceFile|
  00000090  01 00 15 45 6e 63 6f 64  69 6e 67 44 65 74 65 63  |...EncodingDetec|
  000000a0  74 6f 72 2e 6a 61 76 61  01 00 27 6f 72 67 2f 61  |tor.java..'org/a|
  000000b0  70 61 63 68 65 2f 74 69  6b 61 2f 64 65 74 65 63  |pache/tika/detec|
  000000c0  74 2f 45 6e 63 6f 64 69  6e 67 44 65 74 65 63 74  |t/EncodingDetect|
  000000d0  6f 72 01 00 10 6a 61 76  61 2f 6c 61 6e 67 2f 4f  |or...java/lang/O|
  000000e0  62 6a 65 63 74 01 00 14  6a 61 76 61 2f 69 6f 2f  |bject...java/io/|
  000000f0  53 65 72 69 61 6c 69 7a  61 62 6c 65 01 00 13 6a  |Serializable...j|
  00000100  61 76 61 2f 69 6f 2f 49  4f 45 78 63 65 70 74 69  |ava/io/IOExcepti|
  00000110  6f 6e 06 01 00 01 00 02  00 01 00 03 00 00 00 01  |on..............|
  00000120  04 01 00 04 00 05 00 01  00 06 00 00 00 04 00 01  |................|
  00000130  00 07 00 01 00 08 00 00  00 02 00 09              |............|
  0000013c
  </code></pre>
  <p>In order to compare I used another .class file <code>org.apache.tika.embedder.Embedder</code></p>
  <pre><code>$ hexdump -C ./Embedder.class 
  00000000  ca fe ba be 00 00 00 33  00 14 07 00 0f 07 00 10  |.......3........|
  00000010  07 00 11 01 00 16 67 65  74 53 75 70 70 6f 72 74  |......getSupport|
  00000020  65 64 45 6d 62 65 64 54  79 70 65 73 01 00 36 28  |edEmbedTypes..6(|
  00000030  4c 6f 72 67 2f 61 70 61  63 68 65 2f 74 69 6b 61  |Lorg/apache/tika|
  00000040  2f 70 61 72 73 65 72 2f  50 61 72 73 65 43 6f 6e  |/parser/ParseCon|
  00000050  74 65 78 74 3b 29 4c 6a  61 76 61 2f 75 74 69 6c  |text;)Ljava/util|
  00000060  2f 53 65 74 3b 01 00 09  53 69 67 6e 61 74 75 72  |/Set;...Signatur|
  00000070  65 01 00 58 28 4c 6f 72  67 2f 61 70 61 63 68 65  |e..X(Lorg/apache|
  00000080  2f 74 69 6b 61 2f 70 61  72 73 65 72 2f 50 61 72  |/tika/parser/Par|
  00000090  73 65 43 6f 6e 74 65 78  74 3b 29 4c 6a 61 76 61  |seContext;)Ljava|
  000000a0  2f 75 74 69 6c 2f 53 65  74 3c 4c 6f 72 67 2f 61  |/util/Set&lt;Lorg/a|
  000000b0  70 61 63 68 65 2f 74 69  6b 61 2f 6d 69 6d 65 2f  |pache/tika/mime/|
  000000c0  4d 65 64 69 61 54 79 70  65 3b 3e 3b 01 00 05 65  |MediaType;&gt;;...e|
  000000d0  6d 62 65 64 01 00 76 28  4c 6f 72 67 2f 61 70 61  |mbed..v(Lorg/apa|
  000000e0  63 68 65 2f 74 69 6b 61  2f 6d 65 74 61 64 61 74  |che/tika/metadat|
  000000f0  61 2f 4d 65 74 61 64 61  74 61 3b 4c 6a 61 76 61  |a/Metadata;Ljava|
  00000100  2f 69 6f 2f 49 6e 70 75  74 53 74 72 65 61 6d 3b  |/io/InputStream;|
  00000110  4c 6a 61 76 61 2f 69 6f  2f 4f 75 74 70 75 74 53  |Ljava/io/OutputS|
  00000120  74 72 65 61 6d 3b 4c 6f  72 67 2f 61 70 61 63 68  |tream;Lorg/apach|
  00000130  65 2f 74 69 6b 61 2f 70  61 72 73 65 72 2f 50 61  |e/tika/parser/Pa|
  00000140  72 73 65 43 6f 6e 74 65  78 74 3b 29 56 01 00 0a  |rseContext;)V...|
  00000150  45 78 63 65 70 74 69 6f  6e 73 07 00 12 07 00 13  |Exceptions......|
  00000160  01 00 0a 53 6f 75 72 63  65 46 69 6c 65 01 00 0d  |...SourceFile...|
  00000170  45 6d 62 65 64 64 65 72  2e 6a 61 76 61 01 00 21  |Embedder.java..!|
  00000180  6f 72 67 2f 61 70 61 63  68 65 2f 74 69 6b 61 2f  |org/apache/tika/|
  00000190  65 6d 62 65 64 64 65 72  2f 45 6d 62 65 64 64 65  |embedder/Embedde|
  000001a0  72 01 00 10 6a 61 76 61  2f 6c 61 6e 67 2f 4f 62  |r...java/lang/Ob|
  000001b0  6a 65 63 74 01 00 14 6a  61 76 61 2f 69 6f 2f 53  |ject...java/io/S|
  000001c0  65 72 69 61 6c 69 7a 61  62 6c 65 01 00 13 6a 61  |erializable...ja|
  000001d0  76 61 2f 69 6f 2f 49 4f  45 78 63 65 70 74 69 6f  |va/io/IOExceptio|
  000001e0  6e 01 00 27 6f 72 67 2f  61 70 61 63 68 65 2f 74  |n..'org/apache/t|
  000001f0  69 6b 61 2f 65 78 63 65  70 74 69 6f 6e 2f 54 69  |ika/exception/Ti|
  00000200  6b 61 45 78 63 65 70 74  69 6f 6e 06 01 00 01 00  |kaException.....|
  00000210  02 00 01 00 03 00 00 00  02 04 01 00 04 00 05 00  |................|
  00000220  01 00 06 00 00 00 02 00  07 04 01 00 08 00 09 00  |................|
  00000230  01 00 0a 00 00 00 06 00  02 00 0b 00 0c 00 01 00  |................|
  00000240  0d 00 00 00 02 00 0e                              |.......|
  00000247
  </code></pre>
  <p><strong>What's amazing is the content after &quot;...SourceFile...&quot; is the package location of this class.</strong></p>
  <p>It is possible to write a program which can scan every .class file and determine their location in the directory. However, there is Java 9, Java 10 and Java 11 will coming soon. different java version will cause the different binary content of .class files. And it might be different between JDK and OpenJDK. So it is not compatible enough. But on the other hand, it shows that it is possible to determine the package location of a certain .class file without other infomation.</p>
  <p><strong>Hope someone can provide some ideas, thanks sincerely!</strong></p>