java.lang.NoClassDefFoundError: org/json/JSONObject in customised jar file - spring-boot

I created a jar file that using customized JSONObject class when I include it in my project in pom.xml i am getting the above error. When i import it using IntelliJ it only works in IntelliJ but I wasnt to share the jar file. How can I make my customized file be incorporated with the project?
<dependency>
<groupId>com.roufid.tutorials</groupId>
<artifactId>example-app</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/yourJar.jar</systemPath>
</dependency>

This is what I did
Run below command in project root (the location that contains `pom.xml file)
.\mvnw install:install-file -Dfile='src\lib\custom.json.object-0.1.0.jar' -DgroupId='com.custom.json.object' -DartifactId='com.custom.json.object' -Dversion='0.1.0' -Dpackaging=jar
In pom.xml I added the below line
<dependency>
<groupId>com.custom.json.object</groupId>
<artifactId>com.custom.json.object</artifactId>
<version>0.1.0</version>
</dependency>

Related

Packaging multiple jars using maven

I am new to maven. I want to package my java code so that, it can be run only by running a command java jar.
My code uses 2 external jar files, 1 main java class, 1 property file and will output 1log file.
My POM :
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibm.mq.jms</groupId>
<artifactId>com.ibm.mq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.allclient</artifactId>
<version>9.0.4.0</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
How can i create a single jar file that could be run independently?
Is it better to have this complied in a zip file?
My property file needs to be changed by user. Where should I put my property file?
Generally, try one of the solutions in How can I create an executable JAR with dependencies using Maven?
The property file must be outside the final jar so that the user can edit them.

How to generate maven jar that will include my external jars that under lib folder

I have maven project with lib folder that contains some jars,
In my pom.xml, I've added dependency accordingly like:
<dependency>
<groupId>autoit</groupId>
<artifactId>autoit</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/AutoItX4Java.jar</systemPath>
</dependency>
The problem is when I create a jar and run on cmd I get Exception:
nested exception is: java.lang.NoClassDefFoundError :
AutoItX4Java...
If I run via eclipse there are no Errors.

How to add Maven dependency jar file from the lib folder

I am trying to add a jar file to the Maven dependency in my project. The settings.xml is configured to set the repository to a public repository. But this particular jar is not present in that repo. As i have the jar in my lib folder, how can i configure the Maven dependency to take this jar?
Have a look at system dependencies.
You basically need to define <scope>system</scope>.
<project>
...
<dependencies>
<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
</dependencies>
...
</project>
This is if you don't have your own hosted artifact repository server such as Nexus, Artifactory or Archiva.
If you do, then as Karl-Heinz suggested, you would be better off placing it there, as it's not good practice to commit binary artifacts to version control.
Another option is to set up a repository that is backed by a directory inside your project, per this answer: https://stackoverflow.com/a/2230464/433789
Changing pom.xml worked for me (jar in "/lib" directory in the root project folder) :
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/gson-2.8.6.jar</systemPath>
</dependency>
The best solution is to start using a repository manager like Nexus and put it there. Or you can use the non-maven-jar-plugin.

how to add my one project jar in another maven project in netbeans

I have one project jar oauth.
I want to add it in another maven project . I tried to change pom.xml file but no effect. Can anyone please suggest me?
I tried to add following dependency in my pom.xml file:
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapps/WEB-INF/lib/auth.jar</systemPath>
</dependency>
With the assumption that you have that auth.jar in your local repository (as it builds fine).
Why don't you give a try like this.
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
Honestly speaking I don't prefer to give the jar location in my pom file and using scope as system, I leave this task to handle by Maven to resolve all the artifacts either by searching in local maven repository first(/.m2) or in MAVEN CENTRAL REPO if it is a 3rd party jar.

Java compiles from command line but not from Maven

I have my system set up to compile jee6 code from the command line (no IDE). I recently tried to compile some code that uses javax.enterprise.context.RequestedScope. The code compiled fine from the command prompt but when I tried to build the application using Maven I keep getting javax.enterprise.context package not found error.
What gives? I thought Maven was using the same javac I'm using. Why can javac find the package but Maven can't? Do I need to add dependencies for java packages?
Or...is there a way to tell Maven to use the current class files when building or does Maven have to compile when it builds?
Thanks.
Try adding the Java EE 6 jar to your Maven dependencies:
<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">
. . .
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
This will add the Java EE classes to your compile-time classpath.
To compile your code with Maven, you need to tell it where to find required components.
Looks like you need to add this to your dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
Note the <scope> element. It tells Maven not to bundle this dependency with your artifact, because at run time it will be provided by the application container.
On the reason why it compiles from the command line.
You probably have the required jar file on the command line -cp option or in your global CLASSPATH variable.
Maven, by design, does not pay attention to the global CLASSPATH.

Resources