Results 1 to 6 of 6
  1. #1
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default ClassLoader getResource Problem

    Hello,

    I have txt file that I need to load automatically in my application, so the file name is specified directly in source code. For that happen I need to get relative path to txt file.

    resources variable in ant source, is a path to "target\classes\resources\someFiles\"
    ant code
    Java Code:
     <javac debug="true" includeantruntime="false"
               srcdir="${main-source}"
               destdir="${target-classes}">
    	  <classpath>
    		<pathelement path="${resources}"/>
          </classpath>
    When I run application in eclipse after configuring the class path, everything works fine, but when run the "executable" I get a FileNotFoundException

    java.io.FileNotFoundException: file:\D:\Projects\px\applications\myApp\lib.jar!fi le.txt
    (The filename, directory name, or volume label syntax is incorrect)

    This are two techniques that I used to get File.txt path.
    Java Code:
                    MainClass.getClassLoader().getResource("File.txt");
    		ClassLoader.getSystemResource("File.txt");
    what is the correct way to get the path of the file? Or what is correct way to register a ClassPath?

    Sorry for my bad english.
    Ubuntu, Linux for human Beings!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It looks as though your "file" is inside a jarfile, in which case you cannot use File, FileIbnputStream, FileOutputStream, FileReader, FileWriter, or any other "File" class on it. If you need to "read" it, use getResourceAsStream and use that InputStream. If you need to write to it, forget about it at this point in time as that is a lot more complicated.

  3. #3
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    No, I only need to read from it, but when I use getSystemResourceAsStream("File.txt") I not be able to instantiate BufferedReader from the InputStream. Is it normal? Do I need to use InputStream directly?
    I need to read file line by line, and it's more easier with BufferedReader.
    Ubuntu, Linux for human Beings!

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Check the API docs, there is an InputStreamReader class that you can use in place of your FileReader.

  5. #5
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    I have tried to use getSystemResourceAsStream(str) like you said and the return InputStream is null.

    I thnk there is a problem with classpath, so when I manually configure path in eclipse, it works fine. But after build the application, the path is suppose to be relative and contained in jar,


    Here is FilenotFoundException ouput:
    Java Code:
    java.io.FileNotFoundException: file:\D:\Projects\px\applications\myApp\lib.jar!file.txt
    (The filename, directory name, or volume label syntax is incorrect)
    is it normal to have that path?
    file:\D:\Projects\px\applications\myApp\lib.jar!file.txt
    Ubuntu, Linux for human Beings!

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You are still using some sort of "File" something or you would not be getting a FileNotFoundException. Like I said, use getResourceAsStream and plug that into an InputStreamReader. If the InputStream is null, then you have not entered the name correctly. Show your code.

Similar Threads

  1. Custome classloader problem
    By vijayabaskar in forum Advanced Java
    Replies: 10
    Last Post: 05-06-2009, 12:26 PM
  2. classloader
    By vijayabaskar in forum Advanced Java
    Replies: 8
    Last Post: 04-06-2009, 09:08 AM
  3. ClassLoader and JVM
    By Pradeen in forum New To Java
    Replies: 0
    Last Post: 01-19-2009, 07:04 PM
  4. Problem of Classloader in Eclipse Plugin Developemt.
    By Ashish Naidu in forum Eclipse
    Replies: 0
    Last Post: 09-12-2008, 11:16 AM
  5. eclipse classloader problem
    By sandor in forum Eclipse
    Replies: 2
    Last Post: 05-10-2007, 04:26 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
  •