Getting a Directory within a JAR

Hello, I've been wondering how it is possible to "get" a directory from within my JAR file. If, for example, my JAR's structure is:
- meta-inf\manifest.mf
- something\*.class
- properties\*.properties
- images\com\a.jpg
- images\com\b.jpg
- images\org\a.jpg
- images\org\b.jpg
Then, since my goal is to scan the JAR for all images recursively, and store them in a HashMap, I need to be able to recognise com as a directory, and org as well.
With Thanks,
laginimaineb.

Nevermind, I found the answer already. To get a File from within the JAR I use :
ClassLoader cldr = getClass().getClassLoader();
URL urlToDir = cldr.getResource(directory);
File dir = new File(urlToDir.toURI());Thanks anyway.

Similar Messages

  • How to get the entries in a jar/zip file contained within a jar/zip file?

    If I want to list the jar/zip entries in a jar/zip file contained within a jar/zip file how can I do that?
    In order to get to the entry enumeration I need a Zip/JarFile:
    ZipFile zip = new ZipFile("C:/java_dev/Java_dl/java_jdk_commander_v36d.zip");
    // Process the zip file. Close it when the block is exited.
    try {
    // Loop through the zip entries and print the name of each one.
    for (Enumeration list =zip.entries(); list.hasMoreElements(); ) {
    ZipEntry entry = (ZipEntry) list.nextElement();
    System.out.println(entry.getName());
    finally {
    zip.close();
    Zip file "java_jdk_commander_v36d.zip" contains two zip entries:
    1) UsersGuide.zip
    2) JDKcommander.exe
    How to list the entries in "jar:file:/C:/java_dev/Java_dl/java_jdk_commander_v36d.zip!/UsersGuide.zip"?
    The following code:
    URL url = new URL("jar:file:/C:/java_dev/Java_dl/java_jdk_commander_v36d.zip!/UsersGuide.zip");
    JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
    zipFile = (ZipFile)jarConnection.getJarFile();
    would point to "jar:file:/C:/java_dev/Java_dl/java_jdk_commander_v36d.zip", which is no help at all and Class JarURLConnection does not have an enumeration method.
    How can I do this?
    Thanks.
    Andre

    I'm not sure I understand the problem. The difference between a zip and jar file is the manifest file; JarFile is extended from ZipFile and is able to read the manifest, other than that they are the same. Your code
    for (Enumeration list =zip.entries(); list.hasMoreElements(); ) {
    ZipEntry entry = (ZipEntry) list.nextElement();
    System.out.println(entry.getName());
    }is close to what I've use for a jar, below. Why not use the same approach? I don't understand what you're trying to do by using JarURLConnection - it's usually used to read the jar contents.
    String jarName = "";
    JarFile jar = null;
    try
        jar = new JarFile(jarName);
    catch (IOException ex)
        System.out.println("Unable to open jarfile" + jarName);
        ex.printStackTrace();
    for ( Enumeration en = jar.entries() ;  en.hasMoreElements() ;)
        System.out.println(en.nextElement());
    }

  • How to find directory java program .jar file is in?

    I have a Java program that needs to save some configuration and data files in the same directory as the .jar file that contains the program.
    If the user launches the app from that directory, it's easy.
    The problem is when they do something like "java -jar /dir1/dir2/program.jar".
    When they do that, HOW can I find the directory the .jar file is in - from within the program?

    CasaDelGato wrote:
    I have a Java program that needs to save some configuration and data files in the same directory as the .jar file that contains the program.
    If the user launches the app from that directory, it's easy.
    The problem is when they do something like "java -jar /dir1/dir2/program.jar".
    When they do that, HOW can I find the directory the .jar file is in - from within the program?There is an obscure way that basically consists of the following.
    1. Get the class loader of the class (obviously one in the jar)
    2. Find the 'resource' of that.
    3. Resolve that to a path
    4. Make it absolute if not already.
    5. Extract the directory part.
    2 and 3 are always the hard ones (conceptually and figuring out from the javadocs). There are examples somewhere on the forum.

  • Getting absolute path of a JAR from its classfiles?

    I am creating a class witch is going into a jar-file. With this jar-file I will also have another file (config/data-file). That file will allways be in the same directory as the jar-file.
    What I am wondering is: How can i programatically get the path of the "current" JAR file the "current" class is contained within, so that I can open/read the config/data file?
    Today I use a method which i hope I can replace with a better method:
    private static String jarfile="progconf.jar";
    private static String projectname="ProgramConfig";
    public static String getJarPath() throws Exception{
         try{
              String jvc = System.getProperty("java.class.path")+";";
              if(jvc.indexOf("\\"+jarfile+";")>0){
                   jvc = jvc.substring(0,jvc.indexOf("\\"+jarfile+";"))+"\\";
                   jvc = jvc.substring(jvc.lastIndexOf(":")-1); //Filsti
              }else if(jvc.indexOf("\\"+projectname+";")>0){
                   jvc = jvc.substring(0,jvc.indexOf("\\"+projectname+";"))+"\\";
                   jvc = jvc.substring(jvc.lastIndexOf(":")-1); //Filsti
              }else{
                   throw new Exception("Klarte ikke finne JarPath!");
              return jvc;
         }catch(Exception ex){
              throw new Exception("getJarPath-" + ex.getMessage());
    }-Wolflad

    check out my replies at the link shown below:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=494736
    ;o)
    V.V.

  • How to tell if a class within a jar file is being accessed?

    Hello,
    I have written a web app using NetBeans 5.5 and some external libraries.
    Rome 0.9 and a Rome module georss-rome.jar.
    Everything works fine with the integrated Netbeans tomcat 5.5...
    When I try to deploy this as a war using Tomcat 5.5 standalone It will not work properly. To make things worse, I get no error.
    In my code, I instantiate a class in the georss module by doing the following:
                    GeoRSSModule geoRssModule = GeoRSSUtils.getGeoRSS(entry);
                    if(geoRssModule != null)
    ...do something;
    }I then check for null and if not null I do stuff with methods within that jar.
    Basically I pass a rss feed or a georss feed. If its a georss feed then geoRssModule would not be null.
    As I said, this works fine within integrated tomcat but does nothing (no expected action) with standalone which leads me to believe that this is a deployment/jar/classpath issue.
    To me it seems that the class GeoRSSModule is never actually instantiated, but I never get any errors or expected output.
    I set tomcat logging.properties to FINEST for all types of logging.
    Is there a way to determine if the jar is even being accessed for that class file?
    TIA!
    BTW, I dont have the module source.
    I also verified that the jars (rome.jar and georss-rome.jar) are being deployed to webapps/myapp/WEB-INF/lib
    Message was edited by:
    gforty

    GeoRSSModule geoRssModule = GeoRSSUtils.getGeoRSS(entry);Try placing logging statements before and after this statement so that you can track execution. Make sure that your code at least gets to this particular line where you instantiate the GeoRSSModule object. Then place a logging statement afterwards to see if an object was indeed created.
    What helps me in situations like this is that I also use the NetBeans attached debugger listening on your standalone Tomcat installation. This allows you to trace your execution on the server side, one line at a time, giving you a clearer understanding of program flow.

  • How to get .class files from a JAR at runtime

    Hi. I wrote a program that load the classes it find in the "prog/components" directory. It enables me to add new components without messing with the code of the main program. Everything worked fine when wrote a batch file - but I want to create a JAR file. In the Batch file I gave the path to those files for the class loader in a loop, and it was "prog.components/" + filename[i] (I get the file names by using a FileFilter).
    My question is - how can I obtain the files in the prog/component directory inside the JAR? WHen I left it as it was in the BATCH it didn't work because there wa no prog/component dir outsiede the JAR. What should I do?

    Thanks man, that was really helpful! I wrote some piece of code that inserts the file names I want to an ArrayList. However, I wonder how you would make the program list all the files in the "prog.jar/prog/components/" directory? I think your code would be better tahn mine since you've coped with it before. Could you write the code snippet?

  • Get Domain directory path in weblogic server 10gR3

    Hi all,
    Does anyone know how to get the directory path of to a domin in WLS 10gR3 using JMX?
    Basically, I am trying to create a timer in WLI manually using Java code but for that to work, I
    need to know the domain directory path as below.
    BTW, I know it can be created in WLST with ease... but I want to try using Java code
    import com.bea.wli.mbconnector.timer.TimerConnGenerator;
    public class WLITimerTest {
    public static void main(String args[]) {
    String domainDir = <path of domain>; // e.g. C:\bea103\user_projects\domains\mydomain
    String domainDir = "C:/bea103/user_projects/domains/esis_domain";
    try {
    TimerConnGenerator.main(new String[] {"-inName", "timerName", "-outfile",
    domainDir +"/WLITimerEG_" + "timerName" + ".jar"});
              } catch (Exception e) {
                   e.printStackTrace();
    Thanks
    Sam

    Just to answer my own question. It's System.getenv('DOMAIN_HOME') where DOMAIN_HOME is set in setDomainEnv.cmd of your domain.

  • Get current directory(without user.dir)!!!!

    Dudes,
    need help badly... i need to get the current directory of the jar file where it gets launched from. Since the launching takes place from an html file, the system property "user.dir" does not get set to the current directory. Both the html as well as the jar file is in the same location.
    so in short, i need to get the current dir without using the user.dir that i can later set it explicitly thru my program(to the current dir)

    "Search the CLASSPATH for the first occurence of your jar file... but FFS only do it once, and save the result"
    Cud ya elaborate that statement.
    Also, the reason i wanna know is this : my jar file references all the images that it uses, thru a relative path(./a/b/c.....). So wen i simply double click the jar file, the relative path("./") gets replaced with the current workin directory(from user.dir) and fetching of images works perfectly. But if it is launched via html, the user.dir does not get set to the current working dirctory and as a result the jar file looks for its images in the default location which is the desktop. so it doesnt work
    Now that the context is clear, is there a way out???? or is there any way in which i can set the user.dir from the html itself(maybe by using javascript or somethin??)

  • Deploying Java apps: Jars within a jar

    Dear Java gurus,
    Imagine I have a Java application. I wish to bundle it all within a jar file. The problem is, I also wish to add some dependencies, in the shape of a couple of other jars. This means that the user could run the app using the straight-forward "java -jar myapp.jar" command.
    So, I jar it all up. This Jar contains a MANIFEST.MF that specifies the main class, and also the classpath. ("Class-Path: lib/other.jar") as the extra jars were in a dir called lib within the jar.
    But, this doesn't work run as java complains about not being able to resolve classes that exist within other.jar.
    I have yet to find anything on the net beyond really basic examples of using jar. Anyone had more experience with it and can share some tips?

    Its a horrible thing. Basically, you can't include jars in jars.  I asked this question on the Java.sun.com forums a while ago if you want to search there. Its a common question and pisses people off.
    You have some other options:
    1: extract all dependency jars and include the classes in a single jar file.
    2: distribute multiple jars and set the Class-Path in the Manifest to point to them. Since you're pointing at lib/other.jar, you gotta use:
    myjar.jar
    lib/other.jar
    You won't find another way around this. I recall somebody suggesting that packaging jars with jars should be available in mustang, but as of yet, it is not so.
    Also, I would strongly recommend starting to use Java Webstart /JNLP for distributing java apps. I've never done it, but it takes all this hassle out for you by automatically maintaining dependencies. I'm intending to try to get Ensmer going using JNLP sometime.
    Dusty

  • Peoplecode method to get Webserver directory

    Is there any peoplecode methods available to get webserver directory?
    My requirement is, i have used third party class file (jar) to compare two files. That class file should not execute on application server, it has to execute on client side (ie) on webserver. Some of the Oracle users suggest me that, have to use javascript to execute jar files on client side. Hence i have to place my class file on webserver directory which means webserver holding the directory at the runtime. I need a peoplecode method to get the webserver directory. Please suggest your ideas...!

    Hi sasi,
    If I understand correctly, you are looking for a way to determine a directory (dynamically) at runtime.
    use a URL and the GetURL(URL."URL_ID_VALUE") function to achieve this for you? This way you can configure the path and if you decide to change the requirements (read: directory), then all it would take is adjust the URL.
    Hope this helps.
    Regards,
    997449

  • Run a jar-File and modify a file within this jar-File

    I am running the jar-File, which contains my compiled java files etc.
    The program has a config part, which is saved in xml file.
    This xml file is in the same directory like the class files.
    All theses files are zipped as jar-File.
    Well, I am running the jar file.
    - But how do I open and save the xml file ?
    - Is that possible ?
    - How difficult ist that ?

    I tried to do this some time ago and found out the hard way that you can't, just as DrClap just told you. I would have taken his work for it anyway, but for what it's worth, I thought I'd mention that you're not alone. In my case I wanted to be cute and keep my properties all together within the .jar; thought it'd be cleaner that way. Ended up just creating the more typical class_name-props file in my home directory. Is this what you had in mind?
    ~Bill

  • Updated to firefox 4.0. since then my computer is very slow accessing the internet, websites and getting information from within a website.

    Over a week ago I updated my computer to Firefox 4.0. Since then my computer is very slow accessing the internet, websites and getting information from within a website. Also, a tool bar has shown up for YAHOO which I did not request. The old detailed tool bar for Firefox has disappeared. All that now shows for Firefox is the area to enter website urls and an area to enter topic for a Google Search.
    Many times I have to exit Firefox and re-enter it later to access the internet.
    Please advice what I need to do to get back to the speed I had with the older version of Firefox.
    Thank You,
    Dennis

    Over a week ago I updated my computer to Firefox 4.0. Since then my computer is very slow accessing the internet, websites and getting information from within a website. Also, a tool bar has shown up for YAHOO which I did not request. The old detailed tool bar for Firefox has disappeared. All that now shows for Firefox is the area to enter website urls and an area to enter topic for a Google Search.
    Many times I have to exit Firefox and re-enter it later to access the internet.
    Please advice what I need to do to get back to the speed I had with the older version of Firefox.
    Thank You,
    Dennis

  • HT201318 How do I downgrade my iCloud storage and get a refund within the 15 days?

    Help!
    How do I downgrade my iCloud storage and get a refund within the 15 days?
    I recently up-graded from 10 to 20GB in the middle of my storage plan, but I've changed my mind. How can I get a refund and revert my account back to its previous settings?

    If in the US, follow the link below (you must request a refund within 15 days) -
    https://discussions.apple.com/message/16968425#16968425
    it gives the USA phone number - if you don't live there you will have to find an equivalent number from the 'Contact Us' link at bottom right of this page.

  • How to get current directory and enviroment variables

    Hello, 
       1.- Is there a global variable to get the actual (project, where is DSB and DSQ files) directory? 
       2.- Is there a way to get User directory like reading enviroment variables from Operative System? 
    Thanks in advance.
    PD: I'm using Dasylab12
    Solved!
    Go to Solution.

    Yes, use System Strings
    For example, ${DATA_FOLDER}.
    The easiest way to get the list is to right click and select Global Strings. The bottom half of the dialog box lists system strings, including date, time, the name of the worksheet, with or without path, the DEFAULT folders for worksheet, data, other, black box, etc. 
    - cj
    Measurement Computing (MCC) has free technical support. Visit www.mccdaq.com and click on the "Support" tab for all support options, including DASYLab.

  • How can i get the directory of some files?

    I have the following class and the directory images in the same folder as this class. the problem is that i don't see the icons and i don't know how it works.
    please help. thanx a lot
    public class MyIcons {
    private final static String images = "images/";
    private final static String directory = getDirectory();
    public final static ImageIcon SAVE_FILE =
    new ImageIcon(directory + "savefile.gif");
    // not sure how to get the directory
    public static String getDirectory() {
    return ?????;
    }

    Try following code:
    import java.net.URL;
    public class MyIcons {
    private static final String images = "images/";
    private ImageIcon saveFile;
    public static void loadImage(){
    URL resourceURL = null;
    resourceURL = this.getClass().getResource(images
    + "savefile.gif");
    saveFile = new ImageIcon(resourceURL);
    }

Maybe you are looking for

  • Ipod not showing up in "Source" window

    Hello all!! Need some help here. When I plug my ipod into the computer...my Ipod no longer shows up in the "source" window on itunes. A "do not disconnect screen" will flash on my Ipod for hours on end. I have already reset my Ipod. Dont know what to

  • Some messages are not reaching my webmail INBOX

    I use an email forwarder so that my email address is [email protected] (It's not really xyzzy but you get the idea.) They redirect messages to my actual Verizon email address. In the past week, some emails sent to xyzzy are not getting to VZ. POBOX s

  • Would like to get others opinion on the battery indacator or is it me

    When it bites five percent it won't let me know I have percent I have to look when it's suppose to let me know. When I'm like at 2 or 3 percent it will say oh you have five percent left the message will come up then. I flipping out and I don't know o

  • In photos,events have lost dates and been replaced with IMPRT numbers.Any ideas?

    In photos,events have lost dates and been replaced with IMPRT numbers

  • Startup Problems with GarageBand

    Every time I try to start up GarageBand, it opens the startup window, but when I click "Create New Music Project," the program quits unexpectedly. It won't start up from an existing song file either. Only "Magic GarageBand" works. What could be the p