Getting NoClassDefFoundError while invoking external JAR in maven project - java

I have a maven based project where I have to invoke an external jar (say country.jar)
I added this jar on src/lib folder and did below setting in pom.xml
<dependency>
<groupId>country-stubs</groupId>
<artifactId>country-stubs</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\country.jar</systemPath>
</dependency>
While Running my application I am getting error
java.lang.NoClassDefFoundError: com/fsg/bpo/webservices/countWebService
Location(com/fsg/bpo/webservices/countWebService) is referring to the class present under country JAR
Do I need to add few more settings to configure external JAR in maven ?

If you are using scope system you are saying it is a provided jar later on. I would use runtime maybe. Have a look at maven scopes

If it is web application then your jar won't get bundled inside your war file. Then you will get this issue. you can directly copy this country.jar to your WEB-INF lib folder and use that system path.

1st u need to add ur external jar to ur local maven directory..
using below command on cmd..
mvn install:install-file -DgroupId= -DartifactId=aes-decryption -Dversion=1.0.0 -Dpackaging=jar -Dfile=./target/aes-decryption-1.0.0.jar -DgeneratePom=true

Related

MissingResourceException: Can't find bundle for base name when running JAR

I made a maven spring-boot project using Spring Initializr and added the basic dependencies like starter-web, tom-cat-jasper, dev-tools and etc.. then imported it in Eclipse.
I then added a new external JAR in my local repository that is not from maven repository using
mvn install:install-file
and added the new dependendcy in the pom.xml
<dependency>
<groupId>com.domain.project</groupId>
<artifactId>resourceName</artifactId>
<version>1</version>
</dependency>
And this external JAR has a .properties that's need to be added in order for it to connect to a database. I put this .properties file in src/main/resources and tested the methods in the external JAR and it works as intended in the Eclipse IDE.
After this I run the project as Maven build with goals: clean install so that it can be run as an executable JAR.
Then I run the JAR with start java -jar using a .bat file everything is working but whenever I used the methods from the external JAR via GET/POST request it gives the error:
java.util.MissingResourceException: Can't find bundle for base name resourceName, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1581) ~[?:1.8.0_241]
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1396) ~[?:1.8.0_241]
at java.util.ResourceBundle.getBundle(ResourceBundle.java:782) ~[?:1.8.0_241]
at com.domain.project.vitality.VitalityInfo.VitalityInfoDetails(VitalityInfo.java:28) ~[resourceName-1.jar!/:?]
This is the name of the external JAR: resourceName.jar and the required properties file: resourceName.properties.
I also encountered this error if I rename or removed the required .properties file in src/main/resources in Eclipse IDE.
If I run the project only in Eclipse IDE and access the said external JAR methods via GET/POST request it gives the usual JDBC SQL error.
How do I fix this whenver I build using maven any help is very much appreciated?
EDIT: This is the result from jar tf of executable JAR when using maven build.
BOOT-INF/classes/application.properties
BOOT-INF/classes/resourceName.properties
BOOT-INF/classes/log4j2-spring.xml
EDIT: Issue is resolved.
Turns out the solution was rather simple.
In my case the external JAR has this in its implementation
ResourceBundle bundle = ResourceBundle.getBundle("resourceName");
I changed this into
ResourceBundle bundle = ResourceBundle.getBundle("application");
And simply renamed resourceName.properties file into application.properties or just copy the content from resourceName.properties into your application.properties or any property file that spring-boot is able to read.

Is there any way to modify classpath through POM.xml?

