log4j package does not exist - java

I'm running mvn install, getting below error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile
error: package org.apache.log4j does not exist
In my project hierarchy I have log4j-1.2.15.jar added as a Referenced Libraries..not sure what I'm missing.
Part of POM file with log4j dependency (should I change the version to just say 1.2...or possible remove the scope below?):
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>1.2.15</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>1.2.15</version>
</dependency>

For log4j I only use this libraries:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

At first, it can be related with some your customizations of the environment or with dependencies in your *.pom file (Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin (default-compile) on project: Fatal error compiling: tools.jar not found). You need to check default installed "jre" to a jdk. Also ensure maven instalation and , namely paths: $JAVA_HOME and $M2_HOME
for example the paths adjustments when you have linux:
When I execute the command: echo $JAVA_HOME I will have /usr/lib/jvm/java-9-oracle and appropriate path for maven

For eclipse users this directly resolved this problem. There is no need to re-install anything with maven installed in eclipse oxygen centos 6. Vogella tutorial below says to resolve the dependency by adding the .jar via downloaded index at startup. As identified above answer 1.2.17 is around the same solution that worked for me (1.2.15) You must launch eclipse in a terminal with JAVA_HOME set as mentioned above. Eclipse will handle the rest.
How to use maven in eclipse
Identifies how to download the dependency list. Once you setup your project you open the pom gui and select Dependencies. You search for the above mentioned library select and add it. During initial build, the dependent .jar is downloaded and configured dynamically via the pom.xml state injection, this is why no manual deleting or re-install is necessary.
It is still not managed- To manage the dependency, i.e. add it to the pom, you select the item you just added and click manage and follow the prompts to add it the Dependency Management list. This is an automatic pom addition from the gui - `
<dependency>
<groupId>org.eclipse.ecf</groupId>
<artifactId>org.apache.log4j</artifactId>
<version>1.2.15.v201012070815</version>
</dependency>`
When the Dependency Management list is populated your library will display a (managed:version) tag in orange.
Start smiling, right click pom, run as maven build, the errors should be gone.

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.

Cannot resolve symbol 'Assertions' <-- Error message when trying to use AssertJ in IntelliJ

Similar to some other Questions, I find IntelliJ mysteriously refuses to recognize AssertJ library. I am asking again as (a) I have tried the various suggestions, and (b) I have a very simple example anyone can try themselves.
In IntelliJ 2018 and IntelliJ 2019 pre-release, I create a new project using the Maven archetype maven-archetype-quickstart version 1.4.
AssertJ 3 requires Java 8. So I changed these two lines in the POM for 1.7 to 11.
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
I add this to the POM:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
Using the Maven panel in IntelliJ, I executed a clean and install.
Seems good. I verify the org.assertj:assertj-core:3.11.1 library appears in the Project panel of IntelliJ. The app runs, with Hello World appearing on the console in IntelliJ.
In the App.java file, I add this import statement.
import static org.assertj.core.api.Assertions.* ;
Error reported in the IDE editor:
Cannot resolve symbol 'Assertions'
Some people suggest a corrupted Maven cache. So I quit IntelliJ, and I delete the .m2 folder in my home folder. I re-open my project in IntelliJ, and re-execute the Maven clean & install. Many things are downloading, so I know the Maven cache is indeed being recreated.
Yet, still the error in my editor, Cannot resolve symbol 'Assertions'.
No Java Modules involved, as the quickstart archetype has not yet been updated for that.
Delete <scope>test</scope>
This topic was addressed in a closed ticket # 520 on the AssertJ issue tracker.
When a Maven dependency carries a scope element with a value of test, that means you cannot use that library outside of your test-specific source package/folder.
If you are trying to call AssertJ from code in your example project’s src/main/java/… folder hierarchy, you will see that error. If you call AssertJ from src/test/java…, you will see success.
To enable AssertJ in the src/main/java/… folder hierarchy, delete the scope element in your POM dependency. So this:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
…becomes this:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
</dependency>

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.

Maven not downloading dependencies in Eclipse

