title: Can I try/catch this error: "An unexpected error occurred while trying to open jar"
body: <p>I am working on my first non-trivial Java app. It was started by a co-worker, but she didn't have time to finish, so I took it over. I decided to use Buildr as my build tool: <a href="https://buildr.apache.org/" rel="nofollow">https://buildr.apache.org/</a></p>
  
  <p>First, to start with empty target directories, I do: </p>
  
  <pre><code>buildr clean
  </code></pre>
  
  <p>Then I: </p>
  
  <pre><code>buildr --verbose compile
  </code></pre>
  
  <p>which gives me: </p>
  
  <pre><code>(in /Users/cerhov/projects/openz/lofdg/buildr_fdg, development)
  Compiling buildr_fdg
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
  cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/students.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/students.txt
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
  cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/scores.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/scores.txt
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
  cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/behavior.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/behavior.txt
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
  cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/schoolDates.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/schoolDates.txt
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
  cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/assignments.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/assignments.txt
  touch /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/classes
  Compiling buildr_fdg into /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/classes
  Note: /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/java/com/company/Main.java uses or overrides a deprecated API.
  Note: Recompile with -Xlint:deprecation for details.
  touch /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/classes
  Completed in 0.926s
  </code></pre>
  
  <p>This gives me my ".class" files. Now I want to package this up as a jar, so I:</p>
  
  <pre><code>cerhov : 15:26:04 : ~/projects/openz/lofdg/buildr_fdg $ buildr --verbose package
  </code></pre>
  
  <p>which gives me: </p>
  
  <pre><code>(in /Users/cerhov/projects/openz/lofdg/buildr_fdg, development)
  Building buildr_fdg
  Packaging buildr_fdg
  Packaging buildr_fdg-1.0.0.jar
  rm /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/buildr_fdg-1.0.0.jar
  mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target
  Running integration tests...
  Completed in 0.212s
  </code></pre>
  
  <p>Then I do this: </p>
  
  <pre><code>cerhov : 15:26:56 : ~/projects/openz/lofdg/buildr_fdg $ ls target/
  </code></pre>
  
  <p>and I see a new Jar file has been created:</p>
  
  <pre><code>buildr_fdg-1.0.0.jar    classes         resources
  </code></pre>
  
  <p>so I: </p>
  
  <pre><code>java -jar target/buildr_fdg-1.0.0.jar 
  </code></pre>
  
  <p>but I get:</p>
  
  <pre><code>Error: An unexpected error occurred while trying to open file target/buildr_fdg-1.0.0.jar
  </code></pre>
  
  <p>I'd like to get more info so I wrapped my whole main() function in a big try/catch statement:</p>
  
  <pre><code>public static void main(String[] args) {
  try {
      // all my real code goes here, but I have deleted it for clarity
  } catch (Exception e) {
      e.printStackTrace();
  }
  }
  </code></pre>
  
  <p>Then I re-compiled and re-packaged, but I still get the same error. I am unclear how I can force the app to give me more info. </p>
  
  <p>I wanted to see if my manifest.txt got into my jar, so I copied the jar to another folder, cd'ed to that folder, and:</p>
  
  <pre><code> jar xf buildr_fdg-1.0.0.jar     
  </code></pre>
  
  <p>and then this:</p>
  
  <pre><code>cat META-INF/MANIFEST.MF 
  </code></pre>
  
  <p>showed me: </p>
  
  <pre><code>Created-By: Buildr
  : 
  Manifest-Version: 1.0
  Main-Class: com/company/Main
  </code></pre>
  
  <p>Which looks right. Maybe I can add in a classpath, though Buildr seems to have found all the files.</p>
  
  <p>How do I force the app to give me more information about the problem? </p>
  
  <p>UPDATE:</p>
  
  <p>I tried adding this line to the manifest: </p>
  
  <pre><code>Class-Path: /Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/java/com/company
  </code></pre>
  
  <p>Then I re-compiled and re-packaged. But this: </p>
  
  <pre><code>java -cp buildr_fdg-1.0.0.jar com.company.Main
  </code></pre>
  
  <p>gave me:</p>
  
  <pre><code>Error: Could not find or load main class com.company.Main
  </code></pre>
  
  <p>So I tried every variation: </p>
  
  <pre><code>/Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/java/com/
  
  /Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/java/
  
  /Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/
  
  /Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/
  
  /Users/cerhov/projects/launchopen/lofdg/buildr_fdg/
  </code></pre>
  
  <p>None of which work. Am I suppose to aim the classpath at the source directory or the target directory? </p>
  
  <p>Why is Buildr unable to manage this for me? It does find all of the files and it does bundle them all into the jar, so it knows where everything is. </p>