简体   繁体   中英

Eclipse: Getting the path for the current project through code? Sevlet, web application

I want to get the workspace folder of my current project dynamically. I tried

String path = System.getProperty("user.dir");

But it doesn't give me the workspace folder of my current project. It only shows the path of my eclipse. Can I get the path of my workspace without plugins or what?

Try something like:

 File file = new File(".");//current workspace directory from where eclipse runs java.
 System.out.println(file.getCanonicalPath());
 Output:
 <WorkspaceDirectory>\<projectName>

From an Eclipse plug-in you could write

String location = Platform.getLocation();

where Platform needs to be imported like this

import org.eclipse.core.runtime.Platform;

This would get you the location of your Eclipse workspace and you can work out the project from there by adding the project name to the path.

However you seem to want to obtain this location from the context of your servlet. Can you please explain why you need this? Servlets executing on Tomcat should not really access local files on the disk, because if you want to deploy them on a different server, you would not find consistent behaviour. For instance, if you need your servlet to access some files from your workspace, the best is to add them as resources to your web project as part of the web content.

You should probably also check this question (although you may have done it already): How to get to a local file in servlet

If this is not pointing you to the Eclipse workspace, it is probably because you are not launching Tomcat from your Eclipse, but rather using an external Tomcat instance. Is this correct?

Try to change run configuration --> argument. it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM