-1

i'm trying to get the absolute path of my eclipse project : so i write this;

   File file= new File("");
   System.out.println(file.getCanonicalPath());
   projectPath=file.getCanonicalPath();

so it gives me the right path when i execute it in a class as java application C:\Documents and Settings\Administrateur\Bureau\ready code\JavaServerFacesProject

But when i try to use it in my web application it gives this :

C:\Documents and Settings\Administrateur\Bureau\eclipse\eclipse

So any idea how to do it Many thanks

Amira
  • 3,184
  • 13
  • 60
  • 95

2 Answers2

1

In Eclipse, you can set a Working Directory for your project in the Run Configurations dialog.

Have you tried setting a non-default one there?

(If you cannot set anything, you could try the JVM option suggested here: https://stackoverflow.com/a/7838900/1143126 )

Community
  • 1
  • 1
RobertG
  • 1,550
  • 1
  • 23
  • 42
1

Reading a file from a web application is a classical pitfall, whenever you do this you will suffer.

Web apps (especially Java EE) are not thought out to use the file-system for read/write operation, they rely on the fact that the container (in your case Tomcat) knows how to get the needed resources.

So the basic advice is : don't do it.

But since I basically hate this kind of answers I will give you some advice.

Never use the working directory

You never know where your working directory is, and, more often then not, in any production system, the web-app has no right to write on the working directory.

For example, if you deploy your webapp to a tomcat server, run as a service on a windows machine, you'll find that your working directory is \Windows\System32

You really don't want to write some uploaded files there for example...

You have a few option, what I prefer is to set a path into the web-xml and possibly override it from server configuration (using context).

Second option, even better is, to save a path into a db-table accessed by the web app.

ex:

in the web.xml you set

<context-param>
  <description>Uploaded files directory</description>
  <param-name>file-storage</param-name>
  <param-value>c:\app\storage\uploaded</param-value>
</context-param>

Then in the you server.xml (or you could use the context dir and place there a file with the following code) you override this setting in context.

<Context
    <Parameter
      name="file-storage"
      value="E:\app\storage\uploaded"
      type="java.lang.String"
      override="false" />
</Context>

Check tomcat documentation

Third option, in the slightly happier situation, you want to write just some temporary file, there is the webapp working dir accessible as a servlet context param named : javax.servlet.context.tempdir

If I where you I would go for the database table.

All this complexity is because you can have multiple instance of the same app on different instances of tomcat, even on different machines, and even different web application into the same instance, so there is no easy way to make a 'relative' path is all situations.

Most web-app resolve to serialize data on db (using lobs or similar objects), whenever there is such necessity, or to rely on some kind of service (FTP, cifs, nfs ...).

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
minus
  • 2,646
  • 15
  • 18
  • Second option, even better is, to save a path into a db-table accessed by the web app. what do you mean by "path" from what i understand you meant the absolute path of my web application ? C:\Documents and Settings\Administrateur\Bureau\ready code\JavaServerFacesProject – Amira Nov 29 '12 at 13:37
  • Yes, you store an absolute path into the web.xml file, and then you write and save file there. You can then override the string in the server configuration, to adapt the location of this directory as needed. – minus Nov 29 '12 at 13:57
  • Many thanks , this part is ok and i'll use it when uploading but after uploading the file i need to copy it in the source folder src/test/java in my eclipse project so for that i need the absolute path of my eclipse project which i can not ge it – Amira Nov 29 '12 at 14:06
  • 1
    Why in the source folder? The source folder is not even deployed. Are you uploading java classes? – minus Nov 29 '12 at 14:29