Discussions

EJB design: How to Read files in relative folder deployed in EAR (HELP!)

  1. Hello,

    my deployment environment is Sun Java system, JBoss, Websphere, Weblogic as the application is inhouse tested on all possible App servers.

    In certain (POJO)class, in the EAR, I want to create a 'FILE' object by passing relative path of a folder containing XML files to the FILE CONSTRUCTOR.

    Say ...

    File myFile = new File("/config/resources/xml");
    File[] myFileArray = myFile.listFiles();

    ....work on the array and parse each file using XML parser.

    The issue, I am facing is, when I pass the relative folder path, it does not get resolved and always leads to NULLPOINTER.

    Can anybody help, how to resolve this thing.I was wondering if theres any API to access the
    EAR deployment directory (Win/*nix) independant and append the latter path it.

    Say...

    ${app.Dep.Dir}/config/resources/xml

    Thanks for your Help !

    VJ
  2. I'm no expert on this and am not pretending to know the answer.

    Look into Class.getResourceAsStream(...)

    This is used, for example, to load configuration files that are in the classpath. I've only ever used this with config files outside of jar/ear files, therefore, I'm not sure of the implication of your file being in an ear file and I'm not sure of the implication of your file being in a subdirectory of your classpath and not directly in your classpath.

    Hope that helps.

    As a follow up, I'm confused why your xml files are in the ear file. These types of files are usually used as configuration files and the permise behind these is that you can change them without recompiling or rebuilding your ear. Therefore, these files are usually in the directory along side the ear file and therefore are in the classpath (and can use Class.getResourceAsStream()). If you are going to requiere a recompile or rebuild of ear to change the config file consider adding the config info into a POJO class, maybe a singleton.
  3. This will return the path to the file(s?) you're looking for. Note: "ejbs" is the first level directory in my ejb.jar file.

    URL myURL = this.getClass().getClassLoader().getResource("ejbs/config/test.xml");
    if (myURL != null){
    return myURL.getPath();
    }else{
    return "the URL IS NULL";
    }
    Hope it helps.
    Randall Maier
    Versant Corporation.
  4. Thanks for your help Guys !

    I am gonna try this out today and let you know if alls OK.

    Peace
    VJ
  5. Thanks ![ Go to top ]

    Randall,

    Thanks, your tip has helped !

    Kris, maybe your suggestion is quite good.The XML's contain SQL Queries for external injection to the DAO. I wanted to avoid coupling of Java code with SQL queries. But your point is interesting as, the SQL's may require changes for optimization purpose and if the application is stopped, rebuilt and deployed as currently the XML's are part of EAR then, I clearly see this as not a very intellgient technique.Inspired from your idea, I will load the XML's external to the EAR and a mechanism introduced to detect changes in XML files by the application.

    Peace
    VJ
  6. hi VJ i am also having same requirement like as urs, i need to read xml files from a folder which is in my local system,based on the meta data in xml doc it has to take the documnts from other folder which is also reside in local system and have to upload in to VCM(vignette content management) so can u pl send me the java code which u have for more or less same type of functionality to my email krishnakiss at gmail dot com thanks in advance kk