Example usage for java.util.jar JarFile getInputStream

List of usage examples for java.util.jar JarFile getInputStream

Introduction

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

Prototype

public synchronized InputStream getInputStream(ZipEntry ze) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:JarRead.java

public static void main(String args[]) throws IOException {
    if (args.length != 2) {
        System.out.println("Please provide a JAR filename and file to read");
        System.exit(-1);/*w ww  .  j a  v  a  2s  .  c o  m*/
    }
    JarFile jarFile = new JarFile(args[0]);
    JarEntry entry = jarFile.getJarEntry(args[1]);
    InputStream input = jarFile.getInputStream(entry);
    process(input);
    jarFile.close();
}

From source file:com.mirth.connect.server.launcher.MirthLauncher.java

public static void main(String[] args) {
    try {//from   ww  w .j  a  va2 s.c  o m
        try {
            uninstallPendingExtensions();
            installPendingExtensions();
        } catch (Exception e) {
            logger.error("Error uninstalling or installing pending extensions.", e);
        }

        Properties mirthProperties = new Properties();
        String includeCustomLib = null;

        try {
            mirthProperties.load(new FileInputStream(new File(MIRTH_PROPERTIES_FILE)));
            includeCustomLib = mirthProperties.getProperty(PROPERTY_INCLUDE_CUSTOM_LIB);
            createAppdataDir(mirthProperties);
        } catch (Exception e) {
            logger.error("Error creating the appdata directory.", e);
        }

        ManifestFile mirthServerJar = new ManifestFile("server-lib/mirth-server.jar");
        ManifestFile mirthClientCoreJar = new ManifestFile("server-lib/mirth-client-core.jar");
        ManifestDirectory serverLibDir = new ManifestDirectory("server-lib");
        serverLibDir.setExcludes(new String[] { "mirth-client-core.jar" });

        List<ManifestEntry> manifestList = new ArrayList<ManifestEntry>();
        manifestList.add(mirthServerJar);
        manifestList.add(mirthClientCoreJar);
        manifestList.add(serverLibDir);

        // We want to include custom-lib if the property isn't found, or if it equals "true"
        if (includeCustomLib == null || Boolean.valueOf(includeCustomLib)) {
            manifestList.add(new ManifestDirectory("custom-lib"));
        }

        ManifestEntry[] manifest = manifestList.toArray(new ManifestEntry[manifestList.size()]);

        // Get the current server version
        JarFile mirthClientCoreJarFile = new JarFile(mirthClientCoreJar.getName());
        Properties versionProperties = new Properties();
        versionProperties.load(mirthClientCoreJarFile
                .getInputStream(mirthClientCoreJarFile.getJarEntry("version.properties")));
        String currentVersion = versionProperties.getProperty("mirth.version");

        List<URL> classpathUrls = new ArrayList<URL>();
        addManifestToClasspath(manifest, classpathUrls);
        addExtensionsToClasspath(classpathUrls, currentVersion);
        URLClassLoader classLoader = new URLClassLoader(classpathUrls.toArray(new URL[classpathUrls.size()]));
        Class<?> mirthClass = classLoader.loadClass("com.mirth.connect.server.Mirth");
        Thread mirthThread = (Thread) mirthClass.newInstance();
        mirthThread.setContextClassLoader(classLoader);
        mirthThread.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.peterbochs.PeterBochsDebugger.java

public static void main(String[] args) {
    WebServiceUtil.log("peter-bochs", "start", null, null, null);
    try {//from   w  w  w.  j a  v  a2s  .c o  m
        UIManager.setLookAndFeel("com.peterswing.white.PeterSwingWhiteLookAndFeel");
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (args.length == 0) {
        String errorMessage = "Wrong number of argument\n\n";
        errorMessage += "\nIn Linux/Mac : java -jar peter-bochs-debugger.jar bochs -f bochsrc.bxrc";
        errorMessage += "\nIn windows : java -jar peter-bochs-debugger.jar c:\\program files\\bochs2.4.3\\bochsdbg.exe -q -f bochsrc.bxrc";
        errorMessage += "\n!!! if using peter-bochs in windows, you need to pass the full path of bochs exe and -q to the parameter. (!!! relative path of bochs exe will not work)";
        errorMessage += "\n!!! to use \"experimental feature\", please add \"-debug\" to the parameter list";
        System.out.println(errorMessage);
        JOptionPane.showMessageDialog(null, errorMessage);
        System.exit(1);
    } else {
        if (args[0].equals("-version") || args[0].equals("-v")) {
            System.out.println(Global.version);
            System.exit(1);
        }
    }

    for (String str : args) {
        if (str.contains("bochsrc") || str.contains(".bxrc")) {
            bochsrc = str;
        }
    }

    String osName = System.getProperty("os.name").toLowerCase();
    if (osName.toLowerCase().contains("windows")) {
        os = OSType.win;
    } else if (osName.toLowerCase().contains("mac")) {
        os = OSType.mac;
    } else {
        os = OSType.linux;
    }
    if (os == OSType.mac) {
        com.apple.eawt.Application macApp = com.apple.eawt.Application.getApplication();
        // System.setProperty("dock:name", "Your Application Name");
        macApp.setDockIconImage(new ImageIcon(
                PeterBochsDebugger.class.getClassLoader().getResource("com/peterbochs/icons/peter.png"))
                        .getImage());
        // java.awt.PopupMenu menu = new java.awt.PopupMenu();
        // menu.add(new MenuItem("test"));
        // macApp.setDockMenu(menu);

        macApp.addApplicationListener(new MacAboutBoxHandler());
    }

    if (ArrayUtils.contains(args, "-debug")) {
        Global.debug = true;
        args = (String[]) ArrayUtils.removeElement(args, "-debug");
    } else {
        Global.debug = false;
    }

    try {
        if (PeterBochsDebugger.class.getProtectionDomain().getCodeSource().getLocation().getFile()
                .endsWith(".jar")) {
            JarFile jarFile = new JarFile(
                    PeterBochsDebugger.class.getProtectionDomain().getCodeSource().getLocation().getFile());
            if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                if (System.getProperty("os.arch").contains("64")) {
                    if (Global.debug) {
                        System.out.println("Loading linux 64 bits jogl");
                    }
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_amd64/libgluegen-rt.so")),
                            new File("libgluegen-rt.so"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_amd64/libjogl_awt.so")),
                            new File("libjogl_awt.so"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_amd64/libjogl_cg.so")),
                            new File("libjogl_cg.so"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_amd64/libjogl.so")),
                            new File("libjogl.so"));
                } else {
                    if (Global.debug) {
                        System.out.println("Loading linux 32 bits jogl");
                    }
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_i586/libgluegen-rt.so")),
                            new File("libgluegen-rt.so"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_i586/libjogl_awt.so")),
                            new File("libjogl_awt.so"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_i586/libjogl_cg.so")),
                            new File("libjogl_cg.so"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/linux_i586/libjogl.so")),
                            new File("libjogl.so"));
                }
                try {
                    File f = new File(".");
                    Runtime.getRuntime().load(f.getAbsolutePath() + File.separator + "libjogl.so");
                    System.out.println("Loading " + f.getAbsolutePath() + File.separator + "libjogl.so");
                    Runtime.getRuntime().load(f.getAbsolutePath() + File.separator + "libjogl_awt.so");
                    Runtime.getRuntime().load(f.getAbsolutePath() + File.separator + "libjogl_cg.so");
                    Runtime.getRuntime().load(f.getAbsolutePath() + File.separator + "libgluegen-rt.so");
                } catch (UnsatisfiedLinkError e) {
                    e.printStackTrace();
                    System.err.println("Native code library failed to load.\n" + e);
                    System.err.println(
                            "Solution : Please add \"-Djava.library.path=.\" to start peter-bochs\n" + e);
                }
            } else if (System.getProperty("os.name").toLowerCase().contains("windows")) {
                CommonLib.writeFile(jarFile.getInputStream(new JarEntry("com/peterbochs/exe/PauseBochs.exe")),
                        new File("PauseBochs.exe"));
                CommonLib.writeFile(jarFile.getInputStream(new JarEntry("com/peterbochs/exe/StopBochs.exe")),
                        new File("StopBochs.exe"));
                CommonLib.writeFile(jarFile.getInputStream(new JarEntry("com/peterbochs/exe/ndisasm.exe")),
                        new File("ndisasm.exe"));

                if (System.getProperty("os.arch").contains("64")) {
                    if (Global.debug) {
                        System.out.println("Loading windows 64 bits jogl");
                    }
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_amd64/jogl.dll")),
                            new File("jogl.dll"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_amd64/jogl_awt.dll")),
                            new File("jogl_awt.dll"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_amd64/jogl_cg.dll")),
                            new File("jogl_cg.dll"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_amd64/gluegen-rt.dll")),
                            new File("gluegen-rt.dll"));
                } else {
                    if (Global.debug) {
                        System.out.println("Loading windows 32 bits jogl");
                    }
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_i586/jogl.dll")),
                            new File("jogl.dll"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_i586/jogl_awt.dll")),
                            new File("jogl_awt.dll"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_i586/jogl_cg.dll")),
                            new File("jogl_cg.dll"));
                    CommonLib.writeFile(
                            jarFile.getInputStream(
                                    new JarEntry("com/peterbochs/jogl_dll/windows_i586/gluegen-rt.dll")),
                            new File("gluegen-rt.dll"));
                }
                try {
                    File f = new File(".");
                    System.load(f.getAbsolutePath() + File.separator + "jogl.dll");
                    System.load(f.getAbsolutePath() + File.separator + "jogl_awt.dll");
                    System.load(f.getAbsolutePath() + File.separator + "jogl_cg.dll");
                    System.load(f.getAbsolutePath() + File.separator + "gluegen-rt.dll");
                } catch (UnsatisfiedLinkError e) {
                    e.printStackTrace();
                    System.err.println("Native code library failed to load.\n" + e);
                    System.err.println(
                            "Solution : Please add \"-Djava.library.path=.\" to start peter-bochs\n" + e);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (ArrayUtils.contains(args, "-loadBreakpoint")) {
        Setting.getInstance().setLoadBreakpointAtStartup(true);
        args = (String[]) ArrayUtils.removeElement(args, "-loadBreakpoint");
    } else if (ArrayUtils.contains(args, "-loadbreakpoint")) {
        Setting.getInstance().setLoadBreakpointAtStartup(true);
        args = (String[]) ArrayUtils.removeElement(args, "-loadbreakpoint");
    }

    for (int x = 0; x < args.length; x++) {
        if (args[x].toLowerCase().startsWith("-osdebug")) {
            Global.osDebug = CommonLib.string2long(args[x].replaceAll("-.*=", ""));
            args = (String[]) ArrayUtils.removeElement(args, args[x]);
            x = -1;
        } else if (args[x].toLowerCase().startsWith("-profilingmemoryport")) {
            Global.profilingMemoryPort = (int) CommonLib.string2long(args[x].replaceAll("-.*=", ""));
            args = (String[]) ArrayUtils.removeElement(args, args[x]);
            x = -1;
        } else if (args[x].toLowerCase().startsWith("-profilingjmpport")) {
            Global.profilingJmpPort = (int) CommonLib.string2long(args[x].replaceAll("-.*=", ""));
            args = (String[]) ArrayUtils.removeElement(args, args[x]);
            x = -1;
        } else if (args[x].toLowerCase().startsWith("-loadelf")) {
            Global.elfPaths = args[x].replaceAll("-loadelf=", "").split(",");
            Setting.getInstance().setLoadSystemMapAtStartup(true);
            args = (String[]) ArrayUtils.removeElement(args, args[x]);
            x = -1;
        } else if (args[x].toLowerCase().startsWith("-loadmap")) {
            System.out.println("-loadmap is not deprecated, please use -loadelf.");
        }
    }

    arguments = args;

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            PeterBochsDebugger inst = new PeterBochsDebugger();
            PeterBochsDebugger.instance = inst;

            new Thread("preventSetVisibleHang thread") {
                public void run() {
                    try {
                        Thread.sleep(10000);
                        if (preventSetVisibleHang) {
                            System.out.println(
                                    "setVisible(true) cause system hang, this probably a swing bug, so force exit, please restart");
                            System.exit(-1);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();

            if (Global.debug) {
                System.out.println("setVisible(true)");
            }
            inst.setVisible(true);

            preventSetVisibleHang = false;
            if (Global.debug) {
                System.out.println("end setVisible(true)");
            }
        }
    });
}

From source file:org.superfuntime.chatty.Core.java

@SuppressWarnings("unchecked")
private static void loadBots() {
    logger.info("Loading available bots");
    final Collection<File> botJars = FileUtils.listFiles(botsDirecotry, new String[] { "jar" }, false);
    for (File botJar : botJars) {
        try {// w w  w.j a  v a2s.com
            JarFile jar = new JarFile(botJar);
            InputStream is = jar.getInputStream(jar.getEntry("bot.yml"));
            YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true);
            String mainClass = n.getString("mainClass");
            String type = n.getString("type");
            URLClassLoader loader = new URLClassLoader(new URL[] { botJar.toURI().toURL() },
                    Core.class.getClassLoader());
            BOT_INFO.put(type, new BotClassInfo(loader.loadClass(mainClass).asSubclass(Bot.class), n));
            logger.info("Loaded bot '{}'", type);
        } catch (Exception e) {
            logger.error("Failed to load bot from {}", botJar.getName());
            e.printStackTrace();
        }
    }
    logger.info("Loaded {} bot(s)", BOT_INFO.size());
}

From source file:org.superfuntime.chatty.Core.java

@SuppressWarnings("unchecked")
private static void startPlugins() {
    logger.info("Loading available plugins");
    final Collection<File> botJars = FileUtils.listFiles(pluginsDirecotry, new String[] { "jar" }, false);
    for (File botJar : botJars) {
        try {/*from   w w  w .  j av a 2 s . c  o  m*/
            JarFile jar = new JarFile(botJar);
            InputStream is = jar.getInputStream(jar.getEntry("plugin.yml"));
            YAMLNode n = new YAMLNode(new Yaml().loadAs(is, Map.class), true);
            String mainClass = n.getString("mainClass");
            String name = n.getString("name");
            URLClassLoader loader = new URLClassLoader(new URL[] { botJar.toURI().toURL() },
                    Core.class.getClassLoader());
            Plugin plugin = loader.loadClass(mainClass).asSubclass(Plugin.class).newInstance();
            plugin.start();
            PLUGIN_INFO.put(name, new PluginInfo(name, plugin, n));
            logger.info("Loaded plugin '{}'", name);
        } catch (Exception e) {
            logger.error("Failed to load plugin from {}", botJar.getName());
            e.printStackTrace();
        }
    }
    logger.info("Loaded {} plugin(s)", PLUGIN_INFO.size());
}

From source file:org.mousephenotype.dcc.utils.io.jar.JARUtils.java

public static InputStream getInputStreamFromJar(URL jarURL, String entryName) throws IOException {
    JarFile jarfile = new JarFile(jarURL.getFile());
    ZipEntry entry = jarfile.getEntry(entryName);
    return jarfile.getInputStream(entry);
}

From source file:tk.tomby.tedit.plugins.PluginDriver.java

public static IPlugin createPluginFromJAR(File file) {
    IPlugin plugin = null;//from   w  w  w.  j  a v a2s.c  o m

    try {
        JarFile jar = new JarFile(file);
        JarEntry entry = getDescriptor(jar);
        InputStream input = jar.getInputStream(entry);

        ClassLoader loader = PluginDriver.class.getClassLoader();
        URL rules = loader.getResource(RESOURCE_ROOT + "/plugins-rules.xml");

        if (input != null) {
            Digester digester = DigesterLoader.createDigester(rules);
            IPluginDescriptor descriptor = (IPluginDescriptor) digester.parse(input);
            descriptor.setPluginPath(file.getPath());
            plugin = PluginManager.createPlugin(descriptor);
        }
    } catch (SAXException e) {
        log.error("error in plugin creation", e);
    } catch (IOException e) {
        log.error("error in plugin creation", e);
    }

    return plugin;
}

From source file:net.minecraftforge.fml.common.patcher.GenDiffSet.java

private static byte[] toByteArray(JarFile sourceZip, @Nullable JarEntry entry) throws IOException {
    if (entry == null) {
        return new byte[0];
    }/*from w w  w  .ja va  2 s .c  o  m*/

    InputStream sourceZipInputStream = sourceZip.getInputStream(entry);
    try {
        return ByteStreams.toByteArray(sourceZipInputStream);
    } finally {
        IOUtils.closeQuietly(sourceZipInputStream);
    }
}

From source file:com.thoughtworks.go.agent.common.util.JarUtil.java

private static File extractJarEntry(JarFile jarFile, JarEntry jarEntry, File targetFile) {
    LOG.debug("Extracting {}!/{} -> {}", jarFile, jarEntry, targetFile);
    try (InputStream inputStream = jarFile.getInputStream(jarEntry)) {
        FileUtils.forceMkdirParent(targetFile);
        Files.copy(inputStream, targetFile.toPath());
        return targetFile;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }/*from  www  .j  a  v  a 2  s. c o  m*/
}

From source file:com.athomas.androidkickstartr.util.ResourcesUtils.java

private static void copyResourcesToFromJar(File target, URL url) throws IOException {
    JarURLConnection connection = (JarURLConnection) url.openConnection();
    JarFile jarFile = connection.getJarFile();

    Enumeration<JarEntry> entries = jarFile.entries();

    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        InputStream is = jarFile.getInputStream(jarEntry);
        String entryPath = jarEntry.getName();

        File file = null;/*from www  .  ja  va2 s.  c  o  m*/
        String dirs = "";
        if (entryPath.contains("/")) {
            int lastIndexOf = entryPath.lastIndexOf("/");
            dirs = (String) entryPath.subSequence(0, lastIndexOf + 1);
        }

        File parent = new File(target, dirs);
        parent.mkdirs();

        if (!jarEntry.isDirectory()) {
            String[] splitedPath = entryPath.split("/");
            String fileName = splitedPath[splitedPath.length - 1];
            file = new File(parent, fileName);
            FileUtils.copyInputStreamToFile(is, file);
        }
    }
}