• Post Reply Bookmark Topic Watch Topic
  • New Topic

web app : external jar, props file

 
Edmund Castermund
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,\

I have a webapp I'm developing. I use eclipse, and I have the core logic in its own project which I import as a jar to the web tier project. The core logic jar uses relative file paths for some of the resources the project uses. When on my linux box, that file path is in Web Content/WEB-INF. But when working on my mac it's something else (don't have my mac right in front of me, can't give exact example.)

So, my question is, how do I read a properties or xml config file and pass its values to the core logic process so that the relative file paths go to the same place? Instead of using relative paths, should I set files paths to something like


where LIB_DIR is set from a props file?
thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file is within the web app you should be using getResourceAsStream() rather than fiddling around with file paths.
 
Edmund Castermund
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i st tje properties file in that way will tje values carry over to all dependant jars in the classpath?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using getResourceAsStream() the path is relative from the context root and should be the same everywhere.
 
Edmund Castermund
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha, thanks
 
Edmund Castermund
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
working some more on this. In my webapp project, I have a properties file, config.properties located right under the src folder, next to the log4j.properties file. (log4j works ok)

The webapp project imports another project that also has its own props file. So, a few followup Q's.

I am attempting to load the props file this way. The code to load the props file is not in the webapp project, but the core logic project.




It's throwing this error :




Also, I am working on my mac. When I work on the project on my linux machine I do not have this problem.

  • The ClassLoader.getResourceAsStream(FILENAME) call above does not have any path info, just the filename. I thought that since the props file is located in the 'src' folder, that's where ClassLoader would look. How do I tell what dir ClassLoader is looking in for the file? (and how can I reset it?)
  • Since the webapp project and the core:logic both have props files, which one will be loaded?
  • Right now I have the core:logic project imported to the webapp build path as a project, but I also exported a jar under WEB-INF. Is there anyway to just import the project to the build path?



  • thanks
     
    Paul Clapham
    Sheriff
    Posts: 21867
    36
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would suggest not using the system classloader (which I think your code must be doing, I've never seen that version before). Try using the same classloader as the classes in your application:

     
    Paul Clapham
    Sheriff
    Posts: 21867
    36
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As for the src folder, that's a red herring. You shouldn't be thinking in terms of how things are arranged in your development environment, you should be thinking in terms of how they are arranged in your deployed web app.
     
    Edmund Castermund
    Ranch Hand
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also wanted to mention that this issue was introduced when upgrading to yosemite on the mac. Do not have this issue when working in linux.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65519
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a red herring. Properly written, a web app will work on whatever platform it is deployed.
     
    Edmund Castermund
    Ranch Hand
    Posts: 102
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I managed to figure it out in the short term. I created a class similar to in the package.
    then, I placed my config.properties file in src/com/myapp/util, and the props class was able to read with

    Is it a best practice to place the properties file in that way? The advantage is that with the props file in the same location as the class, I can always easily get to it. But isn't the entire point of a properties file to be able to edit it separate from the application? If the props file is in the jar, what is the advantage to just having a java class that sets all the properties?
    And if I want to override the properties file specific to my web project?
     
    Paul Clapham
    Sheriff
    Posts: 21867
    36
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Normally I do this:



    And then the properties file gets put in the root of one of my classpath entries. Leaving off the slash means it's relative to (in your case) the MyProperties class, which is why putting it in the same directory as that class works.

    You wrote:But isn't the entire point of a properties file to be able to edit it separate from the application?


    Not really, no. It's normal (I might even say it's usual) for a properties file to be read-only, especially in a web application. You'll notice that the getResourceAsStream method doesn't have a corresponding method which lets you write a resource, and that's because resources are typically read-only.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!