test Gen Jar - Java Reflection

Java examples for Reflection:Jar

Description

test Gen Jar

Demo Code


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

public class Main{
    public static void testGenJar() {
        try {//from ww w.j  ava2 s  .co  m
            String javaClassPath = "C:/classes/";// E:/workspace/zcloud-education/target/classes/
            String targetPath = System.getProperty("user.home")
                    + File.separator + "temp" + File.separator
                    + "education.jar";
            System.out.println("targetPath:" + targetPath);
            JarUtil util = new JarUtil(javaClassPath, targetPath);
            util.generateJar();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void generateJar() throws FileNotFoundException, IOException {

        System.out.println("*** --> ?????jar?...");
        String targetDirPath = targetPath.substring(0,
                targetPath.lastIndexOf(File.separator));
        File targetDir = new File(targetDirPath);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
                "1.0");

        JarOutputStream target = new JarOutputStream(new FileOutputStream(
                targetPath), manifest);
        writeClassFile(new File(javaClassPath), target);
        target.close();
        System.out.println("*** --> jar???????");
    }
    private void writeClassFile(File source, JarOutputStream target)
            throws IOException {
        BufferedInputStream in = null;
        try {
            if (source.isDirectory()) {
                String name = source.getPath().replace("\\", "/");
                if (!name.isEmpty()) {
                    if (!name.endsWith("/")) {
                        name += "/";
                    }
                    name = name.substring(javaClassPath.length());
                    if (!name.equals("")) {// fix bug
                        JarEntry entry = new JarEntry(name);
                        entry.setTime(source.lastModified());
                        target.putNextEntry(entry);
                        target.closeEntry();
                    }
                }
                for (File nestedFile : source.listFiles())
                    writeClassFile(nestedFile, target);
                return;
            }

            String middleName = source.getPath().replace("\\", "/")
                    .substring(javaClassPath.length());
            if (middleName.endsWith(".class")) {
                JarEntry entry = new JarEntry(middleName);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                in = new BufferedInputStream(new FileInputStream(source));

                byte[] buffer = new byte[1024];
                while (true) {
                    int count = in.read(buffer);
                    if (count == -1)
                        break;
                    target.write(buffer, 0, count);
                }
            }
            target.closeEntry();
        } finally {
            if (in != null)
                in.close();
        }
    }
}

Related Tutorials