예제 #1
0
  protected void generateJarFile(File jarFile, boolean standAlone, File dir) throws IOException {
    Verbose.log("Generating jar file ... " + jarFile);
    FileOutputStream out = new FileOutputStream(jarFile);
    InputStream manifestIo = getManifest();
    Manifest manifest = new Manifest(manifestIo);
    JarOutputStream jar = new JarOutputStream(out, manifest);

    int rootLen = dir.getAbsolutePath().length();

    int len = "java".length();
    for (File javaFile : javaFiles) {
      String fileName = javaFile.getPath();
      String classFile = fileName.substring(0, fileName.length() - len) + "class";
      String className = classFile.substring(rootLen + 1);
      addFileToJar(className, classFile, jar);

      String javaName = fileName.substring(rootLen + 1);
      addFileToJar(javaName, fileName, jar);
    }

    if (standAlone) {
      Verbose.log("Adding runtime classes to the jar");
      addRuntimeClasses(jar);
    }

    jar.close();
    out.close();
    Verbose.log("Generated jar file " + jarFile);
  }
예제 #2
0
  protected void addFileToJar(String className, String classFile, JarOutputStream jar)
      throws IOException {
    className = className.replace('\\', '/');
    FileInputStream fio = new FileInputStream(classFile);
    jar.putNextEntry(new JarEntry(className));

    int cb;
    byte[] buffer = new byte[8192];
    while ((cb = fio.read(buffer)) != -1) {
      jar.write(buffer, 0, cb);
    }

    jar.closeEntry();
    fio.close();
  }
예제 #3
0
 protected void addRuntimeClasses(JarOutputStream jar) throws IOException {
   ClassLoader cl = Thread.currentThread().getContextClassLoader();
   if (cl == null) {
     cl = getClass().getClassLoader();
   }
   ArrayList<String> runtimeClasses = getRuntimeClasses(cl);
   for (String c : runtimeClasses) {
     jar.putNextEntry(new JarEntry(c));
     InputStream in = cl.getResourceAsStream(c);
     int ch;
     while ((ch = in.read()) != -1) {
       jar.write((char) ch);
     }
     jar.closeEntry();
     in.close();
   }
 }