Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中查找工作目录的基本路径_Java_Eclipse - Fatal编程技术网

在Java中查找工作目录的基本路径

在Java中查找工作目录的基本路径,java,eclipse,Java,Eclipse,我正在使用Eclipse Luna RCP。在我的插件代码中,我想要我的工作区的路径。我使用了以下代码: String basePath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); 这应提供: /home/esghsni/avroWorkspace/AvroDesigner 但它给出了不正确的路径,其中包括存储EclipseRCP的目录路径 我检查了所有相关的问题,但没有一个

我正在使用Eclipse Luna RCP。在我的插件代码中,我想要我的工作区的路径。我使用了以下代码:

String basePath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
这应提供:

/home/esghsni/avroWorkspace/AvroDesigner
但它给出了不正确的路径,其中包括存储EclipseRCP的目录路径

我检查了所有相关的问题,但没有一个有用。我还尝试对返回的路径进行解码(使用
urldecker()
),但它给出了一些其他路径。 任何形式的帮助都将不胜感激。谢谢

File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
使用此文件对象
f
获取该类的绝对路径。

您可以尝试以下操作:

 System.out.println(System.getProperty("user.dir"));

在Eclipse插件中,请使用:

IPath path = ResourcesPlugin.getWorkspace().getRoot().getLocation();
获取文件系统中工作区的完整路径


注意:这必须在插件中运行,在普通Java程序中不起作用。

是前面的答案是正确的

导入java.io.File;
公共B级{
公共静态void main(字符串[]arr)
{
B=新的B();
文件f=新文件(b.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
System.out.print(f.getAbsolutePath());
}
}

谢谢您的回答! 我只是在我朋友的笔记本电脑上执行了相同的代码,它给出了正确的工作区路径。因此,基本上,代码:

字符串basePath=this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()

工作正常。似乎我的笔记本电脑或eclipse出现了一些问题


我还发现,当我切换eclipse工作区并重新启动eclipse时,它会为我提供正确的工作区路径(仅一次)。但是下一次执行之后,它开始给我错误的输出。

ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();您在标题中提到了工作目录,但在问题中使用了workspace。您正在寻找安装插件的eclipse实例的工作区。对吗?在这种情况下,我先前的评论应该会有所帮助。您可以使用
File f=new File(“.”)随时检查您的位置;System.out.println(f.getPath())
这是根据eclipse的
user.dir
设置的路径设置创建的。这将在没有eclipse的情况下从命令行运行时产生不同的输出。我试过了,但结果是我的EclipseRCP存储路径(我的桌面)而不是我的工作区文件夹…你到底想要这个文件夹做什么?当您在Eclipse的其他实例中安装插件时,它可能不存在。