是否可以创建命令行 JDT 应用程序?

发布于 2024-08-15 03:20:37 字数 313 浏览 3 评论 0原文

我想创建一个命令行应用程序来分析 Java 代码。 Eclipse JDT 似乎是完成这项工作的正确工具,但是我在 JDT 上找到的每个教程都将 JDT 作为 Eclipse 插件启动。

我期待这样的事情:

public static void main(String[] args) throws Exception {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    ...
}

开始。但是 getWorkspace() 会抛出服务未启动的异常。

I want to create a command line application which does analysis of Java code. The Eclipse JDT seems like the right tool for the job, however every tutorial I can find on the JDT starts up the JDT as an Eclipse plugin.

I would expect something like this:

public static void main(String[] args) throws Exception {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    ...
}

to get started. However getWorkspace() throws an exception that the service is not started.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-08-22 03:20:37

如果你想利用 JDT,你必须启动 eclipse。您可以使用扩展点“org.eclipse.core.runtime.applications”来创建从命令行启动的最小应用程序。

  1. 创建一个新的插件项目。
  2. 将“org.eclipse.core.runtime”和“org.eclipse.core.resources”添加到依赖项中。
  3. 为“org.eclipse.core.runtime.applications”创建扩展。
  4. 创建一个实现“org.eclipse.equinox.app.IApplication”的类并在您的扩展中引用它。

我的plugin.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         id="id2"
         point="org.eclipse.core.runtime.applications">
      <application
            cardinality="singleton-global"
            thread="main"
            visible="true">
         <run class="testapplication.Application1">
         </run>
      </application>
   </extension>
</plugin>

MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TestApplication
Bundle-SymbolicName: TestApplication;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: testapplication.Activator
Require-Bundle: org.eclipse.core.runtime,
 org.eclipse.core.resources
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

Application1.java:

package testapplication;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class Application1 implements IApplication {

    @Override
    public Object start(IApplicationContext context) throws Exception {
        System.out.println("Hello eclipse at "
                + ResourcesPlugin.getWorkspace().getRoot().getRawLocation());
        return IApplication.EXIT_OK;
    }

    @Override
    public void stop() {
        // nothing to do at the moment
    }

}

输出为:

Hello eclipse,位于 D:/Arne/workspaces/runtime-TestApplication.id2

If you want to leverage JDT you have to start eclipse. You can use the extension point "org.eclipse.core.runtime.applications" to create a minimal application that starts from the command line.

  1. Create a new Plugin-Project.
  2. Add "org.eclipse.core.runtime" and "org.eclipse.core.resources" to the dependencies.
  3. Create an extension for "org.eclipse.core.runtime.applications".
  4. Create a class that implements "org.eclipse.equinox.app.IApplication" and reference it in your extension.

My plugin.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         id="id2"
         point="org.eclipse.core.runtime.applications">
      <application
            cardinality="singleton-global"
            thread="main"
            visible="true">
         <run class="testapplication.Application1">
         </run>
      </application>
   </extension>
</plugin>

MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TestApplication
Bundle-SymbolicName: TestApplication;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: testapplication.Activator
Require-Bundle: org.eclipse.core.runtime,
 org.eclipse.core.resources
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

Application1.java:

package testapplication;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class Application1 implements IApplication {

    @Override
    public Object start(IApplicationContext context) throws Exception {
        System.out.println("Hello eclipse at "
                + ResourcesPlugin.getWorkspace().getRoot().getRawLocation());
        return IApplication.EXIT_OK;
    }

    @Override
    public void stop() {
        // nothing to do at the moment
    }

}

Output is:

Hello eclipse at D:/Arne/workspaces/runtime-TestApplication.id2

稚然 2024-08-22 03:20:37

您需要确保首先启动了 Eclipse。使用 EclipseStarter 类来运行,然后您可以使用 ResourcesPlugin 上的方法。

You want to make sure you have started Eclipse first. Use the EclipseStarter class to get things running and then you can use the methods on ResourcesPlugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文