JAR文件夹递归?

时间:2012-02-19 19:49:15

标签: java jar

我如何JAR tempfolder到temp.jar?

因为这个当前代码似乎确实成功地创建了jar:

public void makeJar() throws IOException {
    File directoryTojar = new File("tempfinal");

    List<File> fileList = new ArrayList<File>();
    System.out.println("---Getting references to all files in: " + directoryTojar.getCanonicalPath());
    getAllFiles(directoryTojar, fileList);
    System.out.println("---Creating jar file");
    writejarFile(directoryTojar, fileList);
    System.out.println("---Done");
}

public void getAllFiles(File dir, List<File> fileList) {
    try {
        File[] files = dir.listFiles();
        for (File file : files) {
            fileList.add(file);
            if (file.isDirectory()) {
                System.out.println("directory:" + file.getCanonicalPath());
                getAllFiles(file, fileList);
            } else {
                System.out.println("     file:" + file.getCanonicalPath());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void writejarFile(File directoryTojar, List<File> fileList) {

    try {
        FileOutputStream fos = new FileOutputStream(directoryTojar.getName() + ".jar");
        JarOutputStream zos = new JarOutputStream(fos);

        for (File file : fileList) {
            if (!file.isDirectory()) { // we only jar files, not directories
                addTojar(directoryTojar, file, zos);
            }
        }

        zos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void addTojar(File directoryTojar, File file, JarOutputStream zos) throws FileNotFoundException, IOException {

    FileInputStream fis = new FileInputStream(file);

    // we want the jarEntry's path to be a relative path that is relative
    // to the directory being jarped, so chop off the rest of the path
    String jarFilePath = file.getCanonicalPath().substring(directoryTojar.getCanonicalPath().length() + 1,
    file.getCanonicalPath().length());
    System.out.println("Writing '" + jarFilePath + "' to jar file");
    JarEntry jarEntry = new JarEntry(jarFilePath);
    zos.putNextEntry(jarEntry);

    byte[] bytes = new byte[4096];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

    zos.closeEntry();
        fis.close();

}

当它试图找到net.minecraft.client.MinecraftApplet时,它说它找不到但是它在jar文件中。

即使我将.jar解压缩到一个文件夹,然后立即重新启动它,它仍然会出现此错误:

java.lang.ClassNotFoundException: net.minecraft.client.MinecraftApplet
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at net.minecraft.GameUpdater.createApplet(GameUpdater.java:369)
at net.minecraft.Launcher$1.run(Launcher.java:88)

出了什么问题?

0 个答案:

没有答案