Access to a file in a jar archive

Hello.
I hava a Bean in a jar file. The Bean shoul read a file
which is is also in the sama archive. The location of the jar file is unknow.
How can the Bean read the file.
With friendly regards
tom

I (now) know about images in jars...
//usage: loadIcon("icons/Open16.gif")
     public ImageIcon loadIcon( String location ){
          URL img = null;
          try  {
               img = ClassLoader.getSystemResource(location);
          }catch (Exception e){
               e.printStackTrace();
          return new ImageIcon(img);
     }Make sure that the location that you use is case sensitive and the seperator is "/".
In the usage above, the image is in a dedicated "icons" folder in the jar.

Similar Messages

  • Access a data file within a jar package

    hello, everyone
    I got a problem in locating a data file packaged in jar file.
    For example, in foo.jar there is a data file with the directory path "d1/d2/data.log". In the command line, I want to access that data file by making it as an input to the Main class which will load data.log at runtime. However, it reported exception of "no such file or directory". In the command line, I input:
    java -classpath foo.jar d1.d2.Main d1/d2/data.log
    any suggestion?
    Thanks
    Chinyi

    I don't understand what you're trying to do by including d1/d2/data.log in the commandline? It appears to be extraneous and invalid.
    Assuming:
    that the class you want to execute is Main.class in foo.jar, and that its path in the jar is di/d2, and
    that Main uses d1.d2.data.log when it access this file, it should work.

  • How to play wave files from a jar archive?

    Dear friends,
    I have a .jar archive with several .wav files, and I would like to play them without extract these files.
    This is possible to do? If yes, please show me how to do this.
    Thank you for your attention,
    Augusto Nunes

    This discussion of getResource is very useful: http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#getresource

  • Can I refer to a file insider a .JAR archive with an instance of File?

    Hi,
    I was wondering if it is possible to refer to a file stored inside a .JAR file just as if it was on a regular folder by using an instance of the java.io.File class. Otherwise, how can one do that?
    Essencially what I need is to check if a particular class file is contained inside a .JAR archive.
    Best regards,
    Norberto

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/jar/JarFile.html
    See the entries() method to get a list of everything in the jar file. Most of the time the only thing you would want to do with a File is read the bytes from it, which you can do easily using the JarFile class.

  • Accesing a file in the Jar archive

    Hello, I am new to Java and I have a problem with my program.
    I am writing an application that among other things can read text files.
    Every thing works fine as long as run the .class
    If I pack the application into a .jar executable (with the text files inside)
    I can not reach the files any more.
    Probably I am doing something wrong during the packing...
    //this is the constructor
    public Antenna(File fos){
         try{
         FileReader oos = new FileReader(fos);
         BufferedReader filebuf = new BufferedReader(oos);
                         //I do something with the file
               oos.close();
                } catch (IOException e) {
                                              JOptionPane.showMessageDialog(this,
              "File: " + fos + " NOT FOUND", "Warning",               JOptionPane.ERROR_MESSAGE); }
    //I use something like this to initialize my class
    Ant1 = new Antenna(new File("Curzola.ant"));When I run my application like .jar (that contains the "Curzola.ant") I get the error unless the file Curzola.ant is external to the jar file in the same directory as the -jar file.
    Does someone know what I am getting wrong?
    I think it should be easy!!
    Thanks
    carlo

    If you want to read a text file line-by-line, BufferedReader is a good choice.
    If you have a InputStream, you can use classes from java.io to convert an
    input stream into a BufferedReader:
    InputStream -> InputStreamReader -> BufferedReader
    For example:
    void f(InputStream in) throws IOException {
        Reader r = new InputStreamReader(in, "ISO-8859-1");
        BufferedReader br = new BufferedReader(r);
        //use br
    }Some coders would use a different InputStreamReader constructor:
    Reader r = new InputStreamReader(in);But that uses the platform's default charset encoding, and since we are
    talking about a file in your jar, you shouldn't assume it's being decoded
    on the same platform it was encoded.
    Your original code, by the way, used FileReader, which is just a convenience
    subclass of InputStreamReader, and your code:
    FileReader r = new FileReader(file);was just shorthand for
    InputStream is = new FileInputStream(file);
    Reader r = new InputStreamReader(is);Finally, what objects should your constructor accept? One approach
    is to be super flexible and accept many types:
    public class Example {
        public Example(URL url) throws IOException {
            this(url.openStream());
        public Example(File file) throws IOException {
           this(new FileInputStream(file));
        public Example(InputStream in) throws IOException {
            this(new InputStreamReader(in, "ISO-8859-1"));
        public Example(Reader r) throws IOException {
            BufferedReader br = new BufferedReader(r);
            //use br
    }The other extreme is to accept one basic type. In your case, since you want to read text, is to accept a Reader:
    public class Example {
        public Example(Reader r) throws IOException {
            BufferedReader br = new BufferedReader(r);
            //use br
    }...and have the calling code do the work of constructing an appropriate reader.

  • Load a file stored in a Jar Archive

    Dear all,
    I have a text file stored in a Jar Archive and I want to obtain a File instance of the text file from the jar file. The reason for this is because I have a method which read in a File instance of the file.
    From my understanding, one can obtain an byte[] array from a file stored in a text file. Is there a way to convert the byte[] into a File instance?
    Thanks.
    Sunny

    Once again, thank you for your prompt response. I agree that one can obtain an inputstream of a file from the Jar archive and work with it. The problem that I am having is that the method that I have is not written by me, to be precise, I am using Syslog and try to read in a configuration file packed in a Jar Archive. The Syslog library only provide a method, which only accept a 'File' instance as an input argument.
    Thanks.
    Sunny

  • Access wav-files in jar-archives (URL works, File works not)

    Hi there!
    The un-jar-ed application works fine, but I have a problem with accessing files in jar archives.
    URL url = Classname.class.getResource("testdir/test.wav");works fine (and loading images works this way), but as soon as I try to get the file
    File file = new File(url.getFile());I get an null-exception.
    (in my case, I use the jmf package to play the sound-files, and I get the problem when I try to instatiate the MediaLocator
    MediaLocator mediaLocator = new MediaLocator(url);but I think the problem is the same)
    Thanx for help

    works fine Then keep doing it that way. What's the problem? It's true that you can't access something in a jar archive via a File object, so don't do that.

  • How to use database file within jar archive

    Hi. I'm quite new in Java programming, and I'm wondering if what I did makes sense or not.
    In my applet I'm using a MS Access Database file, which is contained in the same Jar Archive like the Applet class file. I tried and tried to use this Database file directly but it wouldn't work. Than I read in some other forum that it is not possible to use the database file directly within the archive. First question: Is that true?
    Than I desided to extract the DB file in the init() method of my applet to the default temporary foulder. When the program is closed I use the destroy() method to delete it again. Everything works well now. But is this the typical way this is done?
    Thanks for help!

    Hi,
    here is the code which extracts the database file out of the jar archive (the same archive in which the class file is):
    //get the user temporary folder
    File TempFolder = new File(System.getProperty("java.io.tmpdir"));
    //create (empty) db file
    efile = new File(TempFolder, "steelSections.mdb");
    // if database file is not yet extracted
    if (!efile.exists()) {
    //get an input stream for the database file
    InputStream in = new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream(
    "FaST/db/steelSections.mdb"));
    //create an output stream for the db file on the file system
    OutputStream out = new BufferedOutputStream(new FileOutputStream(efile));
    //-Buffer to copy the data
    byte[] buffer = new byte[2048]; //buffer to copy the binary
    for (;;) {
    int nBytes = in.read(buffer); //read data
    if (nBytes <= 0)
    break; //no more data to read
    out.write(buffer, 0, nBytes); //write data
    out.flush(); //close out and in streams
    out.close();
    in.close();
    If you have the db file in an other jar archive file you need a referenze to the entry in the other jar archive. I'm not sure how to get this, but I'm sure you fill find a solution by searching in this forum...
    Good luck!

  • How can  I access my java class file in a .jar file ...PLz Help anyone!!

    Hi Folks,
    I am new in Java programming, have a .jar file that contains .java files that I want no access and use from another .java file. These files in the .jar file basically form a library thus have no main-method. However I can not declare an instance of the library (in the .jar file) and use it within my program.
    Plz help ...I have tried all I know in vain.
    Thanks.

    temba wrote:
    Hi Folks,
    I am new in Java programming, have a .jar file that contains .java files that I want no access and use from another .java file. These files in the .jar file basically form a library thus have no main-method. However I can not declare an instance of the library (in the .jar file) and use it within my program. You are making little sense. You can't instantiate .java files.
    Plz help ...I have tried all I know in vain.
    Thanks.Could you post WHAT you have tried and also post what error messages you received?

  • Accessing a Jar file within my jar...

    Hi,
    I've got a JDBC Jar file that I wish to include in my Jar package, but I'm unsure how to do this...
    I'm using ant to compile my code & package it up into the Jar, & I can get it to compile fine, and package everything up fine (including the JDBC jar), but when I go to run the app I get an error
    (No suitable driver).
    My app works fine without being Jar'ed up.
    I'm guessing that I need to set the classpath to reference the JDBC jar file in my jar... but I'm unsure how to do this.
    Any help would be greatly appreciated.

    package what you thirdpart jar file and your jar file to one jar file..
    it will run ok.
    maybe the below tool can help you.
    a tool named JavaJar(written by java) can compress and decompress jar,war, ear and.zip...
    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.

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

  • Invisible class files inside jar archives

    Hi!
    After developing an application with J2SE 1.4.1_05, under Windows 98, using some packages archived in jar files, I cannot launch the application with the J2RE 1.4.2_02 or J2SE 1.4.2_02 (in the same Windows 98) because the class files in the jars are not 'visible', even if the CLASSPATH is set up correctly. The only classes that work right are the unpacked ones. But it still does work in Windows 2000.
    What might be wrong?

    Thank you for your answer, and excuse me if my question was kind of confusing.
    The issue here is that the files ARE in the jar files (without './' on the front), but in the environment (Windows 98 + J2RE 1.4.2_02) the application does not work, unless I unpack the jars and use the packages in the same manner I did during the development stage (for example, all classes of package 'myutils' in the folder 'myutils').
    The weird thing is that this DOES NOT happen in the environment (Windows 2000 + J2RE 1.4.2_02)!!! Just after installing the J2RE 1.4.2_02 under Win2K, the app does work OK! That is why it seems that ONLY in the first case (Windows 98 + J2RE 1.4.2_02) the class files inside the jars seem to be 'invisible' to the environment.
    As I have mentioned in my question, the app was developed in the environment (Windows 98 + J2RE 1.4.1_05), and it did still work with the jar files, so I wonder if there might be some compatibility between Windows 98 and the J2RE 1.4.2_02.
    Except my own jars, the same thing does happen with the jai_core.jar file coming with JAI 1.1.2. But this does not happen when it comes about rt.jar, in ..\jre\bin!!!
    All in all, the question still is: what might be wrong?

  • Accessing files outside the jar

    Greetings,
    I've got a jar file with some classes, which is in a directory. In this dir there are also some resources. How to access those resources from inside the jar?
    Thanks in advance,
    imaginner

    Try this...
    In your class, add this line...
    String path = System.getProperty("user.dir");
    it will give the path of the folder from which you are running
    your jar file. To this path, add your properties file name
    and start reading it.....
    For example:
    If i have a properties file which both :
    1) should be able to read and edit by users and
    2) should be accessible by classes in jar file....
    I would use the above concept like this.... in my class.....
    String path = System.getProperty("user.dir");
    Properties props = new Properties();
    props.load(new FileInputStream(path+"yourPropertiesFileName"));
    Hope it helps...
    Vijay

  • How to access all the classes in a jar file

    hai
    i have a custom built JAR file which has many classes(Translets) in it . i want to access the classes inthe JAR file in a JSP page or an servlet. I have tried setting the classpaths etc , but nothing has happened.
    Where should the jar file be Palced - i am using Tomcat Server.
    How Can i Access the class files .. Very Urgent .. Please Help me ..
    Peter Iyer

    What is showing a file not found error? The web browser? if so, that's probably not a problem with the jar file.
    Also, have you expanded your jar file to make sure that you jarred it up correctly?

  • How to Sign jar file?? (Needed to access a File Outside of JAR)

    Hi
    We had a program that acces a txt file in the nokia's memory card but we need to sign the program because if doesn't do that we can't access to the file.
    Thank you

    Hi!!
    I tryed and my N70 says "Imposible install the application".
    and this is my jad code:
    MIDlet-1: VA2, , VA2
    MIDlet-Jar-Size: 2030
    MIDlet-Jar-URL: VA2.jar
    MIDlet-Name: VA2
    MIDlet-Vendor: Vendor
    MIDlet-Version: 1.0
    MicroEdition-Configuration: CLDC-1.1
    MicroEdition-Profile: MIDP-2.0
    MIDlet-Permissions:javax.microedition.io.Connector.http, com.qualcomm.qjae.gps.Gps
    MIDlet-Permissions-Opt:javax.microedition.io.Connector.socket,javax.microedition.io.Connector.file.read,javax.microedition.pim.ContactList.read,javax.microedition.io.Connector.sms,javax.wireless.messaging.sms.send,javax.wireless.messaging.sms.receive,javax.microedition.io.PushRegistry
    Thank you

Maybe you are looking for

  • Data does not show up in 0PUR_C01

    I deleted the set up table for '02' in LBWG , I executed for Industry and transaction key in SBIW under Purchasing . I extracted in RSA3 for 2LIS_02_ITM and it brings 1003 records. I loaded this in BW into 0PUR_C01 . The load was successfull . But 0

  • Is it possible have ISA-type functionality using EP alone?

    We are implementing r3/enterprise (ECC 5.0). we need to provide access to a few large customers (Distributirs/resellers) for Order entry & enquiry of their own orders etc..(with variant configuration). Internet Sales (ISA) seems to provide this funct

  • Archive emails lost after Mavericks upgrade

    After uopgrading to mavericks, all the emails I had moved into mailboxes in the Archive have disappeared. The mailboxes I had created for archiving were all inside "On MY Mac", which is now duplicated about 14 times, and all the mailbox folders insid

  • Swatches

    I am having issues with my swatches. Or at least I think I am. I am using InDesign CS3 5.0.4. I designed a catalog with all my text being black. When I select my text, it shows black but it does not have a color option (RGB, CMYK, LAB), I have to man

  • How do I enable the extension group on reader

    I updated my laptop to the 8.1 windows software. Since then my 'Extension' bar is missing in Reader. How can. I get it back?