I am setting up a project in eclipse . This projects builds successfully through command line(all mvn commands like mvn package, mvn compile, mvn clean install) work perfectly fine. While setting up this project on STS or Eclipse . I see some of the dependencies are not getting downloaded even though they are present in pom.xml. However, searching them in the maven repository and downloading the jar to my local computer and then adding them to build path makes it work on Eclipse.
Is there anything that we need to do to eclipse to make sure it downloads all the dependencies from the repository.
My POM:
<dependency>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server-compat410</artifactId>
<version>4.1.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server-compat420</artifactId>
<version>4.2.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.bookkeeper</groupId>
<artifactId>bookkeeper-server</artifactId>
</exclusion>
</exclusions>
</dependency>
Both these artifacts were not downloaded for eclipse and there jars found http://mvnrepository.com/artifact/org.apache.bookkeeper/bookkeeper-server-compat410/4.1.0 and http://mvnrepository.com/artifact/org.apache.bookkeeper/bookkeeper-server-compat420/4.2.0 were not present in the folder for MavenDependencies and were subsequently giving errors in Eclipse.
However manually adding them to the build path created a new folder (Reference Library) and resolved the Eclipse Errors . Why did Eclipse not download and import these dependencies by themselves from the maven repository ?? Is it a bug in Eclipse or some problem from my side . Please help.
I got the same problem and this is how i solved. :
Right click your project, choose Run As -> Maven install.
Observe the output console to see the installation progress. After
the installation is finished, you can continue to the next step.
Right click your Spring MVC project, choose Maven -> Update Project.
Choose your project and click OK. Wait until update process is
finished.
The error still yet, then do Project->Clean and then be sure you have selected our project directory and then do the follow Project->Build.
Solution 1:
Set correct proxy:
<proxy>
<id>optional</id>
<active>false</active>
<protocol>http</protocol>
<username></username>
<password></password>
<host>172.27.171.91</host>
<port>8080</port>
</proxy>
Solution2 :
just delete
lastupdated extension files from folder
and try updating maven.
[Most of the times this solution will work]
Sometimes there is an error downloading a dependency - eg. some files are downloaded but the actual JAR is missing from the local Maven repository.
In this case I had to delete the whole folder of the problematic dependency in the local maven repository. Only then did Maven update work (Right-click on the project and select Project > Maven > Update Project.... )
In my case, I had unchecked Build automatically. Checking it again started downloading the jars.
I have come across the same issue recently.
First of all you have to configure proxy settings in settings.xml in your maven repository.
If you are using eclipse/STS then please do verify following.
Window -> Preferences -> Maven -> User Settings -> update user settings by pointing your settings.xml
Now it's set to update the maven project. It worked for me.
I was facing similar sort of issue. I tried deleting folders inside .m2 and again building maven project.
I could download all dependency except one dependency which we have created by ourselves and published on Nexus.
Then I changed by java pointing from JRE to JDK which solved my problem
The following worked for me.
Just right-click on Project -> Maven -> Update Project... such as it is shown here.
I had faced a similar issue and following the below steps helped me fix it.
Delete the last modified jar from respective folders.
Select the project
Right Click -> Maven
Update project..
It will download all the missing Jars.
Try to move your dependencies from "type" tag to "scope" tag like below
or
<dependency>
<groupId>net.xyz.xyz</groupId>
<artifactId>xyz-xyz</artifactId>
<version>x.y.z</version>
<type>pom</type>
</dependency>
or
<dependency>
<groupId>net.xyz.xyz</groupId>
<artifactId>xyz-xyz</artifactId>
<version>x.y.z</version>
<scope>test</scope>
</dependency>
then further Maven > Update Project
For me I changed the packaging from pom to jar, and then the dependency got downloaded.
so I changed from <packaging>pom</packaging> to <packaging>jar</packaging>
Make sure you're defining the dependency as close as possible to the leaf of the project tree where it is needed. Otherwise, Maven might ignore it.
For example, if you have a parent project that references projects A and B and the dependency is with respect to project A, then defining the dependency in the parent's pom.xml might get ignored by Maven. So, define it in project A's pom.xml.
Parent Project's pom.xml
Sub-project A's pom.xml <<< define the dependency where it is needed
Sub-project B's pom.xml
I hope this helps someone as it took me 2 days to realize. I re-imported the project multiple times and followed every possible step I've seen online and in the end I had added a small piece of xml code within the pom.xml. Even though it wasn't erroring or even showing a warning it was preventing maven from reading the lifecycle-mappings.
Click into your pom.xml and go to the dependencies tab on the bottom left, if you see an error there it's likely your pom.xml is corrupted in some way and maven will never attempt to download the dependencies even though you won't get any real error. I had looked back at a previous PR and noticed where/what I added and removed it and was able to get maven to work.

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