使用Eclipse Runtime Configuration启动jar文件

时间:2013-01-14 15:06:10

标签: eclipse eclipse-plugin

我需要使用Eclipse运行时配置(ILaunchConfiguration)启动Java程序。但是,我想提供程序作为.jar文件(作为插件的一部分)运行,而不是作为Eclipse项目。

似乎为了从Eclipse运行时配置启动Java程序,我需要指定一个项目(和主类)。

如何使用Configuration框架启动任意.jar文件?

1 个答案:

答案 0 :(得分:0)

本文有助于:

http://eclipse.org/articles/Article-Java-launch/launching-java.html

我使用以下代码运行我的插件.jar目录中的lib文件:

IPath path = new Path("lib" + File.separator + "some.jar");
Bundle bundle = Platform.getBundle(IDs.PLUGIN_ID);
URL url = FileLocator.find(bundle, path, null);
URI uri = FileLocator.resolve(url).toURI();
File file = URIUtil.toFile(uri);
IPath resolvedPath = new Path(file.toString());
IRuntimeClasspathEntry jar = JavaRuntime.newArchiveRuntimeClasspathEntry(resolvedPath);

IPath systemLibsPath = new Path(JavaRuntime.JRE_CONTAINER);
IRuntimeClasspathEntry systemLibsEntry =
JavaRuntime.newRuntimeContainerClasspathEntry(systemLibsPath, IRuntimeClasspathEntry.STANDARD_CLASSES);

List<String> classpath = new LinkedList<>();
classpath.add(aproveJar.getMemento());
classpath.add(systemLibsEntry.getMemento());

configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath);
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);