简体   繁体   中英

NoClassDefFoundError after converting simple java project to maven project in eclipse

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.

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

  • 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)

  • THEN I do Maven > clean. I get build success

  • THEN Maven > Install. I get a build success

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

    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)

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

布局导航器视图

在此处输入图片说明

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>

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM