aspose file tools*
The moose likes JSP and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark ""current working directory" of my Tomcat of Linux is $HOME?" Watch ""current working directory" of my Tomcat of Linux is $HOME?" New topic
Author

"current working directory" of my Tomcat of Linux is $HOME?

Martin Garrido
Ranch Hand

Joined: Dec 04, 2010
Posts: 31
I have in my /classes a sentence new File("webapps/SNMP/WEB-INF/xsl/oid.xml")

...in windows it works.

But in linux, the log of catalina.out is:
java.io.FileNotFoundException: /home/sun/webapps/SNMP/WEB-INF/xsl/oid.xml (No such file or directory)


any help?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Web applications have no concept of a "current working directory", so what that is set to is random. Always use absolute file references for files that are outside of the web app. Within the web app, if (and only if) the app is not deployed as an unexploded war, ServletContext.getRealPath() can be used to find files.

But the preferred way to find files within the web app is getResourceAsStream().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Martin Garrido
Ranch Hand

Joined: Dec 04, 2010
Posts: 31
Bear Bibeault wrote:Web applications have no concept of a "current working directory", so what that is set to is random. Always use absolute file references for files that are outside of the web app. Within the web app, if (and only if) the app is not deployed as an unexploded war, ServletContext.getRealPath() can be used to find files.

But the preferred way to find files within the web app is getResourceAsStream().



.....it's a Javabean
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Martin Garrido wrote:.....it's a Javabean

Not making sense. According to your first post, its an XML file.

Or are you saying (in which case, please take the time to be clear) that the code is in a JavaBean?

If the latter, so what? getResourceAsStream() can locate resources on the class path.
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
@bear: Can it be ,

just curious.
Martin Garrido
Ranch Hand

Joined: Dec 04, 2010
Posts: 31
Bear Bibeault wrote:
Martin Garrido wrote:.....it's a Javabean

Not making sense. According to your first post, its an XML file.

Or are you saying (in which case, please take the time to be clear) that the code is in a JavaBean?

If the latter, so what? getResourceAsStream() can locate resources on the class path.



yes, it's the latter.

" new File("webapps/SNMP/WEB-INF/xsl/oid.xml")" is inside a class Obt, but the real bean is MyBean that use Obt.

So my solution is:

myjsp.jsp:
<jsp:setProperty name="bbdd_id" property="oidxml" value="<%= application.getRealPath( \"WEB-INF/xsl/oid.xml\" ) %>"/>

Mybean:
setOidxml(....
...

Obt.infoDOM( oidxml );




and finally Obt.java:
new File( oidxml )



any suggestion? (and finally the asnwer of my initial question is yes?)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

That's just begging for trouble. It assumes certain behaviours from the webapp server than are not guaranteed by the J2EE standard, so even if it works today, it can break at any time without advance warning. And I do mean ANY time - even without changing the server software.

Also, putting backslashes in filename paths in Java is extremely dangerous, since they may be interpreted as escape characters instead of text. Use forward slashes, even in Windows.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Listen to Tim, for he is wise. Doing File I/O is a minefield and sooner or later, your leg is going to get blown off.

If the file is on the classpath, use the class loader's getResourceAsStream(); if the file is under WEB-INF, use the servlet contact's getResourceAsStream(). ANy other path is likely to result in missing feet.
 
 
subject: "current working directory" of my Tomcat of Linux is $HOME?