I am working on a web application in JAVA, and I used org.json. in the project for some computation, but I get the following error message when I run the application from localhost.
java.lang.NoClassDefFoundError: org/json/JSONObject
I specified the dependency in pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.cloudera.oryx</groupId>
<artifactId>projectname</artifactId>
<version>0.5.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
</dependencies>
</dependencyManagement>
EDITED: Full Pom.xml content
What other thing should be done to get this to running? Thanks.
There may be various reasons, you get NoClassDefFoundError even after adding the dependency
The following are the checks you can do to get.
Add the dependency in <dependencies> section of your main pom.xml. If you feel like I dont require JSON library in service layer in an application like Rest --> service --> DAO, then add in <dependencyManagement> section and add the dependency with out version - in which ever module you want.
You need to make sure that your settings.xml is configured right enough to get the jar files downloaded for you.
Another possibility is refresh your workspace if you are using some IDE - eclipse, IntelliJ to get the dependencies updated.
You can check .m2 --> repository --> org --> json --> <version> folder is available or not. This confirms that the jar is downloaded and you can tick point 2 as confirmed.
You can unarchive the war file and check in the libraries whether the json jar file is added to your end packaging or not. This is the final place to check.
You can use mvn -U clean install to update the dependencies.
I think the problem is your POM's "packaging"
The normal way to build a webapp is to specify the packaging as "war" so that Maven will build a WAR file containing your code and the dependent JARs. You then deploy the WAR file to the web container.
But you are using packaging "pom".
I'm assuming that means you've got another (child) POM file to build your application JAR.
I'm also guessing that you are using "jar" as the packaging in that POM.
I'm also assuming that you are then deploying the JAR to web container; e.g. by hand or using some IDE integration thingy.
I think that the problem here is that while you are deploying your JAR file to the web container, and you don't need to deploy the Jersey dependencies ('cos they are already there!!), you are NOT deploying the "json.org" JAR file.
At any rate, the reason that you are getting the exception is that the web container classloader cannot find that JAR file. If you are deploying without using a WAR, something has to copy the file to the place where the web container's classloader is going to look.
Related
Currently I have a Gradle build for a Java Web Start Swing application that works. It's mainly using the steps outlined in: http://www.apprenticeshipnotes.org/2013/01/generating-webstart-files-using-gradle.html
The huge issue with doing a Gradle build this way, is that we are wasting time signing unchanging JARs every time we do a build (by far where most time is spent) in order to build the application.
I've been looking at Gradle code on Github, looking at other recipes that might point me in the right direction, but I can't seem to determine how I would go from a set of tasks that takes the build artifacts of an existing configuration, signs them, and then bundles them into the application to something that is much more efficient.
My idea was to have the ability to resolve dependencies, then determine if there was a 'signed' version of that dependency in the repository. If there was not a signed dependency, the build would sign that artifact and upload to a repository.
An easier approach that I was trying to take was a project that takes the runtime configuration of the application project and somehow loops over the dependencies and signs them and uploads the artifacts to a repo, but it's not clear to me how I could do this with the Configuration API. Just getting at the files of the configuration is not enough, because I would want dependency information for each JAR file.
It also gets a bit trickier with uploading the JARs, because if, for example, I upload a signed jar, say metrics-json-3.0.2.jar, as metrics-json-signed-3.0.2.jar, I would need to update the POM to reference the signed artifacts. Seems like there should be an easier approach
<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>
<parent>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-parent</artifactId>
<version>3.0.2</version>
</parent>
<artifactId>metrics-json</artifactId>
<name>Jackson Integration for Metrics</name>
<packaging>bundle</packaging>
<description>
A set of Jackson modules which provide serializers for most Metrics classes.
</description>
<dependencies>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</project>
Note, this is different from the signing mechanism provided by the existing Signing Plugin:
http://www.gradle.org/docs/current/userguide/signing_plugin.html
All I want is the project file/jar for this project: http://thiagolocatelli.github.io/parse4j/
It says I need to do the following to obtain it:
Getting Started
Download the library manually
Maven
<project ...>
...
<dependencies>
<dependency>
<groupId>com.github.thiagolocatelli</groupId>
<artifactId>parse4j</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
...
</project>
I have never used Maven, do not really know what it is. Can someone advice me how I obtain the project file?
Maven is a dependency manager. Lots of information about it if you're interested - just use your favorite search engine.
You can also download the jar file directly from http://search.maven.org/remotecontent?filepath=com/github/thiagolocatelli/parse4j/1.3/parse4j-1.3.jar
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.
I have installed maven dependency using the command below
mvn install:install-file -Dfile=d2_lib.war -DgroupId=com.emc.d2fs -DartifactId=d2_lib -Dversion=1.0 -Dpackaging=war -DlocalRepositoryPath="C:\Users\kumarr23\.m2\repository"
I have added this dependency in my root pom.xml as below
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
I can see when I do maven install the jars inside my final war,
when i try importing these classes from eclipse these classes are not included, Is there some thing I am missing?
I am new to maven and i am stuck here !
please advise
Please check if you have d2_lib.war file at the location from where you are running command or else mention the full path of d2_lib.war in command and try again , i believe with the command you specify it is not copying the war file since war file path is not defined properly , you can confirm this by checking if war file is copied to your local maven repository
mvn install:install-file -Dfile=d2_lib.war -DgroupId=com.emc.d2fs -DartifactId=d2_lib -Dversion=1.0 -Dpackaging=war -DlocalRepositoryPath="C:\Users\kumarr23.m2\repository"
There is a convention in maven that java code lives in a .jar file and web classes live in a .war file. When you include a dependency with the war type in your web project, maven will treat the included file as an overlay. This basically means that maven will package the contents of the dependency into your war file, however it will not include the libraries in that war as java code the way you are looking for.
When a war file is created, if the creator also wants people to get at the java code and transitive dependencies the code is based off of, they can use the maven-war-plugin to set a configuration tag of <attachClasses>true</attachClasses>. This will generate an additional artifact in the form of artifactId-version-classes.jar, along with the normal artifactId-version.war file.
When you include both the war as an overlay and the classes jar as a normal dependency, you will be able to see in your dependency tree that maven is only pulling in transitive dependencies from the jar file dependency, not the war.
In your case, you need to find the classes jar file and install it along with the war file in your local maven repository. Then you can add the classes jar to your pom.xml like so:
old pom.xml
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
new pom.xml
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.emc.d2fs</groupId>
<artifactId>d2_lib</artifactId>
<version>1.0</version>
<classifier>classes</classifier>
</dependency>
I found that Maven implies specific directory layout. But I don't understand from here: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
where java libraries needed to compile and run my code should be placed. I think they shouldn't be placed under 'src/main/resources' because resources is something like images or so. Also it doesn't look right to place them under 'src/main/java'. If I wouldn't use maven, I'd place libraries in project's root lib directory. But I don't think that for maven project it will be right. Please advise.
UPD: I solved the problem. The matter was that I set packages for my sources as src.main.myApp instead of main.myApp. This seems to upset maven.
Maven handles your project dependencies in a different way to a 'Standard' Java project.
You declare the libraries you depend on in your project's pom.xml:
e.g.
<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>org.example</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>your-project-web</name>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</project>
When you use a maven command to build the project, i.e. mvn install, it will download the dependencies for you and store them in your local repository.
In Maven, you do not keep libraries in your project. You specify dependencies on these libs, and they get populated into your local repository. At the time of build, if you are packaging the libs (say for a war file), they do get pulled into target//WEB-INF/lib. But in general, the whole idea is not to deal with these libraries or manage them, just to manage dependencies in your pom file, and forget the rest.