Jar file Extraction in different directory...

Hi friends..
i want to extract a jar file using a java program..but not in current directory..i want it to do in another directory..
i tried it using exec(jarfilename,env var,filedirectory) ..but it is not working..help me..
thanks..
Krishna

Hi Krishna,
The following code will definitely help you
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
* @author Snehanshu
public class Main {
     * Extracts a zip file
    public void extractZipFile() {
        try {
            String zipFileName = "C:\\DEMO.jar";
            String extractedFileName = "C:\\extracted.txt";
            //Create input and output streams
            ZipInputStream inStream = new ZipInputStream(new FileInputStream(zipFileName));
            OutputStream outStream = new FileOutputStream(extractedFileName);
            ZipEntry entry;
            byte[] buffer = new byte[1024];
            int nrBytesRead;
            //Get next zip entry and start reading data
            if ((entry = inStream.getNextEntry()) != null) {
                while ((nrBytesRead = inStream.read(buffer)) > 0) {
                    outStream.write(buffer, 0, nrBytesRead);
            //Finish off by closing the streams
            outStream.close();
            inStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
     * @param args the command line arguments
    public static void main(String[] args) {
        new Main().extractZipFile();
}Change the "extractedFileName" as you want.
:)

Similar Messages

  • Extract JAR file to a different directory

    Is it possible to use the jar command to extract the content of a JAR file to a different directory (not the current directory)?
    I want to use this command inside a makefile, looking for the same effect as "unzip example.jar -d directory".
    Thanks in advanced.

    Thanks Chuck, you solved my doubt... It surprises me that JAR doesn't support this option.
    I used the "unzip" command inside the makefile instead, but I wanted to be sure that there wasn't any chance to use "jar" (without using the additional script file).

  • Jar File caching in temp directory problem

    Hello,
    I have a problem where the java plugin is caching jar_cache9999.jar (where 9999 is any number 0-9) files in the temp directory. I have tried following the directions for disk caching in the documentation, but these files are always created, never destroyed.
    I was wondering:
    1. Is there a simple setting that I could set that should resolve this problem? (un-setting "cache jars in memory" wouldn't seem to solve the problem as I would think that would only force the browser to use the same jar if it had already been loaded)
    2. Has this problem been corrected in later releases of the java plugin? I am using the jre1.2.2. When I looked at the problem here in the forums, the bug that was listed was listed as fixed.
    Thanks,
    Dan Hughes

    Hi,
    Java Plug-in has supported disk caching in previous versions by using the same cache the browser uses for all other web documents. This works for casual applet usage, but larger applets can often get flushed from the cache to make room for other documents since the browser has no knowledge that an applet file might be needed in the future. The result is that this caching strategy fails where is needed most in large business applets.
    From 1.3. the new feature is activated by including the new "cache_option" , "cache_archive" and "cache_version" values in the EMBED/OBJECT tag that specifies the use of Java Plug-in as below:
    <OBJECT ....>
         <PARAM NAME="archive" VALUE="...">
         <PARAM NAME="cache_option" VALUE="...">
         <PARAM NAME="cache_archive" VALUE="...">
         <PARAM NAME="cache_version" VALUE="...">
    </OBJECT>
    or
    <EMBED ....
         archive="..."
         cache_option="..."
         cache_archive="..."
         cache_version="...">
    For detail description please go through the following URL:
    http://java.sun.com/products/plugin/1.3/docs/appletcaching.html
    Hope this will help you.
    Anil.
    Developer Technical Support
    Sun Microsystems,
    http://www.sun.com/developers/products

  • Publish a file to a different directory.

    I've seen other questions similar to mine on the forums but haven't found an answer.  My site has a newsletter archive that links to all of our past newslettters.  The newsletters are just html files saved from emails we send out.
    To keep things organized, the newsletters are grouped by year, so all 2010 newsletters are saved in the 2010 directory.  However, when I add the newsletter using the Link to File on My Computer, it adds the newsletter html file to the current directory.
    Is there any way, without using an outside FTP program, to upload the newsletter to the 2010 directory and then link to it from the main newsletter archive page?  I can't use the administer->file placement option because the rule  only applies to this specific upload. This seems like it should be an obvious, simple thing, but I'm stumped.

    Hi,
         As of now I do not think this is possible in Contribute. But there can be a work-around.Can you try the following steps:
    When you edit a web page, add the links to the files in your computer.
    Do Publish As New Page (File -> Actions -> Publish As New Page), and select the Folder in which you want to publish your pages to, by clicking the Choose Folder button.
    The web page and all the files from your computer will be published to that folder.
    Now, re-edit the published web page with the links, and Publish As New Page to the original folder in which the web page was present.
    This is a long workaround, where you will be publishing the web page twice. Hope this helps.

  • How can I redirect Batch Report Files to a different directory.

    I have a custom version of the Batch Process Model that allows for on the fly report updates. At the end of execution the final report is written. I need to redirect this final report to another directory, but I am having trouble identifying where the report directory is specified. Right now, it is writing in the same directory as the sequence file and I need to change this.

    Hi Terry,
    have a look at the following inside the process model TestUUTs (or Single Pass)
    Locals.ModelData.ReportOptions.DirectoryType
    set this to "SpecificDirectory"
    then in
    Locals.ModelData.ReportOptions.Directory
    set this to your target directory.
    If it's a permanent setting, then I'd suggest updating these values before run time in the report options panel from the Configure menu option.
    If it can be determined before the sequence is run, then you could do it as part of the report options callback. Otherwise, slot it in as and when needed, just before the call to the Determine Report File Path and Determine UUT Report File Path steps, although I guess that depends on how much you've modified the process model so far.
    Hope that helps
    S.
    // it takes almost no time to rate an answer

  • File name in different directory

    i have a problem when saving files:
    I copied all the files from one directory to another ---with the same file name and I tried to make chnages in the new directory, But I found that all the changes I made in the new directory also happened in the old directory. So Labview recogonize file names only, not file paths. I don't like this feature!!!!!

    This is an old problem with no easy solution as of now. For a better description of what is going on here, you can read this discussion where I answered a similar question a while ago.
    Ed
    Ed Dickens - Certified LabVIEW Architect - DISTek Integration, Inc. - NI Certified Alliance Partner
    Using the Abort button to stop your VI is like using a tree to stop your car. It works, but there may be consequences.

  • Parsing XML with DTD residing in jar file

    Hi,
    I have problems using crimson parser for my program under JDK 1.4.0b2. It attempts to parse an xml file with SAX. The corresponding lies in a jar file in a different directory but reachable through the classpath. All I get is an exception.
    org.xml.sax.SAXParseException: Relative URI "my.dtd"; kann nicht ohne eine Dokument-URI aufgel�st werden.
    at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3121)
    at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3115)
    at org.apache.crimson.parser.Parser2.resolveURI(Parser2.java:2702)
    at org.apache.crimson.parser.Parser2.maybeExternalID(Parser2.java:2674)
    at org.apache.crimson.parser.Parser2.maybeDoctypeDecl(Parser2.java:1125)
    at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:489)
    at org.apache.crimson.parser.Parser2.parse(Parser2.java:305)
    at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:433)
    at org.xml.sax.helpers.XMLReaderAdapter.parse(XMLReaderAdapter.java:223)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:326)
    I used Xerces before and it worked fine. I already searched the community for that problem. All hints I found assume that xml file and dtd are in the same directory. Setting the systemId of the input source doesn't fix the problem.
    Is there anyone out there knowing what to do?
    Thanks,
    Thorsten

    Use a Resolver to map a PUBLIC name to a local name:
    <!DOCTYPE DOC PUBLIC "-//gaskin.de//XMLDOC 1.0//EN"
    "http://www.gaskin.de/dtd/xmldoc.dtd">
    public static register() {
       ClassLoader loader = Resolver.class.getClassLoader();
       registerCatalogEntry(
          "-//gaskin.de//XMLDOC 1.0//EN",
          "de/gaskin/resources/dtd/XMLDOC.DTD",
          loader);
    }

  • Download and extract jar file from applet

    Can any one provide me a solution for my below problem. Thanks in advance for all the suggestions.
    I want my applet to download a jar file, extract into users home directory by creating a folder and then set the created folder to java library path.
    My applet html file is in "root" directory. The applet is included into applet.jar and placed under "lib" directory which is under "root" directory.
    Now i also had another jar file(contains dlls what I packed) under the same lib folder.
    The below is the following directory structure in my server.
    root ---
         |
         |
         default.html
         lib--
         |
         |
         applet.jar - contains applet related classes
         native.jar - contains dlls which are to be loaded
    When a client downloads my applet, it should copy the native.jar and then extract into a new folder under user home directory. Then set this folder to java library path.
    Regards
    raman kumar

    I managed to do it this way, Just remember that you need to sign the jar files for your applet, else it won't work.
    package jar.extract;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.jar.JarFile;
    import java.util.zip.ZipEntry;
    public class JarExtractor {
         // Dll file in the jar
         private static String JAR_FILE = "dll.jar";
         public void extract() {
              try {
                   JarFile jf = new JarFile(JAR_FILE);
                   // Entry in the jar file rember package and dll file
                   ZipEntry ze = new ZipEntry("package/dll_file");      
                   int length = (int) ze.getSize();
                   byte fileContent[] = new byte[length];
                   InputStream fin = jf.getInputStream(ze);
                   fin.read(fileContent, 0, length);
                   // CODE missing.
                   // You need to write the input stream (fin) to a new files output stream
                   // it is like copying a file !!!
              } catch (IOException e) {
                   e.printStackTrace();
    }Tom

  • Extract nested Jar File

    Okay, I've been looking all day for solutions to this problem and nothing I've found pinpointed the solution, so if anyone would rather point me to another site, that's great.
    I have a .jar installer ( installer.jar ) that contains the installer code and a nested jar file ( program.jar ).
    In my experience, plain text files work great in the "getInputStream() -> FileWriter.write()" method, but my way of handling images requires javax.imageio.ImageIO, and i'm hoping something similar exists for jars.
    ALL+* i want to do is extract that jar file into the current directory, but the ZipEntry.getInputStream() method didn't work, and JarInputStream just confuses me.
    If anyone has links to other topics or source code to point to how a jarfile can be extracted.

    Thanks for the help.
    After looking around and finally noticing the Pack200 package, I have the answer!
    JarFile jf = new JarFile( "outside.jar" );
    JarOutputStream jos = new JarOutputStream( new FileOutputStream( new File( "inside.jar" ) ) );
    Pack200.newUnpacker().unpack( jar.getInputStream( jar.getEntry( "inside.jar" ) ), jos );
    jos.close();This creates a JarOutputStream that will write to the FileOutputStream forever until closed.
    The Pack200 Unpacker takes the inputstream and outputs it to the JarOutputStream.
    When the unpack method returns, the jar file is ready. Close the jar output stream and you have a workable jar!

  • Servlets in Jar files  in WLS 6.1 ?

    Trying to port a WLS 5.1 servlet into 6.1 (an entirely different
              animal it appears)
              I have added an application to my config.xml file as below as well as
              trying to start it
              PasswordPolicy="wl_default_password_policy"
              Realm="wl_default_realm"/>
              <Application Deployed="true" Name="DefaultWebApp_cb2java"
              Path=".\config\PM\applications">
              <WebAppComponent Name="DefaultWebApp_cb2java"
              Targets="PCPNEARN"
              URI="DefaultWebApp_cb2java" WebServers="PCPNEARN"/>
              </Application>
              <StartupClass Arguments="servlet=ProMan"
              ClassName="weblogic.servlet.utils.ServletStartup"
              FailureIsFatal="true" Name="StartPMServlet1"
              Targets="PCPNEARN"/>
              Now my servlet is packaged in a JAR file under the lib directory of so
              when I assume that WLS will automatically pick this up from
              C:\Weblogic6.1\wlserver6.1\config\PM\applications\DefaultWebApp_cb2java\WEB-INF\lib
              directory ? ( as the lib directory is where the doc says it picks up
              jar files from )
              Now when I start WLS it cant find the servlet? Is that because I cant
              package it in a JAR as of WLS 6.1 and have to move the class files to
              the Classes directory instead ?
              Furthermore the Servlet itself references JavaBeans deployed in a
              totally different application. Can I also assume that if I have my
              Classpath correct these will get picked up ?
              

    I would look here
    http://e-docs.bea.com/wls/docs61/webapp/security.html#100365 for methods on
    protecting web app resources.
    "zhen Ni" <[email protected]> wrote in message
    news:3c86d70c$[email protected]..
    Hello:
    I have some derectories and files on my web site,and only appointed usersstored in my database can access their special directories.how can i do?
    thank u!

  • Accessing web application JAR files from applet

    I've got an applet which is part of a web application.
    All the web application JARs are in the webapps/myappl/WEB-INF/lib directory, and I
    can't seem to place anything in that directory on the archive attribute of <OBJECT> tag.
    I'm running into trouble because I'm sending objects from the servlet to the applet and keep getting classnot found during deserialization because I've missed some jar file on the archive attribute. I've had to copy lots of jar files up to where the codebase in the HTML file is to make this run. My question is this.
    Is it better to just change the tomcat security policy file (catalina.policy in tomcat conf directory) so that the JARs are directly accessible and I don't have to copy them from WEB-INF/lib???? Has anyone done this before? What would the line look like?
    I'm assuming that I have to follow the instructions (THAT I DON'T UNDERSTAND)
    at the bottom of the policy file. Can someone help me out here????
    should I have something like
    grant codeBase "file:${catalina.home}/webapps/myappl/WEB-INF/classes/-"
    { permission java.security.AllPermission; };
    grant codeBase "file:${catalina.home}/webapps/myappl/WEB-INF/lib/-"
    { permission java.security.AllPermission; };
    Will this allow me to place references to jar files (and the classes directory) in the archive attribute, such as
    <OBJECT .....  >
        <param name="archive" value="../WEB-INF/lib/applet.jar,../WEB-INF/lib/someapp.jar,../WEB-INF/classes" />trailer for catalina.policy is
    // You can assign additional permissions to particular web applications by
    // adding additional "grant" entries here, based on the code base for that
    // application, /WEB-INF/classes/, or /WEB-INF/lib/ jar files.
    // Different permissions can be granted to JSP pages, classes loaded from
    // the /WEB-INF/classes/ directory, all jar files in the /WEB-INF/lib/
    // directory, or even to individual jar files in the /WEB-INF/lib/ directory.
    // For instance, assume that the standard "examples" application
    // included a JDBC driver that needed to establish a network connection to the
    // corresponding database and used the scrape taglib to get the weather from
    // the NOAA web server.  You might create a "grant" entries like this:
    // The permissions granted to the context root directory apply to JSP pages.
    // grant codeBase "file:${catalina.home}/webapps/examples/-" {
    //   permission java.net.SocketPermission "dbhost.mycompany.com:5432", "connect";
    //   permission java.net.SocketPermission "*.noaa.gov:80", "connect";
    // The permissions granted to the context WEB-INF/classes directory
    // grant codeBase "file:${catalina.home}/webapps/examples/WEB-INF/classes/-" {
    // The permission granted to your JDBC driver
    // grant codeBase "file:${catalina.home}/webapps/examples/WEB-INF/lib/driver.jar" {
    //   permission java.net.SocketPermission "dbhost.mycompany.com:5432", "connect";
    // The permission granted to the scrape taglib
    // grant codeBase "file:${catalina.home}/webapps/examples/WEB-INF/lib/scrape.jar" {
    //   permission java.net.SocketPermission "*.noaa.gov:80", "connect";
    // };

    Thank you for your reply.
    As I mentioned in my first post I had seen a couple of posts mentioning ServletContext before, but dismissed them as I am not using any servlets of my own. However, since you showed me exactly how it was used I realised I could probably just use a dummy servlet to get this information.
    The following page suggests a couple of techniques for doing just this.
    http://www.jguru.com/forums/view.jsp?EID=1087411
    So I think that's me sorted, cheers.

  • Question about classpath and jar file

    I have a jar file (actually jhall.jar, the java help jar file). How do I uses its classes if I don't want to add it to the classpath. Currently, I have the jar file in the same directory as all my other classes.

    it simply adds secondjar.jar into firstjar.jar, rather
    than extracting secondjar.jar's files and put it into
    firstjar.jar. How would I be able to do that?Don't do either of them. Distribute the two jar files separately, and in the manifest of the one which is meant to be your executable jar file, put a Class-Path: entry that refers to the other one. Look at the latter parts of the tutorial about jar files:
    http://java.sun.com/docs/books/tutorial/jar/basics/
    PC&#178;

  • Best practices for managing jar files

    just wondering what anyone's thoughts are on managing jar files. I have read that instead of adding the jar files to your CLASSPATH environment variable, you can just extend the jdk. This is accomplished by inserting the jar files into the C:\jdk1.3.1_01\jre\lib\ext directory. I suspect that if the latter option is truly feasible, it would be preferred to adding entries to the CLASSPATH variable for every jar file you want to use. You could end up with hundreds of entries into the CLASSPATH variable if you have that many jar files in use.
    Any thoughts, comments, or links to where I can find any info on this?...
    thanks for any information.....

    In my company, we use scripts (in UNIX system) to handle this problem. The following scripts can iterate jar files in a given directory:
    for i in ${YOUR_LIB_DIR}/*.jar
    do
    YOUR_CLASSPATH=${YOUR_CLASSPATH}:$i
    done
    With this simple script, you can use any directory in the same way as the ext\ direcotry (just copy the jar file in it), and you still have the flexibility of maintaining jar files in different directories, say a number of projects.
    but i don't know whether .bat/.cmd scripts for windows have the same function.

  • JAR file with 3rd party package

    Hello.
    I am sorry if similar question has been asked many times. But I really have no idea to make the JAR work...
    I have downloaded dom4j package into my Windows XP machine. And put the file to my C:\j2sdk1.4.0\jre\lib\ext\. Then I develop my application in other directory. Is there a way to edit the manifest file such that the JAR file can contain all the classes including classes from dom4j, such that I can run the application in other machine that without dom4j's jar file in the ext directory?? Thank you very much.

    why not?
    1. extract dom4j.jar
    2 add all extracted files to your own .jar file..
    everything will be ok.
    The following may help you..
    JavaJar(written by java) can compress and decompress jar,war, ear and.zip...
    it can deal with manifest file and executable .jar file .specially.
    may download from http://www.qwerks.com/download/4114/JavaJar.zip
    the tool is very verygood tool.
    but the homepage(www.pivotonic.com) of JavaJar cannot be accessed.
    it is funny.
    good lucky.

  • Problem with executable jar file

    Hi,
    I wrote a simple little xml based app in eclipse, it had xalan and xerces in build path. I created an executuable jar that ran fine on my machine; I was able to move it to different directories and launch it successfully, but when I put the jar file on a network directory and asked a coworker to try running it, they got a message that said it couldn't find the main class.
    how do you deal with that?
    thanks,
    bp

    they got a message that said it couldn't find the main class.
    Are you sure?
    If you are sure then none of the other responses will help.
    That error suggests one of the following
    1. The jar you moved is not the jar that you are running.
    2. Your co-workers did not execute it correctly.
    3. You do not have an executable jar (and you are not testing it correctly on your box.)
    How exectly did they run it?
    The standard double click will give an error about "Main-Class" with that specific text when you attempt to run a jar that is not in fact an executable jar.
    That error is NOT the same as the standard class not found error.

Maybe you are looking for

  • Preview icons not appearing

    ust recently, the previews that show up for pictures and videos stopped appearing. For example, if I had a group of photos and I waited for a couple of seconds while looking at them, small thumbnails of the photos would replace the generic "preview"

  • AC Adapter Compatibility

    Hi, I am not familiar with the intricacies of batteries and charging and whatnot so I was wondering if my 135W charger I normally use for my W510 will work with my wife's T61? Her original AC adapter is doing this thing where it beeps when you plug i

  • Update infotype

    I need to update infotype 22, 31 , 68. I am using function module "HR_INFOTYPE_OPERATION". But using this FM i can only upate the fields of infotype which are reflected in the infotype screen. I need to update other fields which are there in the PA t

  • Delete from 95 million rows table ...

    Hi folks, need to delete from a 95 millions rows regular table, what should be my best options, have tried CTAS using parallel, but it failed after 1+ hrs ... it was due to bad query, but checking is there any other way to achieve this. Thanks in adv

  • Copy, paste and crash in Keynote. I've seen a lot of postings with the same problem. Is there a solution or fix? I'm using keynote 9 v5.1.1

    I've seen a lot of postings about keynote crashing when doing simple copy and paste. I have the same problem, Keynote 9 v5.1.1 Is there a fix?