Reputation: 64
I am using Eclipse Luna RCP. In the code of my plugin, I want the path of my workspace. I have used the following code:
String basePath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
This should give:
/home/esghsni/avroWorkspace/AvroDesigner
But it is give incorrect path which include path the directory where eclipse rcp is stored.
I have checked all the related questions but none helped. I have also tried decoding the path returned (using URLDecoder()
), but it gave some other path altogether.
Any kind of help would be appreciated. Thank you!!
Upvotes: 2
Views: 5989
Reputation: 1
Yes previous answer is correct
import java.io.File;
public class B {
public static void main (String [] arr)
{
B b=new B();
File f = new File(b.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
System.out.print(f.getAbsolutePath());
}
}
Upvotes: 0
Reputation: 64
Thank you for all the answers! I just executed the same code on my friend's laptop and it gave the correct workspace path. So basically, the code:
String basePath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
is working correctly. Seems as if there is some issues in my laptop or eclipse.
I also found out that when I switch the eclipse workspace and restart the eclipse, it gives me the correct workspace path (only once). But next executions onwards, it start giving me wrong output.
Upvotes: 0
Reputation: 111142
In an Eclipse plugin use:
IPath path = ResourcesPlugin.getWorkspace().getRoot().getLocation();
to get the full path of the workspace in file system.
Note: this must be run in a plugin, it won't work in a plain Java program.
Upvotes: 0
Reputation: 6515
you can try this:
System.out.println(System.getProperty("user.dir"));
Upvotes: 1
Reputation: 1565
File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
Use this file object f
to get absolute path of this
class.
Upvotes: 0