Maven, package does not exist - java

I have a module whose pom file is:
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>common module</name>
In that artifact ('common'), I have a package named com.mycompany.common.objects. In the consuming package, my pom file is:
<dependency>
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
When I run mvn install it always complain: package com.mycompany.common.objects does not exist.
I tried explicit importing in the class where the error was:
import com.mycompany.common.objects
No luck. I tried in both the IDE (IntelliJ) and in commandline. Any idea? thanks

While working with IntellijIDEA, generated files can cause this issue. Writing
mvn idea:idea
on IntellijIDEA Maven console to reset those files did the trick for me. Also, see:
Package doesn't exist error in intelliJ

From your sample, we cannot see any artifact containing the package com.mycompany.common.objects you are using.
You are adding dependency com.mycompany.Common:common as a POM (and you are declaring the packaging of com.mycompany.Common:common as POM too). If it is actually a JAR artifact that contains the package you need to use, then remove the packaging from the POM and dependency (which means, using default which is JAR).

For anyone struggling with this and not familiar with java, make sure that the said package exists in your local repository. Maven has a local repository ~/.m2 where the packages are installed for local access, so even if your dependency package is correctly declared as a dependency in pom.xml and is compiled and exists in your project, if it does not exist in the local repository, the mvn compile will trigger a "package does not exist" error.
To fix this:
In the missing package folder, do:
mvn install //--> this will package and install your missing package in the local repo
Then in your project that you wanted to compile:
mvn compile // --> now that the missing package is in the local repo it should work

Please correct me, If I'm wrong. I understand that the common is a POM that defines several dependencies which intents to be used by other modules. The Importing Dependencies may meet your requirement.
For example
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I hope this may help.

I had the same problem recently. Everything in my project was setup correctly with dependencies etc. I tried removing /target dirs but nothing worked.
Finally, I solved it by removing the Module Dependency from my dependent project and then readding the dependency. Not sure what is going on in the background, but some sort of refresh of the classpath must have been made. Perhaps the problem was due to the Maven setup.
Hope it helps someone who reaches this question from a search engine.

Not sure if there was file corruption or what, but after confirming proper pom configuration I was able to resolve this issue by deleting the jar from my local m2 repository, forcing Maven to download it again when I ran the tests.

For me the problem was with the sourceDirectory and testSourceDirectory nodes in my pom.xml.
I was using
<sourceDirectory>${basedir}/src/test</sourceDirectory>
<testSourceDirectory>${basedir}/test</testSourceDirectory>
and changed it to
<sourceDirectory>../src/test/java</sourceDirectory>
<testSourceDirectory>../src/test/java</testSourceDirectory>

Your IDE (Eclipse in my case) may not distinguish between compile and runtime scope. This means that the IDE will let you use runtime scope dependencies in your code, but maven won't. In such a such change the dependency scope from runtime to compile.

you need to add the maven-plugin into (each) child module (for compiling main and test source)
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugins>
and then you add the plugin-management into the parent pom, for centralizing the plugin config (version...)
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</pluginManagement>
Then you can add your dependency into the dependent module pom
<dependency>
<groupId>com.mycompany.Common</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
http://www.jouvinio.net/wiki/index.php/Projet_Maven_multi-modules

Related

Can't resolve the error: java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException

