我创建了一个名为base的eclipse 4应用程序项目,并使用其base.product运行它。在使这个项目成为maven项目之后,我为apache commons-io库添加了一个依赖项,以便使用像这样的文件函数
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
System.out.println("se presiono "+btnNewButton.getText());
File file = new File("mi archivossss.txt");
try {
FileUtils.writeStringToFile(file, "prueba de archvio");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
当我尝试运行项目时,我收到以下异常:
java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
at base.parts.PrincipalPart$1.mouseDown(PrincipalPart.java:40)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:192)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1151)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:164)
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:382)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:236)
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:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
at org.eclipse.equinox.launcher.Main.main(Main.java:1438)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils cannot be found by base_1.0.0.qualifier
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:423)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:336)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:328)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
因此,我手动配置了依赖项,这是有效的,所以我知道这是一个与maven依赖项相关的配置问题。但是我不知道如何解决它。
确保Maven pom.xml中存在正确的依赖关系:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
确保m2e插件安装在Eclipse中。
在Eclipse中,选择项目文件夹并右键单击。转到弹出窗口中的Maven选项,然后在下一个对话框中选择“更新项目”,然后选择“确定”。这就是Maven如何将类放在类路径中的机器上。
关键在于您将此项目作为Eclipse应用程序运行。
假设您尝试添加maven依赖项的项目是Eclipse插件。
这不起作用,因为m2e无法处理插件依赖项。
为了能够在Eclipse中运行您的产品(即Run as ...> Eclipse Application),您需要从插件清单(Plug-in Dependencies)中将依赖关系添加为META-INF/MANIFEST.MF
。
或者,您可以使用Tycho构建产品。