Can run a Maven project within IntelliJ but not from Terminal - java

I have created a console application in IntelliJ that utilizes Maven. From within IntelliJ I can compile and run the app with no issues ...
From terminal however i execute the following commands (in the same dir with pom.xml)
mvn Install -U
java -classpath target/myApp-2.0-SNAPSHOT.jar MainClass
The install command seems to build the jar file without any issue. The second command gives me the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonParseException
In my pom.xml my dependencies are as follows
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
It seems to me that its not pulling in the Jackson Dependencies but im not sure what im missing here.

As said, the problem you have is that your jar needs the other jars to execute.
I see 3 solutions :
1- As stated above, when you run the program, add the -classpath argument
2- Use the maven-jar-plugin to add the dependencies in the manifest, then you'll only have to have the dependencies at the requested place to have all execute.
See http://maven.apache.org/shared/maven-archiver/examples/classpath.html#aAdd
3- Package the dependencies inside your jar with a plugin like jarjar : http://sonatype.github.io/jarjar-maven-plugin/
This will create you a standalone jar
Hope it can help.

Maven has no impact on your runtime classpath (only your compile-time classpath). You need to add your dependencies to the classpath.

Related

How to add local Maven package jar files to a Maven project

Summary
When using maven-install-plugin:install-file to install maven jar package all dependencies are ignored and this plugin write a dumb/broken pom file.
Context
I have a local Maven package jar file foo-java-1.2.3.jar which contains its own pom.xml in META-INF/maven/org.mizux.foo/foo-java/pom.xml with few dependencies
<dependencies>
<dependency>
<groupId>org.mizux.foo</groupId>
<artifactId>foo-linux-x86-64</artifactId>
<version>1.2.3</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.12.2</version>
</dependency>
</dependencies>
Then, I tried to install it locally using:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=foo-java.1.2.3.jar
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=foo-linux-x86-64.1.2.3.jar
ref: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
note: net.java.dev.jna:jna-platform and com.google.protobuf:protobuf-java can be found on maven central...
Then when using it in a Bar project pom.xml:
...
<dependencies>
<dependency>
<groupId>org.mizux.foo</groupId>
<artifactId>foo-java</artifactId>
<version>1.2.3</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
while mvn compile pass ?
I can't run my Bar's main since all transitive dependencies of foo-java are not passed to Bar so I got some java.lang.NoClassDefFoundError
mvn exec:java -Dexec.mainClass="org.mizux.bar.Test"
...
java.lang.NoClassDefFoundError: com/sun/jna/Platform
Looking at ~/.m2/repository/org/mizux/foo/foo-java/1.2.3/foo-java-1.2.3.pom this one seems to only contains the minimum (groupID, artifactID...) but the whole dependencies part is not present !!
questions:
How I can install local maven jar package so the pom installed is the full one and not a truncated version.
Why install plugin generate this dummy pom while the complete one is present inside the jar artifact ?
note: When using mvn install on a local build of foo-java the pom exported in .m2 is vastly different i.e. it's a copy of the foo-java original pom.xml !
i.e.
%diff ~/.m2/repository/org/mizux/foo/foo-java/1.2.3/foo-java-1.2.3.pom .../foo-java/pom.xml
note2: here a github project to reproduce the issue
https://github.com/Mizux/java-ortools you can take a look at action to see a full log https://github.com/Mizux/java-ortools/actions on linux/macos/windows and a docker ubuntu container
Why install plugin generate this dummy pom while the complete one is present inside the jar artifact ?
According to the doc
Generate a minimal POM for the artifact if none is supplied via the parameter pomFile. Defaults to true if there is no existing POM in the local repository yet.
ref: https://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html#generatePom
Now I don't know if we can force the maven-install-plugin to copy the pom.xml present in the jar file itself (ed which is already parsed by the plugin !!!)
Ugly workaround
I could extract the pom.xml from the .jar file (which contains it!) then pass it along the jar in the install-file command.
unzip -j foo-java-1.2.3.jar META-INF/maven/org.mizux.foo/foo-java/pom.xml
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=foo-java-1.2.3.jar -DpomFile=pom.xml
mvn compile
mvn exec:java -Dexec.mainClass="org.mizux.bar.Bar"
Since the feature you are expecting does not work:
Either use the latest version of the plugin (3.0.0-M1) to see if this is fixed
Either run maven in debug (mvnDebug -X) and place a break point in the line below; for all that matters, your JAR file is probably broken.
At the point where JAR entries are matched
Where the JAR entries are processed
Since you will have to debug in your IDE, you may also want to clone the GIT repository and switch to the tag as to match the version of the plugin you use.
Either extract the pom manually and pass it.

