Java ClassNotFoundException with maven dependency
Change provided
to compile
Provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
Maven dependency provided on submodule ear throws ClassNotFoundException
Because of transitive dependencies are not included when dependency is provided, so I had to restructure the modules this way.
Modules:
- app (pom)
- app-ear
- jasperreports (compiled) MOVED FROM APP-COMMONS
- app-back
- app-commons (provided)
- app-front
- app-commons (provided)
- app-commons
...
I was not able to change app-commons dependency to compiled because it is dependent of app-front and app-back. In app-commons as compiled I have an @ApplicationScope class that it would produce ambiguous Beans pointing to the class in app-commons because of jakartaee context management.
ClassNotFoundException with Maven dependency system scoped
If you read the Maven documentation about this scope, it seems the expected behavior if your application server doesn't provide this library at runtime:
This scope is similar to provided except that you have to provide the
JAR which contains it explicitly.
The provided
scope states :
This is much like compile, but indicates you expect the JDK or a
container to provide the dependency at runtime.
Not advised solution : add this library in the lib folder of your application server.
Cleaner solution : add this maven dependency in your maven repositories manually or with mvn install:install-file
.
And remove the system
scope of this dependency. It will use the default
scope.
NoClassDefFoundError on Maven dependency
when I try to run it, I get NoClassDefFoundError
Run it how? You're probably trying to run it with eclipse without having correctly imported your maven classpath. See the m2eclipse plugin for integrating maven with eclipse for that.
To verify that your maven config is correct, you could run your app with the exec plugin using:
mvn exec:java -D exec.mainClass=<your main class>
Update: First, regarding your error when running exec:java
, your main class is tr.edu.hacettepe.cs.b21127113.bil138_4.App
. When talking about class names, they're (almost) always dot-separated. The simple class name is just the last part: App
in your case. The fully-qualified name is the full package plus the simple class name, and that's what you give to maven or java when you want to run something. What you were trying to use was a file system path to a source file. That's an entirely different beast. A class name generally translates directly to a class file that's found in the class path, as compared to a source file in the file system. In your specific case, the class file in question would probably be at target/classes/tr/edu/hacettepe/cs/b21127113/bil138_4/App.class
because maven compiles to target/classes
, and java traditionally creates a directory for each level of packaging.
Your original problem is simply that you haven't put the Jackson jars on your class path. When you run a java program from the command line, you have to set the class path to let it know where it can load classes from. You've added your own jar, but not the other required ones. Your comment makes me think you don't understand how to manually build a class path. In short, the class path can have two things: directories containing class files and jars containing class files. Directories containing jars won't work. For more details on building a class path, see "Setting the class path" and the java and javac tool documentation.
Your class path would need to be at least, and without the line feeds:
target/bil138_4-0.0.1-SNAPSHOT.jar:
/home/utdemir/.m2/repository/org/codehaus/jackson/jackson-core-asl/1.9.6/jackson-core-asl-1.9.6.jar:
/home/utdemir/.m2/repository/org/codehaus/jackson/jackson-mapper-asl/1.9.6/jackson-mapper-asl-1.9.6.jar
Note that the separator on Windows is a semicolon (;).
I apologize for not noticing it sooner. The problem was sitting there in your original post, but I missed it.
Related Topics
How to Test Code Dependent on Environment Variables Using Junit
How to Check Json in Response Body With Mockmvc
Redirecting to a Servlet from a Jsp on Button Click
Null Pointer on an Autowired Bean Which Is Not Mocked by Mockito
Spring Rest - Create Zip File and Send It to the Client
How to Sort Date in Descending Order Using Comparator
How to Check Whether an Array Is Null/Empty
How to Split a String Between Letters and Digits (Or Between Digits and Letters)
Cannot Construct Instance of 'Java.Time.Localdate' - Spring Boot, Elasticseach, Jackson
How to Download a Pdf File in Chrome Using Selenium Webdriver
How to Close a Javafx Application on Window Close
Why Do I Get 404 for Rest With Spring-Boot
Get Unique Values from Arraylist in Java
Save Child Objects Automatically Using JPA Hibernate
Springboot 401 Unauthorized Even With Out Security