Results 1 to 16 of 16
  1. #1
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default How to load drl file dynamically within the Application

    Hi,

    I am developing one project.In that i created different packages.Each packages has lot of java files..
    What i am doing is,i want to load a drl file dynamically from java class..That file is somewhere in the project.
    My problem is,if i do in this way then i am getting null pointer exeception..that means that file is not loaded...
    i used the following piece of code..

    InputStream in=this.getClass().getClassLoader().getResourceAsS tream(" drl file name");

    InputStream in=Thread.currentThread().getContextClassLoader(). getResourceAsStream("drl file name");

    But both statements are not working....please anybody help me...


    Thanks in advance
    Prasad.vara

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Where is that file?
    Why are you using getClassLoader?
    Why not simply use the getResourceAsStream() method on Class?

  3. #3
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default

    Hi,
    All drl files are in com.abc.drl package... in cofig folder

    My current java class is in com.main.javafiles package... in src folder

    I used the simply this.getClass().getResourceAsStream() also...it also not working..


    Thanks...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Is the config folder on your path?
    (That will depend on what IDE you're using).
    If so then your file is "/com/abc/drl/<filename>", for the purposes of getResourceAsStream()...the one from Class that is.

  5. #5
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default

    yes.I have set the classpath for both src and config folders...I am using eclipse IDE..

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    And that applies to runtime as well?
    And did you use the file string I supplied, with the full path (and leading slash)?

  7. #7
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default

    i put only file name not full path even leading slash also...
    code is like as:

    getResourceAsStream("mytest.drl");
    file name is :mytest.drl
    this is in package com/abc/drl/mytest.drl

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Which is why it doesn't work.
    Have you tried with the full path name as I suggested?

  9. #9
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default

    if i try with the full path then it is working fine...But i want to load it by providing the file name only not full path...
    So, if i do as explained before then it is not working...can we load it by providing the file name only?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The file name only should work if the file is in the 'current directory' when the program executes.
    I have no idea where the current directory is when executing inside of an IDE.
    Create a File object and print out its absolute path:
    Java Code:
         File testFile = new File("Here");
         System.out.println("apPath=" + testFile.getAbsolutePath());
         //apPath=D:\JavaDevelopment\Testing\ForumQuestions5\Here

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Quote Originally Posted by prasad.vara View Post
    if i try with the full path then it is working fine...But i want to load it by providing the file name only not full path...
    So, if i do as explained before then it is not working...can we load it by providing the file name only?
    Then move the file.
    Why is adding the path difficult? It's just prefixing a constant value to the front of the filename string.

  12. #12
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default

    This time, I am using only one package( which contains drl files).In future, it will happen that using more than one package... Then we don't know that we can load which drl file from which package...That is why, I try to load the drl file with only file name not with full path..


    Thanks in advance.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    If you put the file in a package, then you'll need to use that to find the file.
    If the code that is trying to read the file is in the same package as the file, it may be able to read it with only the filename. Write a small simple program to test this.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Quote Originally Posted by prasad.vara View Post
    This time, I am using only one package( which contains drl files).In future, it will happen that using more than one package... Then we don't know that we can load which drl file from which package...That is why, I try to load the drl file with only file name not with full path..


    Thanks in advance.
    You need to know where the file is.
    If different files are in different directories then how on earth do you expect the system to know which directory to look in?
    Magic?

  15. #15
    prasad.vara is offline Member
    Join Date
    Jul 2010
    Location
    Hyderabad
    Posts
    21
    Rep Power
    0

    Default

    see the drl files have been divided to provide modularity for the project and have been put in more than one package. the task is to load the files from a class say RuleEngine, can u help me over this????

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Make a small test program with code in different packages that tries to read the file and print the first line of the file.
    Create several files with the same name but different contents so they can be identified when they are read and the first line is printed. Put versions of the files in all the folders corresponding to the packages.
    Execute the code and see what is printed out.

Similar Threads

  1. Replies: 1
    Last Post: 11-03-2009, 08:07 AM
  2. retain value of input type file in a jsp file while being dynamically generated
    By nidhi c in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 03:21 AM
  3. loading .properties file dynamically
    By nanaji in forum Advanced Java
    Replies: 3
    Last Post: 03-17-2009, 04:40 AM
  4. Dynamically loading the jsp page to the application
    By shekar in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-09-2008, 04:28 PM
  5. Replies: 0
    Last Post: 02-16-2008, 10: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
  •