• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

root directory when deploying app

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic