Problems with URI in a jar file
Part of an application that I've been working on gets a file from the local machine with the following code:
To simplify things, I've put the files in the same package directory as the classes that read and write to them. This works fine when run from Eclipse and when run from the command line. However, once I put all the package directories in a jar file with a manifest that specifies the main class I get the following error when I run it (using java -jar filename.jar)
URL url = getClass().getResource( fileName );
File thisFile = null;
// Convert URL to URI and use to create File
thisFile = new File( url.toURI() );
catch ( URISyntaxException e )
The top of the stack trace output refers to the line of code highlighted in red. Does anyone know what this error means?
Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
Last edited by alcdotcom; 09-29-2005 at 08:23 AM.
Ok, I've got more information. I inserted
to see what the path was and the output I got was:
System.out.println( url );
Here "test.jar" is the name of the jar file, "dataObjects" is the package directory, and "Users" is the name of the file I'm trying to use in the program. Something about this path is making the toURI method throw an exception. Anyone know why?
for me its consistent and logical,
if we got a File object from a jar,
this may be a headache,
becos jar file itself a file
and it can not include files or directories..
File f = new File("somepath\\somefile.jar");
System.out.print(f.isFile());// true its a file
f.listFiles();// return null, becos its not a directory.
if you need to write or read into/from a file ,
you will need streams ..
and document say:
getClass().getResourceAsStream(...) returns it.
Last edited by mr1yh1; 09-29-2005 at 10:53 AM.
I probably should have mentioned that these are text files. Does the stream still apply since I'm just using BufferedReader to red the file in and Formatter to write the file?
Originally Posted by mr1yh1
Another question is, should I be taking a different approach and using external data files?
1-)you can build an BufferedReader from an InputStream .
2-)i couldnt get an OutputStream ,
InputStream in = ...
BufferedReader x =
new BufferedReader( new InputStreamReader(in) );
i found this
( a file in a jar file, should be referenced by an URL)
so , it seems you can read , but can't write..
sorry for misleading ...
look at this API..
By jase_dukerider in forum C++
Last Post: 04-14-2005, 08:48 PM
Last Post: 11-06-2001, 06:20 AM
Last Post: 06-28-2001, 04:21 PM
Last Post: 10-19-2000, 06:28 AM
Last Post: 10-17-2000, 06:24 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center