Reputation: 225
I'm trying to run this java application with maven from command line on ubuntu with OpenJDK 13
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)
The project is created with Intellij Idea.
I guess I've made the build successfully with these commands
git clone https://github.com/danvega/httpclient-tutorial.git
cd httpclient-tutorial
mvn package
However, I don't know how to run the application from command line.
I tried these commands
cd target/classes
java dev.danvega.Application
and got this error
Error: Unable to initialize main class dev.danvega.Application
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
What am I missing?
Upvotes: 1
Views: 3905
Reputation: 72884
The third-party dependency that contains com/fasterxml/jackson/core/type/TypeReference
(which you can find the pom.xml com.fasterxml.jackson.core:jackson-databind
is required at both compile time and runtime. If you run using the java
command, you need to specify the dependency on the classpath. But since you are using Maven, there is an exec-maven-plugin
that you can use for convenience which will handle the classpath at runtime:
mvn exec:java -Dexec.mainClass="dev.danvega.Application"
You can also compile then run in the same command:
mvn package exec:java -Dexec.mainClass="dev.danvega.Application"
Upvotes: 1
Reputation: 2308
You started Java without specifying where the Maven dependencies can be found, which is called CLASSPATH (and since Java 9 also MODULEPATH if you use the Java Module System), similar how *.dll files can be found in the PATH on windows or LD_LIBRARY_PATH can be used for *.so on Unix-systems
Please see the great answers and questions from others before you:
I personally prefer either:
java -jar yourfat.jar
(or just click and it will start on Windows)Upvotes: 3
Reputation: 1148
Here, you have maven project which has one dependency on jackson-databind
which in turn will have some more dependencies i.e jackson-core
and jackson-annotations
.
Classes from these dependencies are not bundled in your application jar, so you cannot just run the Application
main class from your project directly using java
command, you need to specify the dependent classes on java classpath so that java can load these dependent classes of your program.
Since, it is a maven project, these dependent jars
will be pulled into maven
default directory (.m2
) into your's home path and as you mentioned, you are using ubuntu that will be /home/<your username>/
, For example your username which you are logged in with is singularli
then your home path must be /home/singularli
, you can also check it with echo $HOME
command.
So, you would find the maven folder, which stores all the jar(s), into your home /home/singularli/.m2/repository
, now here you would find jars like jackson-databind
, jackson-core
(these will be little inside subdirectories, as it keeps according to the package name, given below command example will give you more idea about it).
At last, once you find these jars
, you would need to specify the classpath using -cp
flag and include these jars with your application jar which would look like as given below:
java -cp "target/httpclient-tutorial-1.0-SNAPSHOT.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar" dev.danvega.Application
It should work the same way as shown in that video, you referred in your question.
Please notice that you may have different versions i.e com/fasterxml/jackson/core/jackson-annotations/2.11.4, I included 2.11.4 as an example, you may check the version in this project and include that, if different versions are there and you included anyone of them, it may cause some issue as some feature used in this project might not be present in that version
Upvotes: 1