Extracting and updating files inside a JAR file... from my code

Hi.
I need some help in the following:
from my code, I need to search a JAR file, open it, unzipped it, then I need to search a xml and property files inside the JAR, after that I need to modify the files and finally, my code has to save the changes and update the JAR file. All that must be from my java code.
How can I do that? I am trying to use Runtime.getRunTime().exec().... but I am not sure that it works. Besides, I don�t know what command to include in the exec() method. Is there any way to do that?
Thanks

The normal reason I see for this requirement is that people want to update a properties or data file. The approach I use it that I first look for the data file in a well defined location and if I find it I use it and update it as required. If I don't fine it I create it base on a template file read from the jar file using getResourceAsSrream().
In this way I never have to update the jar file.

Similar Messages

  • Include and call an .exe file inside my jar file

    Hi.
    I want to include an exe file and call it inside my jar file. this is the code.
    InputStream is = getClass().getResourceAsStream("/native/my.exe");
              int[] line = new int[is.available()];
              File myFile = File.createTempFile("my","exe");
              myFile.deleteOnExit();
              myFile.setExecutable(true);
              FileOutputStream out = new FileOutputStream(myFile.getAbsolutePath());
              int i = 0;
              int c;
              while((c=is.read()) != -1){
                   line=c;
                   i++;
              is.close();
              for(int j= 0;j<line.length;j++)
                   out.write(line[j]);
              out.close();
    So i store it in my temp folder. Than i run
    String myFileLocation = myFile.getAbsolutePath();
    p = rt.exec(myFileLocation);
    Everything goes fine, but the behaviour of the .exe file is not the same. I mean when i run the commands
    PrintWriter writer = new PrintWriter(p.getoutputStream());
    writer.println("command");
    the behaviour is not the same. What goes wrong.

    But to load the xml file I need to create a File objectIf this is true then you will not be able to load the XML file, since a jar entry is not a File object and cannot be represented as one.
    However if you get rid of whatever it is that loads your XML file and replace it by more standard JAXP processing, you should be able to parse an InputStream that reads from the jar entry. You know how to get that so it should not be a problem.

  • Java Files inside the jar file cannot be read or accessed through Eclpse

    The Java File inside the jar file of the eclipse cannot be accessed through the Eclipse . It shows error for the modules in the jar file .
    But when compiled with adding the jar files to the class path the compilation is successful .
    How can i browse through the files in the jar like going into the function definition .
    TIA ,
    Imran

    Open MPlayer OSX and show the playlist (command-shift-p) then drag the file into the playlist. Click the file in the playlist and click the "i"nfo button. What does it list for file, video and audio format?
    Not all of the codecs used in avi and wmv files are available for OS X so I'm guessing your file happens to be using one that isn't...

  • I am having Html help files inside my Jar file

    Dear Friends;
    i am having Html help files inside my Jar file.
    how will open the html file using Java Help.
    Thank u

    http://java.sun.com/products/javahelp/reference/docs/index.html

  • How to read, write file inside the JAR file?

    Hi all,
    I want to read the file inside the jar file, use following method:
    File file = new File("filename");
    It works if not in JAR file, but it doesn't work if it's in a JAR file.
    I found someone had the same problem, but no reply.
    http://forum.java.sun.com/thread.jsp?forum=22&thread=180618
    Can you help me ? I have tried this for all night !
    Thanks in advance
    Leo

    If you want to read a file from the JAR file that the
    application is packaged in (rather than a separate
    external JAR file) you do it like this ...
    InputStream is =
    ClassLoader.getSystemResourceAsStream("filename");Better to use
    this.getClass().getClassLoader().getResourceAsStream();
    From a class near to where the data is. This deals with multiple classloaders properly.

  • How to read a file inside the JAR file

    Hi All,
    I want to save some preferences in a file called "preferences". I kept the preferences file under my java package.
    If i am running the code inside the NetBeans 6.9 IDE it's working file. But once i have created a JAR and try to run the application, it couldn't find the path.
    Please help me that how to resolve this issue. I don't want to save this preferences file outside of my JAR (i.e) within my java package.
    Here is the code,
    package mypackage;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    public class ReadFile
        private void writeFile()
            try
                OutputStream out = new FileOutputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
                PrintWriter writer = new PrintWriter(out);
                writer.println("Hello Java!");
                writer.close();
                out.close();
            catch (Exception ex)
                System.out.println(ex.getMessage());
        private void readFile()
            try
                FileInputStream fi = new FileInputStream(getClass().getClassLoader().getResource("mypackage/preferences").getPath().replace("%20", " "));
                BufferedReader br = new BufferedReader(new InputStreamReader(fi));
                System.out.println(br.readLine().trim());
                br.close();
                fi.close();
            catch (Exception ex)
                System.out.println(ex.getMessage());
        public static void main(String[] args)
            ReadFile read = new ReadFile();
            read.writeFile();
            read.readFile();
    }If i run the JAR, i get the following error message,
    {color:#ff0000}*file:\my jar path\jarname.jar!\mypackage\preferences (The filename, directoryname, or volume label syntax is incorrect)*{color}

    Thanks sabre150,
    sabre150 wrote:
    You cannot update a running jar file. I am sorry. I didn't know about it. Thanks to point out.
    There are two ways I approach this depending on my exact requirements -
    1) use the java.util.prefs.Preferences APII will try this one
    >
    2) if not already present I copy the preferences file from the jar to a known place. I use a directory in the user's home directory and normally make the directory name the program with a '.' prefix.
    One cannot access files in a jar file using the File API. One needs to use the getResource() or getResourceAsStream() methods on Class or ClassLoader.

  • Updating an XML file inside the Jar executable

    Hello all!
    I would like to ask how can i update an XML file that is packaged inside my jar file?
    For intstance:
    I have a Java Application in NetBeans
    Inside a package i have constructed a custom .xml file.
    I can read from this file using
    .....getClass().getResourceAsStream("/.../file.xml")and after that using XPath for querying and works fine.
    I want to update an entry in my xml file so the jar contains now the newly updated file.
    Thanks everyone who spends his time to read this. Hope someone can help me.

    Even if it were possible, it would be a bad idea. A jar file is something you deploy. It can be really hard to manage ongoing maintenance if the thing you deploy changes after you've deployed it.
    Just create a file, in an appropriate place given the user's OS, to hold data that changes after the jar has been deployed. Your application will still be in a single jar; it's just that the application will happen to create additional content outside the jar.

  • Running a batch file inside a jar

    HI all
    I created a jar file , in which i need to run a batch file , so i wrote a main class to execute the run the batch file , but when iam running the jar it is searching for the given batch file out side the jar.
    my code is some thing like below
         public static void main(String args[])
              try
                   Runtime rt=Runtime.getRuntime();
                   Process process=rt.exec("%DIRNAME%/start.bat");
       InputStreamReader reader =
                      new InputStreamReader ( process.getInputStream () );
                   BufferedReader buf_reader =
                       new BufferedReader (  reader );
                    String line;
                    while ((line = buf_reader.readLine ()) != null)
                                 System.out.println (line);
              catch (IOException e) {
                    System.out.println (e);
         }so please help me.. is it the problem in specifing the path...?
    are does it have any other method to read the stream functions ..
    thanks in advance
    sam

    I don't think this will work. You could not go to a command line and enter, "%DIRNAME%/start.bat" if start.bat was inside a jar - the CLI doesn't know how to read a batch file inside a jar.
    I think it would work to programmatically read the lines of start.bat (when it is inside the jar) into an array of Strings and use Runtime.exec with the array as the argument.
    It would also work to extract the start.bat file, then execute it using Runtime.exec.

  • A .dll inside a .jar file

    Hello,
    I want to place a .dll inside a .jar file ? It's possible, What is the library.path setting: the jar file or the path where the dll is put is in the jar file; and the argument of the System.loadLibrary?
    Thank for all

    Hello,
    I want to place a .dll inside a .jar file ? It's
    possible,Of course, you can put any type of file you want into a jar file.
    What is the library.path setting: the jar
    file or the path where the dll is put is in the jar
    file; and the argument of the System.loadLibrary?You cannot do a System.load() or System.loadLibrary() on something in a jar file.
    You have to first extract the dll to the filesystem. Then if you put it in the library path you can use System.loadLibrary(). But I think it is easier to use System.load() as you know the complete path to where you put the dll and can just supply that to System.load().

  • Changing values in property file inside a jar in an ear

    Hi,
    I have an application that contains EJB. The environment values needed for initial context of the ejb is kept in a .properties file. I have created an ear of the application. Inside the ear there is a jar which contains the .properties file. I need to deploy the ear to different environments. However before deploying I need some kind of script to change the values in the .properties file inside the jar in the ear.
    Can anyone provide some help/pointers about how can I achieve it (Using ANT/WLST)

    WLST does not have any capability to update a JAR.
    WLS 10.3 (still in tech preview) has the ability to override properties
    files inside the jar with an external properties file. This would allow you
    to deploy the same ear to different environments, and then only deploy a
    different properties file in the plan directory.
    <Rishi Shah> wrote in message news:[email protected]..
    Hi,
    I have an application that contains EJB. The environment values needed for
    initial context of the ejb is kept in a .properties file. I have created an
    ear of the application. Inside the ear there is a jar which contains the
    .properties file. I need to deploy the ear to different environments.
    However before deploying I need some kind of script to change the values in
    the .properties file inside the jar in the ear.
    Can anyone provide some help/pointers about how can I achieve it (Using
    ANT/WLST)

  • Call class inside a jar file??

    Hello group,
    I have a class with a main mathod and this class is inside a .jar file. This jar file reside under my
    web-inf\lib or class-path directory in my application at Weblogic Server.
    How can I call this class by command line?
    I have to create a cliente to do it?
    Is it possible? I´m having problems because the weblogic server is using the jar file at the same time that I trying use it calling by command line.
    Where can I find more information?
    Message was edited by:
    Denimar

    Authcloud9981 wrote:
    Hi I have an executible JAR file that I can call from command line by using the command - "java -jar file.jar".
    Is there a way for me to call this JAR file inside a Java class in another Java Program?There can be more going on in a jar than just as a container for classes.
    The manifest defines the behavior. So to replicate the behavior you must do exactly that.
    As an example of that.
    [http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html#download]
    Additionally by running it in your application you must consider what the scope of that is. If you just load the classes directly then they will be in the system class loader and thus cannot be unloaded. Using a class loader solves that.

  • Can I place jar files inside ejb-jar.jar?

    Hi,
    I'm reading about the packaging rules for creating an ejb-jar.jar file, and I'm wondering whether it's possible to include a jar file (containing helper classes) inside of the ejb-jar file. A "lib" dir (for containing such helper jars) doesn't seem to be part of the ejb-jar file format.
    The EJB 3 spec has this to say (Ch 20, "Enterprise JavaBeans 3.0, Final Release):
    "The ejb-jar file must also contain, either by inclusion or by reference, the class files for all the classes and interfaces that each enterprise bean class and the home interfaces, component interfaces, and/or web service endpoints depend on, except Java EE and J2SE classes. This includes their superclasses and superinterfaces, dependent classes, and the classes and interfaces used as method parameters, results, and exceptions"
    Now, it seems that the root of the the ejb-jar file contains all the classes (in the appropriate Java package layout). But what about including JAR files? The ejb-jar spec seems to have nothing to say about a "lib" dir that can contain jars (as a WAR file can).
    Summary of questions:
    1) Can an ejb-jar file contain a jar file? If so, how/where?
    2) Can the ejb-jar file's META-INF/MANIFEST.MF file contain a reference to a JAR file?
    I'm not considering correct design at the point... I'd just like to know if these 2 scenarios are possible.
    Thank you!
    kamuifx

    Yes, I have an ear file..................
    I have an EAR file; inside the EAR I have a WAR file (web module) and an EJB-jar file (EJB module).......... the EJB need some classes placed in a jar file (a helper .jar or an util .jar)...........
    my question is: How can I use the classes of the .jar files in te EJB??????
    thanks

  • Can i load a class in subdirectoy  inside a jar file using applet tag?

    hi every one.. thank you for reading ... i am really in dire need for the solution..
    my problem is that i have a jar file contianing a package which inturn contains my applet class...
    i am trying to access this applet class using a applet tag in html file. this html file is in same directory as the jar file. i am having no problems in windows but when i am doing this in linux apache server i was getting class not found exception. (already checked the file permissions). and when i am successful when using simple package directory instead of jar file . so gist of my quesition is "can i load a class in subdirectoy inside a jar file using applet tag in a html file"?

    When you tested in Windows were you using Internet Explorer? On Linux you will be using a different browser, usually Mozilla of some version, or Firefox. Note that the HTML tags for applets will be different between the browsers if you are using the object tag. Principally the classid value for the object tag will differ between Firefox and Internet Explorer.

  • How to read some files inside the jar

    Hi,
    I have an applet that runs with JWS. The user can input some information and then I need to show this information in a web page. As the application can be run offline, I cannnot use JSP�s to generate the web page. The information is saved in a xml file, so I use a xsl parser to generate the html code. The problem is that I have to include some javascript files (.js). I put these files inside the jar, but, how can I read these files, or how can I reference these files from inside the html page ?
    Thanks !

    You can use getClass().getResource(classpath)
    to get a stream version of the data from your
    jar file. If you need to put it in a file, you
    can write that stream to a file in a temp directory.
    classpath is the classpath of your js file

  • Updating a properies file in a .jar file

    Hi
    I have an application that reads a properties file from a .jar file using the getSystemResourceAsStream method on the ClassLoader class. What I would like to be able to do is to change values in this properties file when the application exits so the next time into the application the last used values for certain text boxes are remembered from last time. Can anyone suggest a way to achieve this. I can write the properties file out using the FileWriter class and this works but I read it is not a good idea to use these classes for this purpose.
    Thanks in advance

    Those .properties files can perfectly be writting using a FileOutputStream
    (not a Writer). Updating a .properties file in a .jar file isn't possible
    without rewriting the entire .jar file.
    Your scenario should work fine: if the .properties file doesn't exist, get
    a "prototype" from the .jar file and store it as an independent file. Update
    this file using a FileOutputStream ad lib.
    kind regards,
    Jos
    ps. if you don't like this scenario, you could have a look at the Preferences class.

Maybe you are looking for