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.

Similar Messages

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

  • 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 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 can i get the path to config folder placed inside the jar file?

    Hi i have developed an RCP application using eclipse.
    In my application config directory is there.
    When i export my RCP application as JNLP Project the jar file is created which contains config folder inside it.
    When i download the application using java web start , how can i get the path to config folder placed inside the jar file?
    Will the config folder exists in local cache in my system?
    Help needed.
    -Deepak

    -- This works in CS6:
    tell application "Adobe InDesign CS6"
      set myDocument to active document
      set selectedRectangles to selection of myDocument
      set theGraphicsLink to file path of item link of (graphic 1 of (item 1 of selectedRectangles))
    --> "Macintosh HD:folder/folder/filename.tif"
    end tell

  • How can read/write FORMSWEB.CFG & DEFAULT.ENV file entries programatically?

    Environment: Forms 9i (Web)
    As : Oracle 9i As
    How can read/write FORMSWEB.CFG & DEFAULT.ENV file entries programatically?

    I guess you cannot. There is an enhancement request to get the name of the config-section as a GET_APPLICATION_PROPERTY.
    What you could do is add extra parameter(s) to your form and pass the necessary formsweb.cfg parameters in this form parameter. See Get formsweb.cfg section name in Forms for more info and an example.

  • 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 display PDF file, inside the SWF file or flash exe file

    I  have create the application for Employee details using flash actionscript 3.0
    This application is to view the organization employee details, iam using the tree component in this application,
    the tree component is to get the dada via xml file, this xml file save in the remote server ,
    I display the PDF file inside the swf file to using " StageWebView" class . Then i goto Publish Setting to change the  player Adobe Air 2.6 and run the flash file using
    ctr-l + Enter . It is work correctly the PDF file is Displayed inside the Swf Output. But I Publish this file to exe . then i run the exe it is not working .
    The application is run The xml data is onely get and displayed the Tree component then i click the tree item employee name the xml data in not display and the pdf file is not open .
    Pls Help...................

    hi i need to display the pdf file inside the swf file or flash exe file .
    I use the navigateToURL class this is open the pdf file on the browser. I want to display the pdf file in the swf window .
    Please suggest.

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

  • 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

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

  • To add the JNI dll to the jar file and use the dll inside the jar file

    Hi to everybody,
    I am new to java.
    I want to add the JNI dll to the jar file and use it in the java class.
    How can I achieve it.
    Please help me.
    Thanks in advance.
    Regards,
    M.Sivadhas.

    can't be done because none of the known operating systems support reading binary libraries from .jar files ... you can add the binary to the jar but then you have to extract it...
    besides, mixing platform specific and platfrom independent components is not a very good idea, i'd keep the dll out of the jar to begin with

  • How to detrmine what directory you (the JAR file) are in

    Is there some sort of function that returns (ideally in a string, or array of strings) what directory you (the JAR file) are in? Something along the lines of:
    String dir = Function();Then dir would be something like : "C:\Users and Settings\Me\Java Apps"
    Thanks.

    It's not clear exactly what is wanted. Look at the information in the API for java.lang.System,getProperties(), which lists properties available - choos the one you want.

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

  • Why can't I load the image files in the Jar file?

    I make an application in a structure bellowing:
    A.class
    images\a.gif
    images\b.gif
    and I packed all of them(include the images directory) into a A.jar file.
    when I run the A.jar file,the application can't load the images in the Jar file.
    How can I do to solve this problem?

    It isn't working. The URL that is returned is blank. I tried rearanginf my stuff a little. Now it is set up like the other guys. So all the classes aren't in a package. so its:
    MyClass.class
    Sounds/mysound.wav
    all of this is in a .jar file. If it isnt in a .jar file it works. Bit I have mor than one class so I would really like it in a .jar file. But when it is MyClass.class.getResource("sounds/mysound.wav") returns nothing. Not null. Just blank.

Maybe you are looking for

  • Please Help me Out APPLE TEAM :(

    Hi, I recevied an iPhone 5 as a gift in the month of january from my friend from US. Am in INDIA and am using it in INDIA only. It was perfectly working fine until i upgraded it to ios 7 beta version through a third party. i saw one of my fried doing

  • How do I remove the master on 2 pages

    I have a document with 52 pages and spreads. Now I have added to pages at the beginning of the document and want to remove the master for only these 2 pages. How do I do that? Thanks

  • PLS-00435 error

    Hello, i got PLS-00435 when compiling the following procedure: create or replace type marire_ob is object(id_dept number, salariu number) create or replace type marire_angajati is table of marire_ob; create or replace procedure proc_upd_salary (marir

  • Watching iPod photos on TV

    Is it possible to view singles photos on TV using the Apple iPod AV cable or just slide shows?

  • My time capsule cannot support printer MF4500 is there a solution ?

    my time capsule cannot support printer canon mf4500 is there any solution ?