在Eclipse中将简单的Java项目转换为Maven项目后出现NoClassDefFoundError [英] NoClassDefFoundError after converting simple java project to maven project in eclipse

查看:117
本文介绍了在Eclipse中将简单的Java项目转换为Maven项目后出现NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在OSX(Java 1.6)上的eclipse中的标准Java项目中编码的类.该类使用另一个库中的类和接口.
我选择了该类,然后执行运行方式">"Java应用程序",一切正常.

I have a class that I coded in a standard java project in eclipse on OSX (Java 1.6). The class uses classes and interfaces from another library.
I select the class, then do Run As > Java Application and all works well.

之后,我尝试将我的项目作为Maven项目运行,事情开始变得有些沮丧..我总结了这里的所有步骤,希望有人能告诉我我做错了什么: -从标准Ja​​va项目中,我单击鼠标右键,然后进行配置">转换为Maven项目",然后单击芬兰语".到目前为止一切都很好.

After that I try to run my project as a Maven project and things start to get a little frustrating.. I summarise all the steps here hoping that someone will tell me what I am doing wrong: - From the standard java project I right click and did Configure > Convert to Maven project and clicked Finnish. All good so far.

  • 然后依次单击构建路径">配置构建路径",然后添加包含我的项目的文件夹.还是不错

  • Then Build Path > Configure Build Path > and add the folder that contains my project. Still good

自从我在SO的某个地方读到Maven使用JDK 1.5而不是1.6以来,我删除了所有@Override注释.无论如何,我都将其删除,并且我的危险信号消失了.至此,我的类看起来与原始Java项目中的类完全一样(除了我删除的@override之外)

THEN I remove all the @Override annotations since I read somewhere on SO that Maven uses JDK 1.5 instead of 1.6. Whatever, I remove them and my red flags go away. At this point my class looks exactly like in the original java project (except for the @override that I removed)

当我做Maven时>清洁.我获得了构建成功

THEN I do Maven > clean. I get build success

Maven之前>安装.我获得了构建成功

THEN Maven > Install. I get a build success

当我选择我的类并执行运行方式"> Java应用程序时,会得到以下难看的痕迹:

THEN I select my class and do Run As > Java Application and I get this ugly looking trace:

线程"main"中的异常java.lang.NoClassDefFoundError:LMAXTrading/algos/HeartbeatClient 引起原因:java.lang.ClassNotFoundException:LMAXTrading.algos.HeartbeatClient 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:202) 在java.security.AccessController.doPrivileged(本机方法) 在java.net.URLClassLoader.findClass(URLClassLoader.java:190) 在java.lang.ClassLoader.loadClass(ClassLoader.java:306) 在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301) 在java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Exception in thread "main" java.lang.NoClassDefFoundError: LMAXTrading/algos/HeartbeatClient Caused by: java.lang.ClassNotFoundException: LMAXTrading.algos.HeartbeatClient at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

我不知道从这里去哪里.您可以想象,我经历了很多试验和错误,并在SO和其他地方进行了搜索,以找到一种使之起作用的方法.但我只是不知道出什么问题了.因此,如果有人有想法,我就准备好倾听.

I don't know where to go from here. You can imagine that I went through a lot of trials and errors and searched on SO and elsewhere to find a way to get this to work. But I just cannot figure out what is wrong. So if someone has an idea I am so ready to listen.

我要从导航器"视图以及程序包资源管理器"视图中粘贴目录布局以下

I am pasting below my directory layout from the Navigator View as well as from the Package explorer view

这是POM.xml,我在其中添加了JRE配置

And here is the POM.xml where I have added the JRE config

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>someproject</groupId>
  <artifactId>someproject</artifactId>
  <version>1.0-SNAPSHOT</version>
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

推荐答案

项目目录结构与默认的Maven目录结构不匹配. Maven希望源位于根文件夹(包含pom.xml的文件夹)下的src/main/java目录中.来源在src文件夹中,因此出现No sources to compile错误.

The project directory structure does not match with the default Maven directory structure. Maven expects the source to be in src/main/java directory under the root folder (the folder containing pom.xml). The source here is in the src folder hence the No sources to compile error.

您可以移动源以使用默认的Maven目录结构,也可以通过将以下内容添加到pom的build部分中来更改pom.xml以显式指定源目录:

You can either move your sources to use the default Maven directory structure or change the pom.xml to explicitly specify your source directory by adding the following to the build section of the pom:

<sourceDirectory>${basedir}/src</sourceDirectory>

有关Maven标准目录布局的更多信息,请参见

More info on Maven's standard directory layouts can be found here.

这篇关于在Eclipse中将简单的Java项目转换为Maven项目后出现NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