简体   繁体   English

我可以在 Eclipse 的项目资源管理器中生成文件吗?

[英]Can I generate a file in the project explorer in Eclipse?

What I want to do is to write some information in a file, and then generate that file and make it visible right away inside a specific folder in the package explorer.我想要做的是在文件中写入一些信息,然后生成该文件并使其在 package 资源管理器的特定文件夹中立即可见。 Is that possible and if so how can I generate it exactly in the package explorer (I can do it in Desktop or wherever, ut I would like some insight on this specific case).这可能吗?如果可以,我怎样才能在 package 资源管理器中准确生成它(我可以在桌面或任何地方进行,但我想对这个特定情况有所了解)。

Thank you!谢谢!

That sounds like you should be writing an eclipse plugin.听起来您应该编写一个 eclipse 插件。 An app can do it to, but there are many caveats:一个应用程序可以做到这一点,但有很多警告:

  • It's incredibly convoluted code design.这是令人难以置信的复杂代码设计。 An app is written, and then compiled and packaged, and then run - when run there usually isn't an IDE at all, you run it elsewhere.编写一个应用程序,然后编译和打包,然后运行 - 运行时通常根本没有 IDE,您可以在其他地方运行它。 Conflating these notions and 'breaking the barrier' by having the app write data to 'the source folders' is bizarre.通过让应用程序将数据写入“源文件夹”来混淆这些概念并“打破障碍”是很奇怪的。
  • It's just not possible to do it without at least having to press F5 ('refresh') on the folder or project, unless you tell eclipse to use the filewatch system, but not all OSes even support it.除非您告诉 eclipse 使用 filewatch 系统,否则至少不必在文件夹或项目上按 F5(“刷新”),这是不可能的,但并非所有操作系统都支持它。

So how do I do it?那么我该怎么做呢?

Aspect 1: Find the location看点一:找位置

Given that the 'source' side and the 'compiled' side are supposed to be separate universes, there is no way to 'find' the source folder without hardcoding into your app either the location or the notion 'you were built in standard eclipse and are running within its runner/debugger, therefore, find your own classpath root, go up one, go down to the 'src' dir, and there you go.鉴于“源”端和“编译”端应该是独立的宇宙,如果不将位置或概念硬编码到您的应用程序中,就无法“找到”源文件夹,无论是位置还是概念“您是在标准 eclipse 中构建的,并且正在其运行程序/调试器中运行,因此,找到你自己的类路径根,go 向上一个,go 向下到“src”目录,然后是 Z34D1F91FB2E514B8576FAB1A7A7A5。 Make files in this location' - which is hardcoding a lot .在这个位置制作文件' - 这是很多硬编码。

Better option is to use a parameter: Start your application with a parameter or better yet a java environment variable with this data.更好的选择是使用参数:使用参数或更好的 java 环境变量与此数据启动您的应用程序。

Forget about eclipse for a moment, this is basic java:暂时忘记 eclipse,这是基本的 java:

class Test {
  public static void main(String[] args) {
    System.out.println(System.getProperty("hello.world"));
  }
}

If you then do on the command line:如果您在命令行上执行以下操作:

javac Test.java
java -Dhello.world=TopOfTheMorning Test

Then TopofTheMorning prints.然后TopofTheMorning打印。 In eclipse's run dialogs you can set VM parameters.在 Eclipse 的运行对话框中,您可以设置 VM 参数。 Set this one, and use eclipse's variables system to inject the right location:设置这个,并使用eclipse的变量系统注入正确的位置:

Menu: Run, Run Configurations...菜单:运行,运行配置...

pick the run config (or make one if needed), and set in the 'VM arguments' block of the 'Arguments' tab -Dsource.folder= , then click 'Variables...', and use container_loc or project_loc .选择运行配置(或根据需要制作一个),并在“参数”选项卡-Dsource.folder=的“VM 参数”块中进行设置,然后单击“变量...”,并使用container_locproject_loc You end up with something like:你最终会得到类似的东西:

