0

I'm using maven on centos 7, and starting with iText7. I followed the tutorial editing the pom file and Hello World project, but when I type

java -cp target/xxx-1.0-SNAPSHOT.jar com.itext.app.App

I got

java.lang.NoClassDefFoundError: com/itextpdf/layout/element/IBlockElement

Because I'm new to create Java project on linux, so I can't figure out where might be the problem.

Error message enter image description here enter image description here

Ben Ingle
  • 575
  • 2
  • 12
benben994
  • 40
  • 9
  • 1
    Please tell us which tutorial you are following. Please add your code and the pom file after your changes – Uladzimir Asipchuk Apr 15 '19 at 08:35
  • @UladzimirAsipchuk Hi, I've added the pom file and the code. The tutorial is on the website, as I remembered, the name is HelloWorld2.java – benben994 Apr 15 '19 at 08:43
  • It looks like you're trying to run from a built jar file. Is the jar file a fat jar? If not, it won't have iText on the classpath at runtime, and you would need to add those to the classpath when you add the built jar. – Ben Ingle Apr 16 '19 at 02:27
  • Hi @BenIngle, doesn't Maven will automatically download the jar file if I write the pom file? From my understanding...(I'm new to use maven with command) – benben994 Apr 16 '19 at 02:58
  • By default, Maven does not include dependencies in the jar. There are a number of plugins that do this for you though, most notably the Maven Assembly plugin and the Shade plugin. Check this SO question: https://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven – Ben Ingle Apr 16 '19 at 12:36

1 Answers1

0

My guess is your runtime jar does not include bundled iText, and iText is not being added to your classpath. Two options:

  1. Use Maven Assembly or Shade plugin to bundle a fat/uber jar. Maven does not bundle dependencies by default. By using one of these plugins, Maven will produce a jar that includes all dependencies that are specified in your POM. Then you can run your application with the same command line. See this SO question for details: Building a fat jar using maven

  2. Add iText to the classpath at runtime. Your command line would look like this:

java -cp /path/to/iText-7/;target/xxx-1.0-SNAPSHOT.jar com.itext.app.App

Ben Ingle
  • 575
  • 2
  • 12
  • As I mentioned, my command is: java -cp target/xxx-1.0-SNAPSHOT.jar com.itext.app.App, which seems is similar to your second method. But I'll try the other method, anyway, thanks! – benben994 Apr 17 '19 at 02:02
  • Hi @BenIngle, I tried your first solution and it worked perfectly!! Thanks for your advice. I was first thinking that the problem is due to itext library, but it seems to be dependency problem. Although I still not figure out how to correctly compile with second method, but the first method totally work! – benben994 Apr 17 '19 at 03:20
  • Glad I could help. For the second option, Java doesn't know about iText in your original question because Maven didn't put the iText jars in the build jar, and you didn't tell Java where to find it. My second solution tells Java where to find your iText jars that you either download manually from iText's website, or Maven downloads for you. Check here for setting the classpath: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html. And check here for finding jars downloaded by Maven: https://www.baeldung.com/maven-local-repository – Ben Ingle Apr 17 '19 at 18:56