• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Martin Garrido
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Martin Garrido
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@bear: Can it be ,

just curious.
 
Martin Garrido
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic