如何在Java,Eclipse中找到当前项目的路径?

编程入门 行业动态 更新时间:2024-10-10 09:16:41
本文介绍了如何在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中找到当前项目的路径?

本文发布于:2023-11-08 20:49:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前项   目的   路径   中找到   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!