Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

root directory when deploying app  RSS feed

 
Edward Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an app that saves pdfs and images from a web page. The web sections send info to the server elements running in Java. I have hardcoded the path to where the images and pdfs need to be saved but on the server, these paths will be different. I'd prefer to just save them to something like:

whateverMyDeploymentDirectoryIs/files/pdfs

or something. How do I find out what my root directory is so that I can make the path relative instead of hard coded?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your architecture. This application -- does it get the files from the web page by connecting to it via HTTP, or in some other way? And then you said something about a server -- what server is that? The web server for that web page, or some other server where your application is going to be put for some reason?

As for "your" root directory, you ask as if you have no control over what it is. That would be true if you were talking about a web application, but not if your application was something else. In the latter case you just run your application in a script which first sets the current working directory (the correct terminology for what you call "root directory") before it runs the application.
 
Edward Young
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if it wasn't very clear. This app runs on an oracle server. The front end is flex which calls the java backend services, i.e. request a pdf to be created in a local directory on the web server and then give the user the link to download it.

I do not have any access to the oracle server. I simply upload the .war file via a web console. Originally, I asked the sys admins to give me the path the app was running in and I hardcoded it into the pdf and image generation code as a quick fix. The app was recently transferred to a new server and I would like to fix this so that in the future I don't have to change the path string every time the app moves to a new server. That is why I'd like Java to programmatically figure out what directory it is running in on its own.

I've tried just creating a dummy file:

File test = new File("");
String myPath = test.getAbsolutePath();

Do you think that will work?
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot assume that the "current directory" (working directory) is reliable in a J2EE environment.

The best way to maintain external content for a webapp is to create an external repository for it and configure the webapp to pull from it. For example, /var/lib/mywebapp/reports/pdf/.

You could hardcode such as location into the webapp, but I prefer something more flexibly, so I make it a JNDI variable defined in the webapp's web.xml. Here's a sample that defines a polling interval and its default value:


This value can then be retrieved by webapp code from the JNDI entry "java:comp/env/etc/pollinterval". A directory path would be the same way, except that you'd define the env-entry-type as a java.lang.String.

This is the default value, but many webapp servers can override it as part of the deployment process. For example, the Tomcat webapp server's server-specific deployment descriptor is a Context definition, and the override would look like this:



And of course, since you're talking about file storage, I will add the usual warning: NEVER write files into a WAR. It's a violation of the J2EE standards, won't work on all servers or all configuations, and has a high risk of losing important data. The /var/lib location is a common Linux convention, but the actual place to generate or upload files isn't really important, just that it should be external to the WAR and to the webapp server.

Note that for temporary files, you can use the java.io.File.mktmpfile() method to construct a temporary file and not have to know what directory the file is in, since the java.io.File element for the created file already knows it. In the case of Tomcat, the default location of that directory is in TOMCAT_HOME/temp, but it can be assigned anywhere. The webapp server is merely overriding the default JVM temp file directory location in any case.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!