maven external jar dependency - java

I am trying to use a jar given by my teacher in my code. So I placed the jar in a lib directory in my project and ran this command :
mvn install:install-file -Dfile=lib/IDLogger.jar -DgroupId=IDLogger -DartifactId=IDLogger -Dversion=1.0 -Dpackaging=jar
Everything is fine.
Then I add the dependency to the pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
But then when I try to use the jar, the compilation fails and tells me that the symbol(the class) can't be found. I even tried adding an import statement :
import IDLogger.IDLogger;
but it tells me that there is no such package.
How can I use this jar in my code in maven?
This is the code :
IDLogger logger = IDLogger.getInstance();
...
logger.logID(id);
I get the symbol IDLogger not found error
This is as much as I know about this custom jar...

Try to add your lib in system scope :
1. add in your project :
Your Project/lib
+IDLogger.jar
2. in your pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/IDLogger.jar</systemPath>
</dependency>

Related

Spring Boot Jar file usage problem when using artifact

I have a library project that I build and generated jar file in the out folder. Then I was using this Jar file by adding to another Spring Boot project via the Libraries section.
While it was working, the I started to get the following error and I am not sure if something is changed after building the library project:
The following method did not exist:
'void org.springframework.beans.factory.support.DefaultListableBeanFactory.setApplicationStartup(org.springframework.core.metrics.ApplicationStartup)'
The method's class, org.springframework.beans.factory.support.DefaultListableBeanFactory, is available from the following locations:
jar:file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar!/org/springframework/beans/factory/support/DefaultListableBeanFactory.class
jar:file:/Users/einstein/.m2/repository/org/springframework/spring-beans/5.3.8/spring-beans-5.3.8.jar!/org/springframework/beans/factory/support/DefaultListableBeanFactory.class
The class hierarchy was loaded from the following locations:
org.springframework.beans.factory.support.DefaultListableBeanFactory: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.AbstractBeanFactory: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.FactoryBeanRegistrySupport: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.core.SimpleAliasRegistry: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.DefaultListableBeanFactory
So, what is missing and how can I solve the problem?
Here is my lombok depebdency in pom.xml file:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
If your library jar has it's size in MBs then it must be a fat jar. You should change the scope of jar's dependencies in pom.xml to provided and then build jar. For example
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
**<scope>provided</scope>**
</dependency>
Above you can see scope is set to provided that means the dependencies which are required by your library will be provided by parent application which will be using it.

Maven dependency version