-Dsource.folder=${project_loc:your.project.name}

Now, in your app, you can run System.getProperty("source.folder") and now you know where to write!现在,在您的应用程序中,您可以运行System.getProperty("source.folder")并且现在您知道在哪里编写了!

Aspect 2: Filewatch看点二:文件监视

Eclipse doesn't have a crystal ball, it doesn't know that a file has just appeared, written by another application (yes, eclipse is running that app, but it is nevertheless another app,). Eclipse 没有水晶球,它不知道刚刚出现的文件是由另一个应用程序编写的(是的,eclipse 正在运行该应用程序,但它仍然是另一个应用程序,)。 so that file isn't going to magically poof into existence in the Project Explorer, The usual route is to click on some parent of the location where the file now is, and hit F5, which tells eclipse to re-check the directory for new files.因此该文件不会神奇地出现在项目资源管理器中,通常的方法是单击文件现在所在位置的某个父级,然后按 F5,这会告诉 eclipse 重新检查目录是否有新的文件。 then it appears.然后它出现了。

However, advanced OSes have this concept called a 'file watcher', which lets you tell the OS, as an application: "Hey, can you give me a quick ping if a file appears in this directory?".然而,高级操作系统有一个称为“文件观察器”的概念,它可以让你告诉操作系统,作为一个应用程序:“嘿,如果文件出现在这个目录中,你能给我一个快速的 ping 吗?”。 Eclipse supports this – but your OS has to support it too! Eclipse 支持这一点——但你的操作系统也必须支持它!

In the menu: Window, Preferences...在菜单中:Window,首选项...

Then, General -> Workspace.然后,常规-> 工作区。 There, check the box with 'Refresh using native hooks or polling'.在那里,选中“使用本机挂钩或轮询刷新”复选框。

And then... pray.然后……祈祷。 That isn't guaranteed to actually work.这不能保证实际工作。 You don't have to write the app first, just make on the command line or in the finder or windows explorer or whatnot any file in the src structure, switch to eclipse, and check if the file just appears within a second or so.您不必先编写应用程序,只需在命令行或 finder 或 windows explorer 或src结构中的任何文件中创建,切换到 eclipse,并检查文件是否仅在一秒钟左右出现。 If not, that part just doesn't work and you're dead in the water: You must write an eclipse plugin to get this functionality.如果没有,那部分就不起作用了,你就死定了:你必须编写一个 eclipse 插件来获得这个功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用元素之一在Eclipse IDE的项目浏览器中更改* .xml文件的名称 - How can I change the name of *.xml file in the eclipse IDE's project explorer with one of its element Eclipse中缺少.project文件,但可以在Win Explorer中看到 - .project file missing in Eclipse but can be seen in Win Explorer 我在位置有一个属性文件(来自Eclipse Mars Project Explorer) - I have a properties file at location (from eclipse mars project explorer) 如何在Eclipse Java项目中自动生成.jar文件 - How Do I Automatically Generate A .jar File In An Eclipse Java Project 我可以生成日食工作区并手动/以编程方式进行投影吗? - Can I generate eclipse workspace and project manually/programmatically? 在Eclipse Android项目中的项目资源管理器中看不到文件 - Can't see files in project explorer in Eclipse android project 如何获取Eclipse项目资源管理器中的完整文件路径? - How to get full file path present in eclipse project explorer? Eclipse,在项目资源管理器视图中拥有具有文件属性的 TreeViewer - Eclipse, own TreeViewer with file attributes in Project Explorer View 在Eclipse Kepler Project Explorer中无法检测到SVN文件更改 - SVN file changes undetectable in Eclipse Kepler Project Explorer 从Eclipse项目资源管理器中删除Java文件和包中的警告图标 - Removing warning icons from Java file and packages in Eclipse project explorer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM