Could not resolve dependencies for maven project - java

I've got this kind of error when I try to run my app in Eclipse.
Failed to execute goal on project. Could not resolve dependencies for project pl.wyk:Game-Logic:war:0.0.1-SNAPSHOT: Could not find artifact com.sun:tools:jar:1.8.0_45 at specified path C:\java\jdk/../lib/tools.jar.
But when I try run app from console everything is fine. So what is wrong with Eclipse? I use this same external Maven Runtime.

Try to add your system JDK as Library in Eclipse and use it for the project.
If this doesn't work then you may have to add scope dependency but may cause portability issues:
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>

The problem was with Alternate JRE. You need to choose proper path. In my case:
C:\java\jdk\jre

Related

How to solve "java: package com.opencsv does not exist" in Maven with IntelliJ?

In Maven, I have quite a few dependencies that IntelliJ import. I also have the one for CSVReader
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.2</version>
</dependency>
In the class that uses it, I have import com.opencsv.CSVParser;. It says, "Cannot resolve symbol CSVParser". It gives me an option to add library to classpath. I'm hesitant to do this as last time I did this, it resolved the error but wouldn't allow me to run my Spring Boot application. Why can't the library just be imported like the other ones?
Had the same issue.
A right-click on the project -> Maven -> 'Reload project' may probably fix it.
Commenting out lines where you used the dependency and then building the project once and reloading helps.

What is the equivalent of adding a jar to deployment assembly in maven

I am working with a maven project in eclipse which I have been testing using the built-in "run on server" tomcat option. When I try to do this, I get errors stating certain dependencies are unmet from an external jar I reference in the POM. However I have found if I add the jar via the DeploymentAssembly Tab I can run in eclipse without issues:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.company.user.server.UserDetailsAuthoritiesMapper#0'
The issue comes when I try to deploy on an actual tomcat instance. The Maven build runs fine, but when I start the servlet I get the same unmet dependency errors. This to me is indicating that the external jar is not being properly packaged into the war. What is the maven equivalent of adding the package via the DeploymentAssembly tab in eclipse? The entry in the POM.xml:
<dependency>
<groupId>com.company.webapp</groupId>
<artifactId>webapp-user</artifactId>
<version>106</version>
</dependency>
Thanks
You need to locate the maven info for the external jar. If you google the name of the jar and maven you often find a direct link to the block you need. For example if I want version 1.58 of the Bouncy castle jar Google "Maven BouncyCastle" you can find the artifact info. Add that info to your pom.xml as a new dependency in your block.
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
</dependency>
After you rebuild, refresh maven in your IDE 1st after doing a clean, this will tell maven to pull down the jar and added it to your build.
You can also go to the repo directly and search:
Maven Repo: https://mvnrepository.com/

NoClassDefFoundError on Maven dependency present locally

I am new to Maven Project. I am making changes to one of the open source maven project. I am facing a problem in adding a library to the project. So far i have done this :-
I added a library named jni4net.j-0.8.8.0.jar to the resources folder of the project.
I right clicked the jar(in Intellij) and clicked 'Add as library'.
Then in the pom.xml i added:-
<dependency>
<groupId>jar.0.8.8.0.jni4net</groupId>
<artifactId>jar.0.8.8.0.jni4net</artifactId>
<version>0.8.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/jni4net.j-
0.8.8.0.jar</systemPath>
</dependency>
But when i build this project(build is successful, test cases are running) and use this it throws following error:-
java.lang.NoClassDefFoundError: net/sf/jni4net/Bridge
Please help me resolve it. I am new to maven and pom. I have looked at various answers, but not getting it right.
PS - I named groupId and artifactID as just reverse of jar file
This is not the right way to add that dependency.
All you need is:
<dependency>
<groupId>net.sf.jni4net</groupId>
<artifactId>jni4net.j</artifactId>
<version>0.8.8.0</version>
</dependency>
The dependency will be retrieved from Maven Central when you build.
Using <systemPath>...</systemPath> is highly discouraged as it usually ties your project to a local environment.
Since jni4net.j dependency is available in maven central, You don't have to download and put the dependency manually. Maven will download and store the dependency locally in `'.m2' folder. Just add dependency as bellow.
<dependency>
<groupId>net.sf.jni4net</groupId>
<artifactId>jni4net.j</artifactId>
<version>0.8.8.0</version>
</dependency>

Can't run the google-api-java-client samples in eclipse

I'm trying to run the YouTube JSON-C Sample in eclipse. I have followed the instructions in the link and I managed to run it in the command line using mvn -q exec:java, but when I import the project to eclipse (I use eclipse indigo), it says that "The import com.google.api.client.googleapis cannot be resolved", and gives me compiler errors in every line that is related to the api. Is there some other configuration that needs to be done? Specifically adding google-api-java-client-1.5.0-beta jars to the build path?
Ok I finally solved the problem. What did the work for me was to execute mvn eclipse:eclipse on the project folder in terminal...
You need to check if you are pulling out all the dependencies in pom.xml from the repository:
<dependencies>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.5.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.5.0-beta</version>
</dependency>
</dependencies>
Check the settings.xml file in the Maven installation directory if it is pointing to a global repository or if you have hosted your own Nexus/Sonatype repository then make sure that your settings.xml points to that and also you must have the above two jars in your repository.
Or if you just want to get the project up and running, then instead of importing as a Maven project, just import it as a normal Java project and manually add the two libraries to the project's classpath. That should at the least get you started with the project without worrying about the Maven stuff.

Unable to run unit tests with Snakeyaml in IntelliJ

I have created a Java component which reads a YAML file using SnakeYaml. The environment I use is IntelliJ with a Maven plug-in and my project is built using a Maven pom file. When I run the Maven test project, my unit tests all pass. However, when I run the unit tests within IntelliJ directly, they fail.
Specifically, the call new Yaml(myConstructor) below is throwing an exception:
Constructor myConstructor = new Constructor(....)
Yaml yaml = new Yaml(myConstructor)
The specific exception is:
java.lang.NoSuchMethodError:
org.yaml.snakeyaml.Yaml.(Lorg/yaml/snakeyaml/constructor/BaseConstructor;)V
Any ideas?
This happens because of the TestNG plugin. It's an issue for both IntelliJ and Eclipse.
Easily solved in two ways:
Update to the latest TestNG plugin version for your IDE of choice, hoping that the shipped version does not conflict with the one required for your project
Enable the "Use Project TestNG jar" in Eclipse (or the IntelliJ equivalent). This setting is available in the TestNG section of the project specific settings.
The second way would be preferred because TestNG dependencies will be managed by your build tool (are you using a build tool, right ?!) and you have a lot more flexibility.
Take look at file -> proj structure -> artifacts. There can be error. Just click at "Fix" button. Also try recreate itellij project from maven configuration. Usually there is troubles with libraries scopes, like TEST/PROVIDED.
The below worked for me
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.2</version>
<type>jar</type>
<exclusions>
<exclusion>
<artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId>
</exclusion>
</exclusions>
</dependency>

Categories

Resources