When doing a mvn clean install -U I am getting:
[ERROR] Failed to execute goal on project xxx-security: Could not resolve dependencies for project xxx:xxx-security:jar:50-SNAPSHOT: Failed to collect dependencies at
xxx:xxx-persistence:jar:50-SNAPSHOT -> org.mybatis:mybatis:jar:${mybatis.version}: Failed to read artifact descriptor for org.mybatis:mybatis:jar:${mybatis.version}: Could not
transfer artifact org.mybatis:mybatis:pom:${mybatis.version} from/to nexus (http://example.net/repository/Standard/): Failed to transfer file: http://example.net/repository/Stan
dard/org/mybatis/mybatis/$%7Bmybatis.version%7D/mybatis-$%7Bmybatis.version%7D.pom. Return code is: 400 , ReasonPhrase:Invalid repository path. -> [Help 1]
What I don't get is why maven is using mybatis.version in the url to get the dependency rather than a version number. I have searched the code and cannot find mybatis.version in there. I did find:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.0.4</version>
</dependency>
So why is maven using mybatis.version rather than 3.0.4?
It looks like your xxx:xxx-persistence:jar:50-SNAPSHOT jar has internal dependency with org.mybatis:mybatis:jar:${mybatis.version}. Provided that you define mybatis.version in your properties.
You can do following:
Execute mvn dependency:tree to check which specific jar has this dependency.
Also define <mybatis.version>3.0.4</mybatis.version> under <properties> section of pom.xml. You can optionally remove <version>3.0.4</version> from your <dependency> section where you defined mybatis dependency.

Class not found in Maven Dependency

I am facing a problem with maven dependency.
I have a maven project called DdsInterface. Inside the pom.xml there is this dependency
...
<dependency>
<groupId>it.snam.ned</groupId>
<artifactId>ned-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
...
That is used in a class to import the NedRestTemplate object. Everything in the project is working.
Then I build a jar with command maven clean install and generate a ned-dds-interface.jar.
I then install the library in my local repository .m2 with command
mvn install:install-file -Dfile="target/ned-dds-interface.jar" -DgroupId=ned-dds-interface -DartifactId=ned-dds-interface -Dversion=1.2 -Dpackaging=jar
Now I want to use this library ned-dds-interface in another project call Portal. So in the pom.xml I created the dependency
<dependency>
<groupId>ned-dds-interface</groupId>
<artifactId>ned-dds-interface</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
Finally I create the war for the project to run in a Web logic server using
mvn clean install
and then deploy the package Portal that successfully run on Weblogic. But executing a function I get the following error
Root cause of ServletException.
java.lang.NoClassDefFoundError: it/snam/ned/libs/core/NedRestTemplate
at it.snam.ned.libs.dds.v2.AbstractDdsDaoImpl.<init>(AbstractDdsDaoImpl.java:25)
at it.snam.ned.libs.dds.v2.DdsFolderDaoImpl.<init>(DdsFolderDaoImpl.java:36)
at eng.la.controller.DdsController.getFolderInfo(DdsController.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Truncated. see log file for complete stacktrace
Can someone please help me with this problem?

How to setup LWJGL with Maven?

Im unable to successfully add proper dependencies for LWJGL in maven project. I have copied lwjgl dependency tempalte from maven repository, added it to my pom.xml and tried to run basic application from lwjgl.org, without success.
Below you have my pom.xml file, unfortunately when I try to run my first application i get:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Failed to locate library: lwjgl.dll
I found some sollution that I need to download all jars and attach them as jar library, so I did. File > Project Structure > Librarires > added folder which store all jars for LWJGL, unfortunately this is still not working.
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.lwjgl/lwjgl-opengl -->
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>java3d</groupId>
<artifactId>vecmath</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
Please go to https://lwjgl.org/customize, select a variant ("Release" or "Early Access"), select "Mode" = "Maven", select all your needed/wanted modules and simply copy/paste the produced pom.xml snippet.
Note that the pom.xml snippet presented on the website is not a complete pom.xml. You still have to provide the surrounding <project> XML element and additional needed XML child elements.
I'm using the IntelliJ Idea IDE for this explanation.
Go to https://lwjgl.org/customize, select a version (Release, Stable, or Nightly), select "Mode" = "Maven". I personally chose "Getting Started" for the modules but you can choose base on your needs. Now press "copy too clipboard" to copy the pom.xml file.
Now open up IntelliJ and create a new project. Select Maven Project and follow the creation of the program. I called mine "maven-test". Copy and paste the script (pom.xml) from the lwjgl website that you have in your clipboard and past it before the </project>.
Also copy and paste this inside of the
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
This will prevent an error because of the default Maven compiler which is v1.5
Make a new java class under maven-test/src/main/java called Hello World and paste in the code found at https://lwjgl/guide
Finally press the Maven button on the right of the IntelliJ IDE Editor and press the refresh button that says "Re-import All Maven Projects". Right click insideHelloWorld.java and press run 'HelloWorld.main()'

NoClassDefFoundError when importing Tika 1.13 in Eclipse

I've done the following steps per the tika guide:
Add the tika-core and tika-parser dependencies to the pom.xml of the maven project
Run maven install from eclipse to produce tika-core jar and tika-parser jar
Add tika-core jar and tika-parser jar to my eclipse project build path
And I get this runtime exception when trying to run tika:
Exception in thread "main" java.lang.NoClassDefFoundError: org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException
at com.ibm.hrl.ace.pdftotext.TikaExtracter.parse(TikaExtracter.java:33)
at com.ibm.hrl.ace.pdftotext.Main.AllPdfsToText(Main.java:116)
at com.ibm.hrl.ace.pdftotext.Main.main(Main.java:34)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException
at java.net.URLClassLoader.findClass(URLClassLoader.java:600)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:786)
at java.lang.ClassLoader.loadClass(ClassLoader.java:760)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:326)
at java.lang.ClassLoader.loadClass(ClassLoader.java:741)
... 3 more
As far as I can see, when I build the jars using maven, it does add pdfbox properly... from the build log:
[INFO] Including org.apache.pdfbox:pdfbox:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:fontbox:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:pdfbox-tools:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:pdfbox-debugger:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:jempbox:jar:1.8.12 in the shaded jar.
And here are my maven dependencies:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-core -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-parsers -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.13</version>
</dependency>
</dependencies>
The problem is that if you manually add tika-core and tika-parsers jars in your build path you will not have the transitive dependencies that are listed in their own POM.
So I would suggest to:
Remove the tika-core and tika-parsers version that you have built yourself. Instead you should rely on the versions that are available on central. This will ensure that another one building your project will get the same jar (and not a locally built one)
You have two options
(Option A, use Maven) Do not add manually into Eclipse build path the jars. Rely either or built-in Maven plugin for Eclipse (m2e for instance) or use Eclipse plugin for maven (call mvn eclipse:eclipse to update .classpath and .project).
(Option B, without Maven) If you cannot use Maven for your project, you will have to add not only tika-parsers and tika-core jars, but all (most of) the transitive dependencies needed by these project (including for instance specific library per format [POI for Office, pdfbox for PDF...). You can get a list of the dependencies by typing mvn dependency:list in the folder containing the pom of tika-parsers.

Categories