How to modify files in the jar pack

Is there any way to allow the classes to read and write the files or directories which are inside the same jar file?
Thanks

I think you can consider the jar file as a zip file and modify the content of it using
java.util.zip package:
http://www.java-tips.org/java-se-tips/java.util.zip/

Similar Messages

  • How to read files outside the jar ?

    Hi all,
    One of my classes inside jar needs to read XML file that resides in the same location where the jar sits. I use MyClass.getClass().getResource("MyXMLFile.xml") but it keeps returning null.
    If I place the XML file inside the JAR, I can find it using above methods, but since I want the user to easily edit the XML file without opening the jar file and detach the XML file, I choose not to include the XML file inside the jar.
    Any help would be greatly appreciated.
    Setya

    Thanks for the dukes.
    My JDBC post on this forum discusses the issues of
    using Class-Path in your jar file. It works, but it
    may vary from system to system.
    Thanks also from my side: I have read recently your discussion about reading files in the jar's directory, since I had the same problem. So I was really glad to see your solution. However, I had one more problem, namely that I'm running my application sometimes from the jar, sometimes from the classes directly (debugging). In order to have always the correct path (although a different one) I have added some more lines to deal with that case. I hope that will be useful for you as well!
    Dieter Profos
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.net.URL;
    * @author pkwooster
    * @author Dieter Profos
    * @version 1.01 04/03/26
    public class ReadOutside extends Frame {
    private TextArea textArea;
    public static void main(String[] args) {
    // Create application frame.
    ReadOutside tc = new ReadOutside();
    tc.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
    System.exit(0);
    // Show frame
    tc.setSize(600,250);
    tc.setVisible(true);
    public ReadOutside() {
    textArea = new TextArea(20,80);
    add(textArea);
    readOutside(this);
    public void readOutside(Object o) {
    System.out.println(getCodeBasePath(o));
    /** Gets the absolute path of the directory where the JAR or CLASS file is
    * located. If the application is run from a jar file, the directory of the
    * jar file is returned; if it is run from a class file, the method will
    * return the directory path of the class file.
    * @param o The main class of the application.
    * @return The absolute directory path, or null if neither the class nor a
    * jar file could be found.
    public String getCodeBasePath(Object o) {
    Class c = o.getClass();
    URL url;
    // get the class name
    String className = c.getName();
    textArea.append("class name = " + className + "\n");
    // replace package periods by file separators
    className = className.replace('.','/');
    ClassLoader cl = c.getClassLoader();
    if (cl == null)
    cl = ClassLoader.getSystemClassLoader();
    url = cl.getResource(className + ".class");
    String xPath = url.toExternalForm();
    textArea.append("url = " + url + "\n");
    textArea.append("extPath = " + xPath + "\n");
    // make sure that the path starts with jar:file:
    if (xPath.startsWith("jar:file:")) {
    // remove jar:file: + the subsequent file separator
    xPath = xPath.substring("jar:file:".length()+1);
    int n = xPath.indexOf("!");
    if (n < 0)
    n = xPath.length();
    String jarPath = xPath.substring(0, n);
    textArea.append("jar file path = " + jarPath + "\n");
    n = jarPath.lastIndexOf("/");
    String jarDir = jarPath.substring(0, n+1);
    textArea.append("jar dir = " + jarDir + "\n");
    return jarDir;
    else { // if program is run from its class file (development phase!)
    if (xPath.startsWith("file:")) {
    // remove file: + the subsequent file separator
    xPath = xPath.substring("file:".length()+1);
    textArea.append("class file path = " + xPath + "\n");
    int n = xPath.lastIndexOf("/");
    String classDir = xPath.substring(0, n+1);
    textArea.append("class dir = " + classDir + "\n");
    return classDir;
    return null;

  • How to modify the Manifest file in the JAR file

    Hi guys,
    i am having a JAR which contains one Manifest file and 10 class files.
    actually i want to do some changes on the manifest file, but by using the ,
    " jar -cmf new_manifest_file jar_fileName all_class_files " option i am able to modify the manifest file by repacking the .class also.
    i want to modify the manifest file without disturbing the existing class files in the JAR file.
    Thanx in advance.............! ! ! !
    bye,
    subbu

    yeah, that is correct.
    my doubt is,
    Is it possible to modify the Manifest file which is inside the jar file, by without disturbing the Class files.
    --Subbu                                                                                                                                                                                                                                                                                                                           

  • 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

  • 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.

  • 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.

  • Can i add watermarks to PDF files using the PDF Pack?

    Can i add watermarks to PDF files using the PDF Pack?

    Hi Susannah,
    Adobe PDF Pack does not allow you to edit a PDF file. For that, you would need Acrobat. You're welcome to try Acrobat for free for 30 days: see http://www.adobe.com/products/acrobat.html for more information.
    This Help document describes how to watermark a PDF in Acrobat: Add watermarks to PDFs
    Best,
    Sara

  • 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...

  • 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.

  • Reading a file in the JAR

    i have a jar with class files and xml files in it. my application uses this jar and a particular class has a link to the xml. but while compiling, it shows path not found. the application couldnt read the xml file in the jar. how to set the path or read the xml file
    XML file in JAR:
    SQLMaps/admin/sample.xml [folder structure inside the jar file]
    my application has a path like SQLMaps/admin/sample.xml
    but how do we point to the xml file in the jar
    Regards
    Pradheep

    thanks anyway
    but i need the application to read the xml file in the jar file
    to be more clear.
    the jar file has xml in a specified folder.

  • How to attach files in the SMTP mail?

    Hi All
    Anyone know how to how to attach files into the mail configured
    with SMTP while changing the workstatus. The SMTP mail is working
    fine, but my requirement is to attach a file which is having static information
    like instructions for users or something like that.
    Please help me on this..
    My Version of BPC is MS 7.X
    Regards,
    Baijuchandran.B

    Have you seen this thread? Not an elegant solution, but it appears to work for some bizarre reason.
    https://community.bt.com/t5/Email/Email-won-t-accept-attachments-error-18-temporary-It-isn-t/td-p/14...

  • How to attach files to the thread we post in this forum?

    How to attach files to the thread we post in this forum?
    I’m wondering why there is no feature that allows attaching files from our hard drives! It is not practical to upload our file first in a certain web to provide it as a link in the post.
    Is there a way to attach our files directly from our hard drive to the post?
    Please, have a look on the screenshot below that shows a forum which includes all the very basic features!
    Thank you for the help
    Best
    Jamal

    Pierre,
    Just a guess on my part, but I would suspect non-image files in this case.
    As Ted points out, that WAS possible early on, and then the capbility to attach non-image files, was actually retained by a certain few forums, though not for long. This change was coincidental with a major spam attack, and at least one disgruntled user attempting to attach some rather nasty "stuff." I do ot know if those events prompted the change, as I was not part of the decision making group, that initiated the change.
    Though 95% of the files that I wish to attach, ARE image files, there are times, where a TXT file would be ideal, say for a crash log, or similar. Now, I can convert that TXT to a PNG, but then one has to work with pixel x pixel dimensions, and some files, like those crash logs, can be rather long.
    Personally, I find the loss of the Attach File (was via a button at the bottom-left of the editing screen) to be a sad comment on the actions of some, on these forums, but then that just reflects life outside of the forums.
    Hunt
    PS - I am glad that you found that little article useful Especially in the PS, PrPro, PrE and Encore forums, screen-caps can tell so very much about what is going on with a person's program, system or their Projects/Images.

  • How to change file content in JAR

    I'm deploying a Java application using Webstart based on different jars.
    One of the jars contains a 'config' file. The config file contains paths to databases.
    The idea is that the user can change these paths (or other settings) using the application.
    This would mean that the content of Config file in the jar file should be updated once the user
    changed 'the settings'..
    Is this possible ? If so, thanks to explain in detail.
    Greetz,

    depends.
    If a user makes a change, you can record that change, possibly using the persistance service.
    Then when he starts again, you read in the default config, then read from the persistance store to override those defaults.
    You can not change the initial jar, that is really on the server, or cached on various users machines.
    /Andy

  • How to modify and save the data in the table control

    how to modify and save the data in the table control

    hi priya,
    kindly go thru the code below.
    PROCESS BEFORE OUTPUT.
      MODULE status_9010.
      LOOP WITH CONTROL tab_control.
        MODULE move_data_to_table.
      ENDLOOP.
    PROCESS AFTER INPUT.
      MODULE user_cancel AT EXIT-COMMAND.
      LOOP WITH CONTROL tab_control.
        MODULE move_data_from_table.
      ENDLOOP.
    MODULE move_data_to_table OUTPUT.
    This is to move the data from the internal table to *the table control
    *zmpets_mode-modecode, zmpets_range-rangeid, *zmpets_servfacto-factor are field names of the table *control columns.
      READ TABLE int_factor INDEX tab_control-current_line.
      IF sy-subrc = 0.
        zmpets_mode-modecode = int_factor-modecode.
        zmpets_range-rangeid = int_factor-rangeid.
        zmpets_servfacto-factor = int_factor-factor.
      ENDIF.
    ENDMODULE.                 " move_data_to_table  OUTPUT
    **********************************************8888
    MODULE move_data_from_table INPUT.
    *To move the data from the table control to internal *table 'INT_FACTOR'.
      int_factor-chk = line.
      int_factor-modecode = zmpets_mode-modecode.
      int_factor-rangeid = zmpets_range-rangeid.
      int_factor-factor = zmpets_servfacto-factor.
       MODIFY int_factor INDEX tab_control-current_line.
        IF sy-subrc NE 0.
          APPEND int_factor.
          CLEAR int_factor.
        ENDIF.
    ENDMODULE.                 " move_data_from_table  INPUT
    if this helps , kindly award points.
    for any clarification just mail me.
    regards,
    Anversha.S
    [email protected]

  • Problem in modifying the class file in the jar file

    Hi,
    We created Support Desk link in the mast head with the help of
    com.sap.portal.epsolman.par
    it is working fine.but when we submit the error message it will a notification message saying that created Successfully.
    But we want to change that notification message to something else.But we found out that we need to change that in the java file .Basically the problem is  we dont know, after changing the java file, how do we compile it and put it back into jar file of that par file and deploy on to the portal
    java file is Solman.java
    points will be awarded for sure
    Bala Duvvuri

    hi,
    go thru this [link|How to deploy PAR File on portal??]
    hope this helps.
    Regards,
    Nikhil

Maybe you are looking for