Java Eclipse插件:传递命令行参数

Java Eclipse插件:传递命令行参数,java,eclipse,eclipse-plugin,Java,Eclipse,Eclipse Plugin,目前,我正在传递命令行参数以调用程序。我展示了一个小代码片段供您参考 public class Main { public static void main(String[] args) throws Exception { GlobalVariable.activity = args[0]; GlobalVariable.templatePath = args[1]; .... } 为了调用上面的程序,我从Eclipse

目前,我正在传递命令行参数以调用程序。我展示了一个小代码片段供您参考

public class Main {
    public static void main(String[] args) throws Exception {

        GlobalVariable.activity = args[0];
        GlobalVariable.templatePath = args[1];
      ....

        }
为了调用上面的程序,我从Eclipse传递以下命令。

现在,我正在尝试开发一个eclipse插件,从Java程序本身调用这个命令。为了开发插件,我做了以下工作:

  • Eclipse(新建->项目->Eclipse插件项目),项目名称为“
    org.example.helloworld
  • 我选择了“带弹出菜单的插件”模板
  • 现在,我已经在“
    NewAction.java
    ”中编写了以下代码,以将命令行参数传递给我的主文件,如上所示
  • 我的问题是-我无法调用Main.java文件。这种技术有什么问题?也欢迎使用任何其他替代方法。如果您需要更多信息以进一步澄清,请告诉我

    当我运行代码时,我在控制台上看到以下消息

    !SESSION 2015-11-15 14:04:18.580 -----------------------------------------------
    eclipse.buildId=4.5.1.M20150904-0015
    java.version=1.8.0_51
    java.vendor=Oracle Corporation
    BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
    Framework arguments:  -product org.eclipse.platform.ide
    Command-line arguments:  -product org.eclipse.platform.ide -data C:\Users\inpapat4\workspace/../runtime-EclipseApplication -dev file:C:/Users/inpapat4/workspace/.metadata/.plugins/org.eclipse.pde.core/Eclipse Application/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog
    
    !ENTRY org.eclipse.core.net 4 0 2015-11-15 14:04:36.102
    !MESSAGE WinHttp.GetProxyForUrl for pac failed with error 'The proxy auto-configuration script could not be downloaded
    ' #12167.
    
    !ENTRY org.eclipse.ui 4 0 2015-11-15 14:04:46.332
    !MESSAGE Unhandled event loop exception
    !STACK 0
    java.lang.NoClassDefFoundError: org/antlr/runtime/CharStream
        at org.example.helloworld.popup.actions.NewAction.run(NewAction.java:42)
        at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:247)
        at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:595)
        at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:511)
        at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:420)
        at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
        at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)
        at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4180)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3769)
        at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
        at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
        at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
        at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
        at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:654)
        at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
        at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:598)
        at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
        at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:139)
        at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
        at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
        at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
        at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
    Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.CharStream cannot be found by org.example.helloworld_1.0.0.qualifier
        at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
        at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 32 more
    
    MANIFEST.MF文件的内容如下:

    Bundle-ManifestVersion: 2
    Bundle-Name: Helloworld
    Bundle-SymbolicName: org.example.helloworld;singleton:=true
    Bundle-Version: 1.0.0.qualifier
    Bundle-Activator: org.example.helloworld.Activator
    Require-Bundle: org.eclipse.ui,
     org.eclipse.core.runtime,
     org.eclipse.core.resources
    Bundle-RequiredExecutionEnvironment: JavaSE-1.8
    Bundle-ActivationPolicy: lazy
    

    插件中所需的JAR必须在
    Bundle类路径
    条目的MANIFEST.MF中列出

    为此,请打开MANIFEST.MF编辑器,在“运行时”选项卡上将JAR添加到“类路径”部分

    结果可能类似于:

    “.”项用于插件文件,然后在“lib”文件夹中有3个jar


    还要确保jar列在“build.properties”文件中,以便它们包含在最终的插件jar中。

    当您运行上述代码时会发生什么?感谢Elliott Frisch的评论。为了回答您的问题,我正在用控制台上看到的消息更新问题。那么到底是什么问题?代码编译了吗?你知道吗你运行时会遇到异常吗?@greg-449:我已经用运行此代码时在控制台上看到的错误消息更新了问题。因此,你没有在插件中包含运行代码所需的所有jar,特别是包含
    org.antlr.runtime.CharStream
    的jar。非常感谢你解决了这个问题!这已经完成现在解决:)
    Bundle-ManifestVersion: 2
    Bundle-Name: Helloworld
    Bundle-SymbolicName: org.example.helloworld;singleton:=true
    Bundle-Version: 1.0.0.qualifier
    Bundle-Activator: org.example.helloworld.Activator
    Require-Bundle: org.eclipse.ui,
     org.eclipse.core.runtime,
     org.eclipse.core.resources
    Bundle-RequiredExecutionEnvironment: JavaSE-1.8
    Bundle-ActivationPolicy: lazy