Trying to use Maven to organize my project and I keep running into the following error. I know that this error means the file is present at compile time but for some reason it can't be found at runtime.
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
So I'm working on a project in Java that will take a user query, search Google Images and then download some of the results onto my computer. To that end I've had to use some third party libraries like JSoup, Json-Simple, and Gson.
I initially added Jsoup to my classpath manually and it worked, but then I heard about Maven and started using it instead. My issue is that when I try to run my code I get the error above.
I'm just not sure how to resolve this. I've seen a bunch of other posts about similar errors and I've tried to modify my pom.xml accordingly but
I just can't get it to work. I've tried removing the ~/.m2 file, ran mvn clean, mvn install, mvn package, mvn compile, and it all works fine. But when it comes time to run, I keep getting that error.
Here's most of my pom.xml file.
<repositories>
<repository>
<id>central</id>
<name>Maven repository</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- jsoup HTML parser library # https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
FOUND SOLUTION: So I left out some parts of the pom.xml file to make it easier to read, and because all the other parts were generated by Maven itself so I figured there couldn't be an issue with anything there. But it adds a tag called "pluginManagement" that encloses all other plugins and apparently this does not allow the Shade plugin to run.
Between ngueno's guidance and this post Maven shade plugin is not called automatically for goal "package"
I was able to figure out my issue, though I'm still not entirely sure why it is an issue. Anyways, I figured I'd update this post in case someone else with a similar problem stumbles across it. This was on Mac OS btw, in case it makes a difference. Thanks for your help everyone.
Usually NoClassDefFound errors are related to missing libraries at runtime.
Since you are running using the terminal I supose you are building your project using Maven, and running the generated JAR,
I would recommend to you to use the maven-shade-plugin and generate an uber-jar as I explained on this question.
The purpose generating a uber-jar is to carry all the needed dependencies inside of it (available on the application classpath).
Implement the plugin and try to run using the new JAR.
PS: Remember to check this section related to Executable Jars
UPDATE: Remove the <scope>provided</scope> of your jsoup dependency, to enforce Maven to package it along your app, with the provided scope you are saying that this dependency will be provided by the JDK at runtime.
The jars that you identify in your dependencies must be present in the Runtime classpath.
This is not the classpath that is available when you compile the code;
it is the classpath on the host where you run the application.
You must install these jars on the target host.
Edit: More details
You must do the following:
Identify the runtime host.
Create a directory on the runtime host into which you will install the dependent jar files.
Include every jar in the classpath.
Consider abandoning the "roll-your-own" path.
If you use Spring Boot
(I like it,
I don't work for them).
One feature of spring boot is a reinvented "Fat Jar" that will include the dependencies inside one deliverable artifact (the fat jar) and will add them to the classpath at startup.
Edit:
The Spring boot executable jar file is not a "Fat Jar",
instead it includes the dependencies in a directory in the
executable jar and adds said jars to the classpath on startup.

The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files

I am following the tutorial at spring.io to build a spring app using spring boot.
I can get the program to run perfectly on one computer.
When I try on a different computer I get the following error
The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files
I have tried deleting and adding my JRE Systems Library (JDK 1.8), as well as cleaning and updating the project using maven, and even deleting and re-importing the entire project. All of these methods have shown no success.
My pom file is
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<groupId>test.api</groupId>
<artifactId>api.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>api.test Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<finalName>api.test</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The class that is giving me the error is the HelloWorldConfiguration.java class
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class HelloWorldConfiguration {
public static void main(String[] args) {
SpringApplication.run(HelloWorldConfiguration.class, args);
}
}
Any help would be greatly appreciated. Thank you.
Your Maven cache is corrupted on the second machine. The JAR can't be opened, that's why you get this exception.
You can fix that by running this command on the second machine for that project:
mvn dependency:purge-local-repository
If that doesn't work, try remove your local repo on that machine (~/.m2/repository/org/springframework) and run mvn package again.
I also faced the same issue before. Some dependency corruption might have occurred. You can purge and re-resolve the dependencies.
For that :
go to the project location where the pom.xml is present.
open a command prompt there and enter the command below :
mvn dependency:purge-local-repository -DreResolve=true
I had the same issue and I followed these steps to fix it:
1. Close Eclipse
2. Remove all files inside the repository folder .m2/repository
3. reopen Eclipse and update your Maven project
I had the same problem. I resolved this my deleting the local repository directory of Maven (.m2) on your local computer. It resolved the problem for me.
Find out the version of spring-context your project uses:
(in Eclipse : Project Explorer -> [your project] -> Maven Dependencies)
or
mvn dependency:tree
Lists all dependencies for your project
Delete the folder org/springframework/spring-context/5.0.9.RELEASE from your maven repository .m2/repository. Assuming 5.0.9.RELEASE is the spring-context release version.
this conflict occurs, because some of your dependencies have corrupted.follow this:->
for removing this conflict just go in to your .m2 folder open repository and delete all the files from that folder. and now open your project go in debug do maven clean and after cleaning and all downloading just update your maven projet. and your conflict will have been going to resolve.
I too have same problem, I resolved by removing .m2/repository/org folder,later did maven updated form STS.
Add bellow dependancy
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
It may be because some of the dependency is not resolved correctly.
You can try to change/de-grade version of "spring-boot-starter-parent" to other working project in your local.
E.g. mine Error gone by setting "spring-boot-starter-parent" to "1.5.9.RELEASE" from "2.3.0.RELEASE" (that version was working for other project).
My answer is too late, but maybe helps others.
I had the same issue while using Eclipse IDE with spring project that I made using start.spring.io. The way I fixed it:
Right-click on your project.
Runs As.
Maven Install.
For me ,first answer only works when open the child project as a new project in a new window .
You can try delete main.iml or xxx.iml in your child project src\main\main.iml.
That works for me
EDIT: dont worry, main.iml is auto generated by idea
I also faced the same issue and to resolve this I downloaded the jar and added as an external jar.
Here is the download link: http://www.java2s.com/Code/Jar/o/Downloadorgspringframeworkcontextjar.htm

Running Tomcat Maven plugin with added project provided dependency

Need to be pointed in the right direction on this perhaps, but if I add a "provided" dependency that is not included in the tomcat set of provided dependencies, running tomcat7:run from within eclipse fails with a classnotfoundexception on the class from the provided scope jar.
It needs to "provided" because it's a custom jar from a separate project that I've run mvn install on and for production am copying the jar to the $CATALINA_BASE/shared directory so that it's available (as a singleton) across applications/webapps.
<dependency>
<groupId>IndexFileAccessTracker</groupId>
<artifactId>IndexFileAccessTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
Only way I see (with my limited knowledge of Maven and the Tomcat7 plugin) is to change the scope to compile when running tomcat from the plugin in Eclipse and then change the scope back to provided when running the package goal.
Are there solutions to this? I tried adding the dependency to the the tomcat maven plugin (keeping the main maven dependency as provided but got the same class not found error:
<!-- For Maven Tomcat Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/CounterWebApp</path>
</configuration>
<dependencies>
<dependency>
<groupId>IndexFileAccessTracker</groupId>
<artifactId>IndexFileAccessTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
Again, it needs to be provided in the main Maven dependency because I don't want it included in the deployed WAR.
Resolved by using profiles, similar to https://stackoverflow.com/a/5951630
...
</dependencies>
<profiles>
<profile>
<id>runineclipse</id>
<dependencies>
<dependency>
<groupId>IndexFileAccessTracker</groupId>
<artifactId>IndexFileAccessTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
...
Then in my run/debug configuration just added runineclipse to the Profiles: box.
(On a side note, to do step through debugging I had to manually add the project to the Source tab.)
The build configuration was just the same package in the Goals: box; and I left the original dependency to have scope provided.
The tomcat7-maven-plugin and its run goal
Requires dependency resolution of artifacts in scope: test
Everythig that is on the compile classpath is also on the test classpath.
Thats why it is working with scope compile.
So the solution in your case would be to mark your dependency as test what even is (imo) semantically correct.
This will make the library available at local test-time, but not in the final artifact.

Adding a war dependency with appassembler-maven-plugin

I am using appassembler-maven-plugin to create the script to run an application. That application depends on several projects and solves this dependencies using Maven. One of the project is packeged as a war.
<dependency>
<groupId>my.war.dependency</groupId>
<artifactId>project</artifactId>
<type>war</type>
<version>1.0</version>
</dependency>
The problem I have is that the appassembler is not extracting the war dependency, only the jar dependencies. So I cannot refer to the classes in this war in order to create the script calling to one of the classes inside.
Can you help me?
Thanks in advance.

Looking for a Maven shortcut -- including a jar file in a Maven build

I've inherited a Maven project. I'm just using it as a build tool and I'd like to disturb things as little as possible. I have to make a slight addition to one of the Java files and that addition requires that I include a new jar on the build path. How do I say: here a jar, just use it. It doesn't have to be versioned or depended or downloaded or anything, just use it. Any help would be appreciated.
EDIT: I found this, which actually works (!). If someone who knows about such things could read this answer and if it seems reasonably correct, please close this question as a dup.
EDIT: Nope, I misinterpreted my results. It doesn't seem to work.
By far the best way to manage your dependencies with maven is to get them from a repository, but four total options spring to mind, in order from most desirable to least:
If the jar is a common third-party library, you'll almost certainly find it in some repository somewhere. You just have to add a <dependency> element and possibly a <repository> as well so it knows where to get the dependency from.
A home-grown jar not available in any repo should be deployed to a local repository, like Nexus, which is available to your whole team/company. Then add the dependency to your project just like in option 1. This way it only has to be dealt with once, and everyone else can get the jar via the normal Maven mechanism.
To only deal with the problem locally and not give any reusability of the artifact, you can install it into your local repo (meaning your local cache at ~/.m2/repository) using the install:install-file goal.
Finally, and least desirably, you can use a system-scoped dependency. This means you have the jar file available somewhere in your file system, set the <scope> element of your <dependency> to the value "system", and add a <systemPath> element that contains the full path to the jar in question.
Edit: Since option 4 seems right for you, just put the jar into your project and commit it to your version control. Then, assuming the jar is at lib/foo.jar in your project, add this to your POM's dependencies:
<dependency>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<version>1.2.3.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/foo.jar</systemPath>
</dependency>
That's all from memory, but it sounds right.
Here are some related answers:
Maven: keeping dependent jars in project version control
I would not recommend using install:install-file from a POM - if it's a once off requirement you're better using that from the command line and documenting it as a preparation step. However, making the build self-contained or providing a repository with the required artifacts are certainly better options.
Here is how to proceed. Create a separate maven project inspired from the following pom.xml.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.dwst</groupId>
<artifactId>MavenMissingJars</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Maven Missing Jars</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>dProguard-4.6</id>
<phase>generate-sources</phase>
<goals>
<goal>install-file</goal>
</goals>
<inherited>false</inherited>
<configuration>
<file>toinstall/4.6/proguard.jar</file>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>4.6</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Assuming there is a /toinstall/4.6/ directory relative to your pom.xml and that a jar called proguard.jar is in there, calling this plugin will copy the jar from your local directory to your maven local repository.
This has to be executed once, that's why it is preferable to have a separate small maven project for injecting missing jars.
Then, add a dependency in your project using the coordinates (artifactid, version and packaging) you have defined in the above pom.xml.

Categories