简体   繁体   English

使用Java在Eclipse中打开文件

[英]Opening a file in Eclipse using Java

I would like to write a method that opens a file (eg XML) with the Eclipse XML editor. 我还想写与Eclipse XML编辑器打开一个文件(例如,XML)的方法。

I have an XML file in my workspace. 我的工作区中有一个XML文件。 Alternative route: right mouse click on the file > open with > xml editor . 替代方法: 右键单击文件>使用>打开> xml编辑器 I would like to achieve this programmatically using Java. 我想使用Java以编程方式实现这一目标。

You can use the following statement to run a command line command: 您可以使用以下语句来运行命令行命令:

Process p = Runtime.getRuntime().exec("eclipse.exe C:/path/to/your/file.xml");

Make sure that your Eclipse executable is part of your system's PATH variable or use the complete path in the command. 确保Eclipse可执行文件是系统PATH变量的一部分,或使用命令中的完整路径。

If Eclipse is you default XML program you can go for: 如果Eclipse是您的默认XML程序,则可以进行以下操作:

try {
    Desktop.getDesktop().open(new File("C:/Users/exampleUser/Desktop/test.xml"));
} catch (Exception e) {
    e.printStackTrace();
}

Otherwise go for TimoSta's solution. 否则,请寻求TimoSta的解决方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM