0

I started a project with maven using the "quickstart" archetype. I then changed my POM to include neo4j:

https://github.com/ENCE688R/msrcs/blob/master/pom.xml

I added:

https://github.com/neo4j/neo4j/blob/master/community/embedded-examples/src/main/java/org/neo4j/examples/EmbeddedNeo4j.java

and ran

mvn package

This works with no errors, but

java -cp target/msrcs-1.0-SNAPSHOT.jar org.neo4j.examples.EmbeddedNeo4j

Returns the Error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/neo4j/graphdb/RelationshipType

What am I missing? At this point I simply need to test that I can include and use neo4j.

neatnick
  • 1,489
  • 1
  • 18
  • 29
David Prentiss
  • 548
  • 1
  • 6
  • 16

3 Answers3

2

use

mvn exec:java -Dexec.mainClass=org.neo4j.examples.EmbeddedNeo4j

there is also mvn dependency:copy that copies all dependencies to target/dependencies

and there is the mvn appassembler plugin that allows you to generate startup shell scripts that include all your dependencies as a classpath.

And last but not least there is the maven assembly plugin mvn assembly:single which generates a single jar file that you can run java -jar my-jar-file.jar

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
1

You need to add the Neo4j dependencies to your classpath as well. At the moment you're only adding the source jar you created. If you look at this POM you'll see that Neo4J examples require many other dependencies.

Find the libs directory where the dependencies have been downloaded (this may be in your local .m2 maven repo) and add these jars to your classpath. You do not need to add each jar one-by-one as you can simply add a directory with wildcards - ex:

Windows:

java -cp "target/msrcs-1.0-SNAPSHOT.jar;lib/*" org.neo4j.examples.EmbeddedNeo4j

Mac/Unix:

java -cp "target/msrcs-1.0-SNAPSHOT.jar:lib/*" org.neo4j.examples.EmbeddedNeo4j
Matt Wielbut
  • 2,584
  • 25
  • 29
  • java -cp target/msrcs-1.0-SNAPSHOT.jar:~/.m2/repository/org/neo4j/* org.neo4j.examples.EmbeddedNeo4j Is still giving the same error. (I did check that that directory exists.) – David Prentiss Apr 05 '13 at 12:24
  • Unfortunately, it's not that easy. Classpath entries can only contain a jar or a directory of jars. The path you added was a directory of directories (containing jars). Java won't recursively crawl a directory tree and pick up all the jars. You'll need to create a single directory containing all the dependency jars. Alternatively you can use something like the [maven-assembly-plugin](http://stackoverflow.com/questions/2022032/building-a-runnable-jar-with-maven-2) to collect all the dependencies for you and create a runnable jar. – Matt Wielbut Apr 05 '13 at 12:54
  • Can I ask what your end goal is? If it's simply to run this app - an IDE such as Eclipse will wrap this up nicely for you and handle setting the class path for dependency management. Alternatively, you can use the Maven exec plug-in to have Maven handle the classpath entries for you. [Here is a nice tutorial](http://redstack.wordpress.com/2010/12/22/configuring-maven-to-run-your-java-application/). – Matt Wielbut Apr 05 '13 at 12:54
  • Well, now I've tried to run it with every subdirectory of ~/.m2/repository/org/neo4j/ I can find and still no luck. Ultimately I need to write a java application with the embedded db provided by neo4j. I thought one of the purposes of maven was to handle dependencies. Either my understanding of maven or java dependencies is lacking. Probably both. Anyway, IDEs are not an option. I going to have to figure it out at the command line. – David Prentiss Apr 05 '13 at 13:38
  • How I don't miss the days when I had to use Maven. Ok - I just remembered you can use the Maven dependency plugin to output the dependency libs to a single directory - [quick search for an example gives me..](http://stackoverflow.com/questions/97640/force-maven2-to-copy-dependencies-into-target-lib) – Matt Wielbut Apr 05 '13 at 13:45
  • Then just run mvn install and it should spit everything out for you in nice little lib directory (a pretty bow is optional) – Matt Wielbut Apr 05 '13 at 13:47
  • I miss the days when I didn't have to use java at all. There is something fundamental I'm missing. Without the assembly plugin, would other users have to use maven to run my software? Anyway, it's a different question. Thanks for the help, mwielbut. – David Prentiss Apr 05 '13 at 14:00
  • Maven can assist will all aspects of development and deployment. It's up to the dev to decide how much to use it. It sounds like you're using it purely for dependency management and then you want to cut the cord and just use the naked command line. Both the assembly plugin and the dependency plugin allow you to do that. They extract the required code and dependencies and package it into a directory for you. From there you can go native command line - no more Maven. It's up to you how to deploy it at that point (you could zip up that directory or bundle it up into an EAR (or WAR if a webapp) – Matt Wielbut Apr 05 '13 at 14:19
1

I've started to work on some maven archetypes which could be a good starting point as well.

tstorms
  • 4,941
  • 1
  • 25
  • 47