Results 1 to 7 of 7
Thread: Resources
- 08-17-2009, 11:55 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 26
- Rep Power
- 0
Resources
Hi
I have the following code which returns two different things whether it is run on XP or Vista.
On XP it returns the full path to abc.txt inclucing the JAR file as followsJava Code:URL lURL = getClass().getResource("abc.txt");
On Vista the JAR file is missing and all the above call returns isJava Code:jar:localhost:8080/Folder/App.jar!/com/app/abc.txt
Any ideas why this is and how to fix it?Java Code:jar:/com/app/abc.txt
Regards
Patrick
- 08-17-2009, 02:56 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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 ?
- 08-17-2009, 03:01 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 26
- Rep Power
- 0
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
- 08-17-2009, 03:12 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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:
You can also get the file name and path directly from the URL via getFile() and getPath().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.
- 08-17-2009, 03:25 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 26
- Rep Power
- 0
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
- 08-18-2009, 12:36 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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:Have you actually tried this in Vista?Java Code:URL url = getClass().getResource("abc.txt"); InputStream is = url.openStream(); ... etc.
- 08-20-2009, 03:11 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
loading resources in an applet
By Gatts79 in forum Java AppletsReplies: 5Last Post: 03-06-2009, 02:19 PM -
Protocol for loading resources from jar
By SamSam in forum Advanced JavaReplies: 2Last Post: 02-12-2009, 07:14 PM -
Already import the resources but images are missing
By Java.child in forum New To JavaReplies: 5Last Post: 09-05-2008, 05:09 PM -
Problem loading resources.
By jimm1 in forum Advanced JavaReplies: 6Last Post: 06-23-2008, 07:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks