Example usage for java.util.jar JarOutputStream JarOutputStream

List of usage examples for java.util.jar JarOutputStream JarOutputStream

Introduction

In this page you can find the example usage for java.util.jar JarOutputStream JarOutputStream.

Prototype

public JarOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new JarOutputStream with no manifest.

Usage

From source file:org.apache.hadoop.mapreduce.v2.TestMRJobs.java

private Path makeJobJarWithLib(String testDir) throws FileNotFoundException, IOException {
    Path jobJarPath = new Path(testDir, "thejob.jar");
    FileOutputStream fos = new FileOutputStream(new File(jobJarPath.toUri().getPath()));
    JarOutputStream jos = new JarOutputStream(fos);
    // Have to put in real jar files or it will complain
    createAndAddJarToJar(jos, new File(new Path(testDir, "lib1.jar").toUri().getPath()));
    createAndAddJarToJar(jos, new File(new Path(testDir, "lib2.jar").toUri().getPath()));
    jos.close();/*from w w  w .  ja  va2 s  .  co  m*/
    localFs.setPermission(jobJarPath, new FsPermission("700"));
    return jobJarPath;
}

From source file:org.apache.hadoop.mapreduce.v2.TestMRJobs.java

private void createAndAddJarToJar(JarOutputStream jos, File jarFile) throws FileNotFoundException, IOException {
    FileOutputStream fos2 = new FileOutputStream(jarFile);
    JarOutputStream jos2 = new JarOutputStream(fos2);
    // Have to have at least one entry or it will complain
    ZipEntry ze = new ZipEntry("lib1.inside");
    jos2.putNextEntry(ze);/*from   w  ww. j  av a  2  s.  com*/
    jos2.closeEntry();
    jos2.close();
    ze = new ZipEntry("lib/" + jarFile.getName());
    jos.putNextEntry(ze);
    FileInputStream in = new FileInputStream(jarFile);
    byte buf[] = new byte[1024];
    int numRead;
    do {
        numRead = in.read(buf);
        if (numRead >= 0) {
            jos.write(buf, 0, numRead);
        }
    } while (numRead != -1);
    in.close();
    jos.closeEntry();
    jarFile.delete();
}

From source file:lu.fisch.moenagade.model.Project.java

