如何在Java,Eclipse中找到当前项目的路径? [英] how to Locate the path of the Current project in Java, Eclipse?

查看:572
本文介绍了如何在Java,Eclipse中找到当前项目的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java编程方式找到当前运行/调试的项目的路径,我在Google中找到了,我发现是 System.getProperty(user.id),这没有得到我的项目路径。



我知道命令 Environment.currentDirectory in C#给出了当前运行/调试的项目的路径,所以我确信Java中还有一个类似的方式。



所以我问是否有人可以告诉或者给我一个代码,以编程方式找到当前运行/调试项目的路径?



编辑
i am writing eclipse启动加载的插件。
插件添加一个按钮到eclipse的工作台,在插件的代码
当我点击按钮我搜索当前的目录路径。


$ b $我的目的是,例如我调试一个项目,然后点击按钮,会弹出一个窗口,显示调试项目的路径,谢谢

解决方式

两种方式

  System.getProperty(user.dir); 

或这个

 code> File currentDirFile = new File(。); 
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0,helper.length - currentDirFile.getCanonicalPath()。length);

想法是使用。获取当前文件夹,然后获取绝对位置并从其中删除文件名,因此从

  / home / shark / eclipse / workspace / project / src / com / package / name / bin / Class.class 

当你删除Class.class你会得到



  / home / shark / eclipse / workspace / project / src / com / package / name / bin / 

这是你想要的。


I am trying to locate the path of the current running/debugged project programmatically in Java, I looked in Google and what I found was System.getProperty("user.id"), which didn't get me the project's path.

I know the command Environment.currentDirectory in C# gives the path of the current running/debugged project,so I am sure there must be a similar way in Java, too.

So I am asking if anyone could tell me or give me a code to how locate the path of the currently running/debugged project programmatically?

edit: i am writing plugin which loads with eclipse start. the plugin adds a button to the workbench of eclipse, with in the code of the plugin when i click the button i search for the current directory path.

my purpose is that when for example i debug a project and then click the button, a window will pop up presenting the debugged project's path, thanks

解决方案

Two ways

System.getProperty("user.dir");

or this

File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length - currentDirFile.getCanonicalPath().length);

The idea is to get the current folder with ".", and then fetch the absolute position to it and remove the filename from it, so from something like

/home/shark/eclipse/workspace/project/src/com/package/name/bin/Class.class

when you remove Class.class you'd get

/home/shark/eclipse/workspace/project/src/com/package/name/bin/

which is kinda what you want.

这篇关于如何在Java,Eclipse中找到当前项目的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