I have a situation like below:
1. I have a JAR which is available in cloud URL
2. I want to download the JAR and use it as a JAR in my project.
3. For downloading I am using Maven ant run plugin and putting it in specific folder of the project.
Now I want to add the jar to classpath file via pom.xml
I have downloaded the JAR from the cloud and placed it in specific folder.
Is there anyway in which as soon as I download the JAR from cloud, I can modify my classpath by adding a classpathEntry through POM.xml?
There are various ways but the easiest option is to download the file to your local repository with the file path matching to your dependency's groupId, artifactId and version.
Assuming below is the dependency that you are trying to add to you pom.xml
<dependency>
<groupId>your.company</groupId>
<artifactId>downloaded-file</artifactId>
<version>1.0.0</version>
</dependency>
then download the file to /your/company/downloaded-file/downloaded-file-1.0.0.jar

Add Jar and Classifier as dependency to Maven project

I have a local Jar file that I want to add as a dependency for a Maven project that I am working on.
That is easy to do as this answer states.
The problem is that this Jar file requires a foo.classifier file to work properly.
This Jar file is meant to be run by command line (java -jar bar.jar {argument}). The Jar and the classifier files are in the same directory, so everything works just fine.
However, from my project, I want to call the Jar file using its methods and not running a process.
I added the Jar file as a dependency and managed to call the main method that receives the argument, however, it doesn't work properly because of the foo.classifier the file is not set as a dependency for that Jar file to work.
Does anyone know how can I set it as a dependency of that Jar file?
Put foo.classifier files in exact folder in .m2\repository folder where your local Jar file that you added as a dependency is present as dependency jar will be referred from local repository
You can put your jar to libs directory,and add below code into your pom.xml file:
<dependency>
<groupId>htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.21-OSGi</version>``
<scope>system</scope>
<systemPath>the full path of the jar</systemPath>
</dependency>
Add the jar to maven repository with the follows command line, specify a group id, artifact id and a version
mvn install:install-file -Dfile="my-local.jar" -DgroupId="my.groupid" -DartifactId="name" -Dversion="1.0.0" -Dpackaging=jar
and then add as dependency to your pom
<dependency>
<groupId>my.groupid</groupId>
<artifactId>name</artifactId>
<version>1.0.0</version>
</dependency>

local dependency in my case

I am using maven to build my java project.
I have a library jar named my.jar used as my project's dependency. However, it is not available in the remote central repository. So, I would like to put the jar file under my project. So, I created a folder named my_repo/ under MyProject.
The directory structure of MyProject looks like this:
MyProject/
my_repo/
my.jar
pom.xml
But I have no idea how could I define my pom to find this particular dependency under MyProject/my_repo/my.jar ?
Could someone please help me for my scenario? By the way, I have also some other dependencies defined in my pom.xml, they are available in the remote central repo.
Using the system scope. ${basedir} is the directory of your pom.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/my_repo/my.jar</systemPath>
</dependency>
The recommended way is usually to use a Maven Repository Manager (e.g. Nexus) and then deploy your library to this Maven Repository Manager.
See here for how to set up the settings.xml file:
http://books.sonatype.com/nexus-book/reference/maven-sect-single-group.html
How to create maven dependency for your local jars..
1)Create a maven project of the program of which you have to create the jar.
2)Add required groupid and artifact id and version no.
3)Then after writing the java files export it as a jar.
4)Now open other project in which you want to add local jar dependency.Go into its pom.xml and add dependency with the group id,artifact id and version which u had entered in the jar project.
5)Maven clean and install.Now that jar file will be available in your local repository. :)

how to delete jetty jars from maven repository

I am using eclipse for my project development. In my pom.xml no where I have added dependency for Jetty jars (jetty-server, jetty-io, jetty-http, ...). But when I run "mvn package" command in my local repository and in my WEB-INF --> lib folder these jetty jars are getting added. But I don't want to use those jars. Can someone please suggest me a way to delete these jars through pom.xml.
In my local repository its getting added in repository --> org --> eclipse --> jetty. I tried to delete it by making use of tag in pom.xml, but couldn't resolve.
type mvn dependency:tree in console. You will see where from those jars are
You can try to run your maven project from Console with the command mvn clean package inside the folder that you have your pom.xml file. With this command you clean your application. Maybe this can remove these libraries

Categories

Resources