public void jar() {
    try {//from   ww w. j a v  a  2s . com
        // compile all
        if (!save())
            return;

        generateSource(true);
        if (compile()) {
            // adjust the dirname
            String bdir = getDirectoryName();
            if (!bdir.endsWith(System.getProperty("file.separator"))) {
                bdir += System.getProperty("file.separator");
            }

            // adjust the filename
            String bname = getDirectoryName();
            if (bname.endsWith(System.getProperty("file.separator"))) {
                bname = bname.substring(0, bname.length() - 1);
            }
            bname = bname.substring(bname.lastIndexOf(System.getProperty("file.separator")) + 1);

            // default class to launch
            String mc = "moenagade.Project";

            // target JVM
            String target = "1.8";

            /*
            String[] targets = new String[]{"1.1","1.2","1.3","1.5","1.6"};
            if(System.getProperty("java.version").startsWith("1.7"))
            targets = new String[]{"1.1","1.2","1.3","1.5","1.6","1.7"};
            if(System.getProperty("java.version").startsWith("1.8"))
            targets = new String[]{"1.1","1.2","1.3","1.5","1.6","1.7","1.8"};
                    
            target= (String) JOptionPane.showInputDialog(
                           frame,
                           "Please enter version of the JVM you want to target.",
                           "Target JVM",
                           JOptionPane.QUESTION_MESSAGE,
                           Moenagade.IMG_QUESTION,
                           targets,
                           "1.6");*/

            File fDir = new File(directoryName + System.getProperty("file.separator") + "bin");
            if (!fDir.exists())
                fDir.mkdir();

            // get all the files content
            Hashtable<String, String> codes = new Hashtable<>();
            String srcdir = directoryName + System.getProperty("file.separator") + "src";
            Collection files = FileUtils.listFiles(new File(srcdir), new String[] { "java" }, true);

            File[] javas = new File[files.size()];
            int i = 0;
            for (Iterator iterator = files.iterator(); iterator.hasNext();) {
                File file = (File) iterator.next();
                javas[i++] = file;
            }

            try {
                // make class files
                Runtime6.getInstance().compileToPath(javas, fDir.getAbsolutePath(), target, "");

                StringList manifest = new StringList();
                manifest.add("Manifest-Version: 1.0");
                manifest.add("Created-By: " + Moenagade.E_VERSION + " " + Moenagade.E_VERSION);
                manifest.add("Name: " + bname);
                if (mc != null) {
                    manifest.add("Main-Class: " + mc);
                }

                // compose the filename
                fDir = new File(bdir + "dist" + System.getProperty("file.separator"));
                fDir.mkdir();
                bname = bdir + "dist" + System.getProperty("file.separator") + bname + ".jar";
                String baseName = bdir;
                String libFolderName = bdir + "lib";
                String distLibFolderName = bdir + "dist" + System.getProperty("file.separator") + "lib";

                File outFile = new File(bname);
                FileOutputStream bo = new FileOutputStream(bname);
                JarOutputStream jo = new JarOutputStream(bo);

                String dirname = getDirectoryName();
                if (!dirname.endsWith(System.getProperty("file.separator"))) {
                    dirname += System.getProperty("file.separator");
                }
                // add the files to the array
                addToJar(jo, "", new File(dirname + "bin" + System.getProperty("file.separator")));
                // add the files to the array
                addToJar(jo, "", new File(dirname + "src" + System.getProperty("file.separator")),
                        new String[] { "java" });

                //manifest.add("Class-Path: "+cp+" "+cpsw);

                // adding the manifest file
                manifest.add("");
                JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
                jo.putNextEntry(je);
                String mf = manifest.getText();
                jo.write(mf.getBytes(), 0, mf.getBytes().length);

                jo.close();
                bo.close();

                // delete bin directory
                deleteDirectory(new File(getDirectoryName() + System.getProperty("file.separator") + "bin"
                        + System.getProperty("file.separator")));
                // generate java code with dispose_on_exit
                generateSource();

                JOptionPane.showMessageDialog(frame,
                        "The JAR-archive has been generated and can\nbe found in the \"dist\" directory.",
                        "Success", JOptionPane.INFORMATION_MESSAGE, Moenagade.IMG_INFO);
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
        }
    }
    /*catch (ClassNotFoundException ex)
    {
    JOptionPane.showMessageDialog(frame, "There was an error while creating the JAR-archive ...", "Error :: ClassNotFoundException", JOptionPane.ERROR_MESSAGE,Unimozer.IMG_ERROR);
    }*/
    catch (IOException ex) {
        JOptionPane.showMessageDialog(frame, "There was an error while creating the JAR-archive ...",
                "Error :: IOException", JOptionPane.ERROR_MESSAGE, Moenagade.IMG_ERROR);
        ex.printStackTrace();
    }
}

From source file:lu.fisch.unimozer.Diagram.java

public void jar() {
    try {/*  w  ww  .ja  v  a 2 s  . c o m*/
        // compile all
        if (compile())
            if (save()) {

                // adjust the dirname
                String dir = getDirectoryName();
                if (!dir.endsWith(System.getProperty("file.separator"))) {
                    dir += System.getProperty("file.separator");
                }

                // adjust the filename
                String name = getDirectoryName();
                if (name.endsWith(System.getProperty("file.separator"))) {
                    name = name.substring(0, name.length() - 1);
                }
                name = name.substring(name.lastIndexOf(System.getProperty("file.separator")) + 1);

                /*String[] classNames = new String[classes.size()+1];
                Set<String> set = classes.keySet();
                Iterator<String> itr = set.iterator();
                classNames[0]=null;
                int c = 1;
                while (itr.hasNext())
                {
                    classNames[c]=itr.next();
                    c++;
                }/**/
                Vector<String> mains = getMains();
                String[] classNames = new String[mains.size()];
                for (int c = 0; c < mains.size(); c++)
                    classNames[c] = mains.get(c);
                // default class to launch
                String mc = "";
                {
                    if (classNames.length == 0) {
                        mc = "";
                        JOptionPane.showMessageDialog(printOptions,
                                "Unimozer was unable to detect a startable class\n"
                                        + "inside your project. The JAR-archive will be created\n"
                                        + "but it won't be executable!",
                                "Mainclass", JOptionPane.INFORMATION_MESSAGE, Unimozer.IMG_INFO);
                    } else if (classNames.length == 1)
                        mc = classNames[0];
                    else
                        mc = (String) JOptionPane.showInputDialog(frame,
                                "Unimozer detected more than one runnable class.\n"
                                        + "Please select which one you want to be launched\n"
                                        + "automatically with the JAR-archive.",
                                "Autostart", JOptionPane.QUESTION_MESSAGE, Unimozer.IMG_QUESTION, classNames,
                                "");
                }
                // target JVM
                String target = null;
                if (Runtime5.getInstance().usesSunJDK() && mc != null) {

                    String[] targets = new String[] { "1.1", "1.2", "1.3", "1.5", "1.6" };
                    if (System.getProperty("java.version").startsWith("1.7"))
                        targets = new String[] { "1.1", "1.2", "1.3", "1.5", "1.6", "1.7" };
                    if (System.getProperty("java.version").startsWith("1.8"))
                        targets = new String[] { "1.1", "1.2", "1.3", "1.5", "1.6", "1.7", "1.8" };

                    target = (String) JOptionPane.showInputDialog(frame,
                            "Please enter version of the JVM you want to target.", "Target JVM",
                            JOptionPane.QUESTION_MESSAGE, Unimozer.IMG_QUESTION, targets, "1.6");
                }
                // make the class-files and all
                // related stuff
                if (((Runtime5.getInstance().usesSunJDK() && target != null)
                        || (!Runtime5.getInstance().usesSunJDK())) && (mc != null))
                    if (makeInteractive(false, target, false) == true) {

                        StringList manifest = new StringList();
                        manifest.add("Manifest-Version: 1.0");
                        manifest.add("Created-By: " + Unimozer.E_VERSION + " " + Unimozer.E_VERSION);
                        manifest.add("Name: " + name);
                        if (mc != null) {
                            manifest.add("Main-Class: " + mc);
                        }

                        // compose the filename
                        File fDir = new File(dir + "dist" + System.getProperty("file.separator"));
                        fDir.mkdir();
                        name = dir + "dist" + System.getProperty("file.separator") + name + ".jar";
                        String baseName = dir;
                        String libFolderName = dir + "lib";
                        String distLibFolderName = dir + "dist" + System.getProperty("file.separator") + "lib";

                        File outFile = new File(name);
                        FileOutputStream bo = new FileOutputStream(name);
                        JarOutputStream jo = new JarOutputStream(bo);

                        String dirname = getDirectoryName();
                        if (!dirname.endsWith(System.getProperty("file.separator"))) {
                            dirname += System.getProperty("file.separator");
                        }
                        // add the files to the array
                        addToJar(jo, "", new File(dirname + "bin" + System.getProperty("file.separator")));
                        // add the files to the array
                        addToJar(jo, "", new File(dirname + "src" + System.getProperty("file.separator")),
                                new String[] { "java" });
                        /*
                        // define a filter for files that do not start with a dot
                        FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return !name.startsWith("."); } };                     
                        // get the bin directory
                        File binDir = new File(dirname+"bin"+System.getProperty("file.separator")); 
                        // get all files
                        File[] files = binDir.listFiles(filter);
                        for(int f=0;f<files.length;f++)
                        {
                            FileInputStream bi = new FileInputStream(files[f]);
                            String entry = files[f].getAbsolutePath();
                            entry = entry.substring(binDir.getAbsolutePath().length()+1);
                            JarEntry je = new JarEntry(entry);
                            jo.putNextEntry(je);
                            byte[] buf = new byte[1024];
                            int anz;
                            while ((anz = bi.read(buf)) != -1)
                            {
                                jo.write(buf, 0, anz);
                            }
                            bi.close();
                        }
                         */

                        // ask to include another direectory
                        // directory filter
                        /*
                        FilenameFilter dirFilter = new FilenameFilter() { public boolean accept(File dir, String name) { File isDir = new File(dir+System.getProperty("file.separator")+name); return isDir.isDirectory() && !name.equals("bin") && !name.equals("src") && !name.equals("dist") && !name.equals("nbproject") && !name.equals("doc"); } };
                        // get directories
                        File projectDir = new File(dirname);
                        String[] subDirs = projectDir.list(dirFilter);
                        if(subDirs.length>0)
                        {
                            String subdir = (String) JOptionPane.showInputDialog(
                               frame,
                               "Do you want to include any other resources directory?\n"+
                               "Click ?Cancel? to not include any resources directory!",
                               "JAR Packager",
                               JOptionPane.QUESTION_MESSAGE,
                               Unimozer.IMG_QUESTION,
                               subDirs,
                               null);
                            if(subdir!=null)
                            {
                                addToJar(jo,subdir+"/",new File(dirname+subdir+System.getProperty("file.separator")));
                            }
                        }
                         */

                        /*
                        Set<String> set = classes.keySet();
                        Iterator<String> itr = set.iterator();
                        int i = 0;
                        while (itr.hasNext())
                        {
                            String classname = itr.next();
                            String act = classname + ".class";
                            FileInputStream bi = new FileInputStream(dirname+"bin"+System.getProperty("file.separator")+act);
                            JarEntry je = new JarEntry(act);
                            jo.putNextEntry(je);
                            byte[] buf = new byte[1024];
                            int anz;
                            while ((anz = bi.read(buf)) != -1)
                            {
                                jo.write(buf, 0, anz);
                            }
                            bi.close();
                        }
                         */

                        // copy libs
                        File lib = new File(libFolderName);
                        File distLib = new File(distLibFolderName);
                        StringList libs = null;
                        if (lib.exists()) {
                            libs = CopyDirectory.copyFolder(lib, distLib);
                        }
                        String cp = new String();
                        if (libs != null) {
                            for (int i = 0; i < libs.count(); i++) {
                                String myLib = libs.get(i);
                                myLib = myLib.substring(baseName.length());
                                if (i != 0)
                                    cp = cp + " ";
                                cp = cp + myLib;
                            }
                            //manifest.add("Class-Path: "+cp);
                        }

                        // Let's search for the path of the swing-layout JAR file
                        String cpsw = "";
                        if (getCompleteSourceCode().contains("org.jdesktop.layout")) {
                            if (Main.classpath != null) {
                                // copy the file
                                String src = Main.classpath;
                                File f1 = new File(src);
                                String dest = distLibFolderName + System.getProperty("file.separator")
                                        + f1.getName();
                                // create folder if not exists
                                File f2 = new File(distLibFolderName);
                                if (!f2.exists())
                                    f2.mkdir();
                                // copy the file
                                InputStream in = new FileInputStream(src);
                                OutputStream out = new FileOutputStream(dest);
                                byte[] buffer = new byte[1024];
                                int length;
                                while ((length = in.read(buffer)) > 0) {
                                    out.write(buffer, 0, length);
                                }
                                in.close();
                                out.close();
                                // add the manifest entry
                                cpsw = "lib" + System.getProperty("file.separator") + f1.getName();
                            }
                        }

                        manifest.add("Class-Path: " + cp + " " + cpsw);

                        // adding the manifest file
                        manifest.add("");
                        JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
                        jo.putNextEntry(je);
                        String mf = manifest.getText();
                        jo.write(mf.getBytes(), 0, mf.getBytes().length);

                        jo.close();
                        bo.close();

                        cleanAll();

                        JOptionPane.showMessageDialog(frame, "The JAR-archive has been generated ...",
                                "Success", JOptionPane.INFORMATION_MESSAGE, Unimozer.IMG_INFO);
                    }
            }
    }
    /*catch (ClassNotFoundException ex)
    {
    JOptionPane.showMessageDialog(frame, "There was an error while creating the JAR-archive ...", "Error :: ClassNotFoundException", JOptionPane.ERROR_MESSAGE,Unimozer.IMG_ERROR);
    }*/
    catch (IOException ex) {
        JOptionPane.showMessageDialog(frame, "There was an error while creating the JAR-archive ...",
                "Error :: IOException", JOptionPane.ERROR_MESSAGE, Unimozer.IMG_ERROR);
    }
}