Problems with URI in a jar file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Problems with URI in a jar file

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    10

    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:

    Code:
    URL url = getClass().getResource( fileName );
    File thisFile = null;
    try
    {
    	// Convert URL to URI and use to create File
    	thisFile = new File( url.toURI() );
    }
    catch ( URISyntaxException e )
    {
    	e.printStackTrace();
    }
    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)

    Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
    The top of the stack trace output refers to the line of code highlighted in red. Does anyone know what this error means?

    Thanks.
    Last edited by alcdotcom; 09-29-2005 at 07:23 AM.

  2. #2
    Join Date
    Sep 2005
    Posts
    10
    Ok, I've got more information. I inserted

    Code:
    System.out.println( url );
    to see what the path was and the output I got was:

    jar:file:/D:/Documents%20and%20Settings/someDirectory/test.jar!/dataObjects/Users
    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?

    Thanks

  3. #3
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    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..

    Code:
     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 09:53 AM.

  4. #4
    Join Date
    Sep 2005
    Posts
    10
    Quote Originally Posted by mr1yh1
    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..

    Code:
     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.
    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?

    Another question is, should I be taking a different approach and using external data files?

  5. #5
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    1-)you can build an BufferedReader from an InputStream .
    Code:
    InputStream in = ...
    BufferedReader x = 
           new BufferedReader( new InputStreamReader(in) );
    2-)i couldnt get an OutputStream ,
    i found this
    http://www.tek-tips.com/viewthread.cfm?qid=865431
    ( 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..
    http://java.sun.com/j2se/1.5.0/docs/...putStream.html

Similar Threads

  1. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 07:48 PM
  2. Creating Executeble Jar file
    By Sandeep in forum Java
    Replies: 0
    Last Post: 11-06-2001, 05:20 AM
  3. jar file
    By cesar in forum Java
    Replies: 0
    Last Post: 06-28-2001, 03:21 PM
  4. How to open a jar file
    By Lo in forum Java
    Replies: 4
    Last Post: 10-19-2000, 05:28 AM
  5. Replies: 2
    Last Post: 10-17-2000, 05:24 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center