Results 1 to 7 of 7

Thread: Resources

  1. #1
    zzpprk is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default Resources

    Hi

    I have the following code which returns two different things whether it is run on XP or Vista.
    Java Code:
    URL lURL = getClass().getResource("abc.txt");
    On XP it returns the full path to abc.txt inclucing the JAR file as follows
    Java Code:
    jar:localhost:8080/Folder/App.jar!/com/app/abc.txt
    On Vista the JAR file is missing and all the above call returns is
    Java Code:
    jar:/com/app/abc.txt
    Any ideas why this is and how to fix it?

    Regards

    Patrick

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    As I understand it, the search method and results will depend on the class loader used to load the class object from which the call was made.

    When you say you want to 'fix' it, does this mean that when you use the Vista URL (e.g. to make a File), it does not give access to the file abc.txt ?

  3. #3
    zzpprk is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default

    Hi dlorde

    Thanks for your reply.

    In the URL when running on XP there is a reference to a JAR file called App.jar and I can extract abc.txt from that. All is fine.

    On Vista there is no reference to the JAR file in the URL so my code doesn't know what to extract abc.txt from.

    Does that make sense?

    Patrick

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by zzpprk View Post
    Does that make sense?
    I'm not sure what you mean by 'extract abc.txt'. A URL is a resource locator, you use it to access resources. Either you can access the resource via the URL or you can't. You can check whether the resource is accessible by creating a File from it:
    Java Code:
    URL lURL = getClass().getResource("abc.txt");
    File file = new File(lURL.toURI());
    if (!.file.exists()) {
       return;  // file doesn't exist    
    }
    String filePath = file.getPath();
    ... // etc.
    You can also get the file name and path directly from the URL via getFile() and getPath().

  5. #5
    zzpprk is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default

    Let me try and explain.

    The file I want to load (in this case abc.txt) is located in the same JAR file as the class that wabts to read the file (in this case App.jar).

    On XP, the URL returned by getClass().getResource() contains both the name ot the JAR file and the path to abc.txt within this JAR file as I expect. I can then connect to that JAR file, open it and extract abc.txt.

    On Vista, there is no mention of a JAR file; just the path to abc.txt so I do not know where to get the file from. This file is not located in the file system.

    Regards

    Patrick

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by zzpprk View Post
    The file I want to load (in this case abc.txt) is located in the same JAR file as the class that wabts to read the file (in this case App.jar).

    On XP, the URL returned by getClass().getResource() contains both the name ot the JAR file and the path to abc.txt within this JAR file as I expect. I can then connect to that JAR file, open it and extract abc.txt.

    On Vista, there is no mention of a JAR file; just the path to abc.txt so I do not know where to get the file from. This file is not located in the file system.
    Firstly, apologies for my last post - the File code I posted won't work - I got confused :rolleyes:

    Secondly, I don't see why you need to know what is mentioned in the URL - the important thing is whether the system can use it to give you the file. If the getResource call can't find the file, it should return null. If it can find the file, you should be able to read from the file by getting an InputStream from the URL, like this:
    Java Code:
    URL url = getClass().getResource("abc.txt");
    InputStream is = url.openStream();
    ... etc.
    Have you actually tried this in Vista?

  7. #7
    zzpprk is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by dlorde View Post
    Firstly, apologies for my last post - the File code I posted won't work - I got confused :rolleyes:

    Secondly, I don't see why you need to know what is mentioned in the URL - the important thing is whether the system can use it to give you the file. If the getResource call can't find the file, it should return null. If it can find the file, you should be able to read from the file by getting an InputStream from the URL, like this:
    Java Code:
    URL url = getClass().getResource("abc.txt");
    InputStream is = url.openStream();
    ... etc.
    Have you actually tried this in Vista?
    That works fine on Vista an also on XP.
    Thanks very much.

Similar Threads

  1. loading resources in an applet
    By Gatts79 in forum Java Applets
    Replies: 5
    Last Post: 03-06-2009, 03:19 PM
  2. Protocol for loading resources from jar
    By SamSam in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2009, 08:14 PM
  3. Already import the resources but images are missing
    By Java.child in forum New To Java
    Replies: 5
    Last Post: 09-05-2008, 06:09 PM
  4. Problem loading resources.
    By jimm1 in forum Advanced Java
    Replies: 6
    Last Post: 06-23-2008, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •