0

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

layout Navigator view

enter image description here

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>
10
  • 2
    Maven can use any version of Java. You're misinformed on this - just configure the maven-compiler-plugin. Commented Oct 12, 2012 at 13:10
  • 1
    Did you added the necesary Maven dependencies in pom.xml? Commented Oct 12, 2012 at 13:21
  • Given how much has changed, it might be worth doing a Project > Clean from the Eclipse menus. Commented Oct 12, 2012 at 13:28
  • @MateiFlorescu as I said my project works fine before converting to Maven Commented Oct 12, 2012 at 13:30
  • 1
    Try right-click on the project -> Maven -> Update Project. Also check your run configuration, there is a tab called "Classpath" and there you must have an entry called "Maven Dependencies". Commented Oct 12, 2012 at 13:44

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have added <sourceDirectory>src</sourceDirectory> to POM and the build progresses to this point: "[ERROR] mypath/someproject/src/LMAXTrading/algos/HeartbeatClient.java:[3,19] package com.lmax.api does not exist." which is a package in the jar that is in MYLYBRARIES library (as per my screenshot above). Is there another source I am supposed to add to POM for that library to automatically get picked?
@jule64 you need to add those libraries as dependencies in your Maven POM. Read up the Maven docs on how to do that.
thanks! it works now. Wow that was painful. The Configure > Convert to Maven Project option really works about as well as a as a broken coffee machine. Thanks all for your helpful advices.
@jule64 How about clicking that little green tick then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.