eclipse project has jar in referenced library not listed as dependency

I have a java project setup in eclipse to build with maven. Project itself is a multi-module maven project (but I am not using m2e plugin rather maven-eclipse plugin, and eclipse project does not have maven nature)
When running mvn install within eclipse, everything compiles fine, but when I run the same command in command prompt, I get compile errors due to a missing dependency.
I see that the jar it's looking for is neither listed explicitly as dependency in pom.xml or is a transitive dependency. I tried running mvn dependency:tree but also couldn't see this jar.
How can this jar be available in eclipse?
Update: two missing jars are
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jms</artifactId>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</dependency>
I solved the issue. Problem had to do with my invalid global mirror settings that redirected traffic to wrong remote repository so it could not find and install dependencies, later causing compile error.
In eclipse, I was using embedded maven referencing user settings only with no global settings therefore it worked fine.

JDBC driver not found while debugging with Intellij IDEA

I have a Maven project that I can successfully compile and run through the command line. When I manually deploy a jar with dependencies in Tomee it works. However when I run the same project through IDEA I get
java.lang.ClassNotFoundException: org.firebirdsql.jdbc.FBDriver
I think this is because my jar with dependencies has the jdbc driver included, but the IDEA run configuration deploys my classes and not the jar, so the driver is missing.
It seems to me that I need to somehow configure IDEA additionally even though I already have this in my pom:
<dependencies>
<dependency>
<groupId>org.firebirdsql.jdbc</groupId>
<artifactId>jaybird-jdk18</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
Am I indeed supposed to configure something else beside the pom?
I created the project without any IDE at first, because I was afraid it wouldn't work outside the IDE.

How to import a .jar in a Java's file without IDE and with Maven?

I can't compile the files directly. I use mvn package.
I can't run the files directly. I use storm (Apache).
I don't know much about Maven.
I tried to just put the .jar in the same folder as the code and use import com.path.of.jar. It did compile, but when I tried to run, gave a NoClassDefFoundError.
Try this way to add dependencies directly, like this:
<dependency>
<groupId>sample</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
When you work on a maven based project, you manage dependencies through the pom.xml file at the root of the project. POM stands for Project Object Model and contains information about the project and configuration details used by Maven to build the project (Introduction to the POM).
A maven project produces an artifact uniquely identified by its coordinates: The <groupId>, <artifactId> and <version> that you normally find at the top of your pom.xml file. Once an artifact is published to a repository other maven projects can depend on it.
If you look at the content of your POM file you should see a <dependencies> element containing all dependencies that your project needs. If you want to import classes from a jar in your code you will need to find the maven coordinates of this jar (for example on search.maven.org or mvnrepository.com).
Once you have the coordinates add a corresponding dependency section. It should look like this:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
Next time you run mvn package, the jar will be downloaded, used during compilation and packaged with your artifact.
And if you would like to get a good understanding of maven the following free book is excellent: Maven: The Complete Reference

IntelliJ not loading transitive dependency in maven project

I've made a small library, lets call it lib. It dependends on another library, sublib which is available in Maven central:
lib/pom.xml:
<dependencies>
<dependency>
<groupId>3rdparty</groupId>
<artifactId>sublib</artifactId>
<version>x</version>
</dependency>
</dependencies>
Now I'm trying to use lib in my project proj. I've set it as a dependency:
proj/pom.xml:
<dependencies>
<dependency>
<groupId>mynamespace</groupId>
<artifactId>lib</artifactId>
<version>y</version>
</dependency>
</dependencies>
When I run mvn exec:java -D exec.mainClass=mynamespace.proj.Main the program runs fine.
However if I run it from IntelliJ, I get the following error:
java.lang.NoClassDefFoundError: 3rdparty/SomeSubLibClass
at mynamespace.SomeLibClass.method(SomeLibClass.java:100)
This seems to indicate that IntelliJ does not load the transitive sublib dependency. How can I fix this?
You can manually right click on the pom.xml file in the file tree and select maven > reimport.
Sometimes you'll see a popup saying "Maven projects need to be imported"; you should select Enable Auto-Import.
This option can be found in Preferences > Maven > Importing > [x] Import Maven projects automatically (and is unchecked by default):
What worked for me was changing from using maven (Intellij) version and using my latest version that was installed on my machine previously.
I had a similar problem. The below command resolved the problem. It downloaded all the dependency jars into my IDEA project.
mvn -U idea:idea

Categories