NoClassDefFoundError while executing jar in intellij - java

I am trying to execute a jar file, in terminal.
Jar created in intellij:
Build -> Build Artifacts -> Clean
Build -> Build Artifacts -> Build
Execution cmd:
java -jar helloworld.jar
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
at main.java.com.example.helloworld.sampleclass.mymethod(sampleclass.java:53)
at main.java.com.example.helloworld.sampleclass.main(sampleclass.java:14)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
I have added the following dependency also.
Pom.xml Dependency:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
The code executes perfectly when I try to run in intellij, but I am getting this error when I run with the jar. Any suggestions would be great.

Ensure that you have correctly configured the artifact in (File -> Project Structure -> Artifacts) settings. Most likely, you have not specified the libraries that should be added to the class-path in the Output Layout tab.
I can assume that you created an artifact before adding a dependency. Try to remove the current artifact configuration and create a new one. IntelliJ IDEA will prepare a jar file with all necessary dependencies based on one of the following settings.
See the links below to learn how to manage Artifacts in IntelliJ IDEA:
Work with Artifacts
Artifacts

i know, it's not very intuitive, but maven doesn't actually embed your dependencies into your jar, unless you explicitly ask for it. If you open your jar like a zip (because jar files are essentially zip files with class files in them), you'll see that the class that you see an error about is actually not there.
Personally, rather than using the intellij export (I'm not sure if it just builds the jar the same way maven does), I would use maven directly with mvn package command (optionally mvn package -DskipTests if you don't want to execute your unit tests every time you build your jar). mvn package will build the jar and put it somewhere int the ./target directory.
And then, unless you want to specify the path to your jar dependencies in the command line every single time you run your app, you also will want to use the maven's shade plugin. The shade plugin unzips all the dependencies that you specify in the pom.xml and puts them into your new jar along with the code of your own application: http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
Sorry, I know it gets a little bit complicated here, but if you want to have an executable while using maven dependencies, shading is your best option.

In my case I had to extract the maven library to output root.
Project settings(Ctrl + Alt + Shift + S) -> Artificats -> Output Layout -> Move element to output root.

Related

GWT maven build eclipse

I had a GWT app, and I wanted to automate its build and deploy system, since I do it manually. But I did not find a way how to build the app from command line, so it can than be automated. I had to click the Google button, then compile GWT project and then click Compile.
I found out that it is possible to create a GWT maven project and that it should then be possible to compile my project from commandline with mvn gwt:compile.
So I created a new project using this plugin. Copied my sources from the old project to this new one.
Now the structure is like this:
/src
---/main
------/java -> here are all my sources including my Project.gwt.xml file.
------/webapp
---/test
pom.xml
Now I have 2 problems.
1. I thought that I add dependencies to the pom.xml, and then when I build the app, it will create the jars and I can use those libraries in my GWT app. I guess 'mvn clean install' should do this, but so far I'm getting compile errors.
2. I did not get mvn clean install to work, so I added all the jars manually again... And then yes! I was able to build the app using the plugin GWT button! So I was thinking that now I can use 'mvn gwt:compile', but it fails with:
Unable to find: "com/company/project/Project.gwt.xml" on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?
EDIT:
So I fixed my <moduleName> element in pom.xml, so now it finds the Project.gwt.xml. I'm trying to run:
mvn clean install gwt:compile
But I am getting compile errors. I think, it tries to build my project without the actual dependecies because it tells few classes don't exist, but those classes are part of an external library. (specifically this one). But I have it in the dependencies, so I don't know what more to do.
<dependency>
<groupId>com.github.tdesjardins</groupId>
<artifactId>gwt-ol3</artifactId>
<version>3.0.0</version>
</dependency>
Also in eclipse I had to manually add the jars to my project, so that was why it worked there and not in the command line. So I would also like to ask how to tell eclipse to get those jars and include them to the project, because otherwise eclipse is missing those dependencies and displays many errors.
First I had a problem with <moduleName> in my pom.xml was missing com.company.project prefix before the actual module name.
Then I had errors in my Java files, which was caused by RELEASE version of GWT-OpenLayers 3 library missing some of the features that I previously used by building the JAR from the GitHub repository.

Need to create Java JAR file and add it to Maven Dependency

I have a project downloaded from git. Here is the link to source code https://github.com/dwdyer/reportng, I have downloaded it and now I am trying to create a JAR file and then want to attach it to maven repository. When I compile it using mvn compile and mvn package, it gives me the same INFO message, and in my target folder there is a jar file is created. But only pom.xml and pom.properties are shown inside it, instead of whole hierarchy of compiled class files replaced with Java files
Maven is very picky about following a very specific folder layout in the given project (which you can override but it is not really intended to do so).
Instead you may want to just install the generated jar file directly in your local repository using the mvn install:install command.
If you want to script this, see Multiple install:install-file in a single pom.xml for instructions on how to create a pom.xml doing this.
First of all, you have to debug problem:
cd reportng/
mvn -e clean install
-e switch on errors' trace.
If everything ok, install will add jar just created jar file to your local repository.
Then it will be available as dependency to any project:
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
</dependency>
By the way:
Jar is available in maven central, so referencing it as dependency, having compile time internet connection, will be enough.
This can be solved by changing the Maven strusture. Maven must contain src/main/java, whereas in the program it is src/java/main

Maven Spring project: Adding a custom library using Eclipse IDE [duplicate]

I want to add jpoller.jar as a maven dependency, so I edited my pom.xml file to include it:
<dependency>
<groupId>org.sadun</groupId>
<artifactId>jpoller</artifactId>
<version>1.5.2</version>
</dependency>
Now, as expected, when I compile from the command line I get an error because jpoller isn't in any of the repositories I have listed in my pom, nor could I find one for it. Although I could create a repository for it, I'd rather not at this point. Thus, I get the following error:
[INFO] Failed to resolve artifact.
Missing:
---------- 1) org.sadun:jpoller:jar:1.5.2
Try downloading the file manually
from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.sadun -DartifactId=jpoller -Dversion=1.5.2 -Dpackaging=jar -Dfile=/path/to/file
How can I do this from the M2Eclipse plugin on machines where the maven CLI isn't available?
How can I do this from the M2Eclipse plugin on machines where the maven CLI isn't available?
Well, simply do it from Eclipse. First, get that jpoller jar and save it somewhere on your file system. Then, in Eclipse, from the top bar, Run > Run Configurations... then right-click on Maven Build and configure the New_configuration freshly created:
Select an arbitrary Base directory
Fill the Goals with install:install-file
Add parameters for each required parameters, without the -D prefix (e.g. file as Parameter name and /path/to/file as Value and so on for groupId, artifactId,packaging and version).
And run this configuration. Or... just install Maven.
The install command automates the creation of a folder structure in ~/.m2 and pom.xml file for the dependency artifact. This can be done manually. OR You can simply copy the ~/.m2/{group}/{artifact} folder from a machine that does have mvn installed.
Edit: This tool will help you find public repositories for a given dependency.
Edit2: See http://maven.apache.org/guides/mini/guide-coping-with-sun-jars.html for an explination of the process of installing dependencies manually. Note that most sun jars are now available in the java.net repository http://download.java.net/maven/2/

Running a Maven project with many dependencies

I'm hacking on a Maven-based project with a lot of dependencies; the project is normally meant to be developed in Eclipse but I need to work on it from the command line.
How to build+execute the project in a sane way? Something like mvn run, but of course Maven is not meant for running Java projects (for some reason).
The problem is specifying all the dependencies on java's commandline, I don't even know how to autogenerate that. I can currently deal with it using the assembly:single maven plugin (using the jar-with-dependencies descriptor) which will package the dependencies to a single .jar for me.
However, there really is a lot of dependencies and the assembly phase can take about two minutes, greatly disrupting my hack-test cycles so I'm looking for other ways to run the project with minimum build overhead - any recommendations, please?
Note: One possibility is running it in Eclipse once and capturing the java commandline. However, that's just a one-time hack, not a general solution in case I change pom.xml later or come to another project from the suite without Eclipse access anymore.
Have a look at the maven exec plugin
mvn exec:java -Dexec.mainClass="com.example.Main"
if you do this frequently, you can of course configure it via plugin configuration.
Regarding finding out project dependencies - you can use maven dependency plugin
http://maven.apache.org/plugins/maven-dependency-plugin/list-mojo.html
If you want to put them into file it'd be smth like
mvn dependency:list > dependencies.txt
See this question: How can I create an executable JAR with dependencies using Maven?. You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest.
I see three solution to this:
onejar-maven-plugin - faster than assemlby with jar-with-dependencies descriptor
With onejar-maven-plugin, you'll (...) get a nice clean super jar with the dependency jars inside.
Spring Boot Maven Plugin - but this is dedicated to Spring projects
Maven Assembly Plugin with custom descriptor. This custom descriptor should grab all dependencies into lib folder, maven-jar-plugin should set Class-Path in Manifest.fm according to this new location. After this you can simply execute your program or zip your jar with lib folder as distribution.
After this is possible to run your program on your computer or any other with one command:
java -jar myjar.jar

JAR in project-specific local Maven repo, how to see library in Eclipse project?

I've been trying to add a custom .jar (ftp://ftp.ncbi.nlm.nih.gov/pub/eutils/soap/v2.0/java/axis2_1.5.2_jdk_6.0.12/eutils_axis2.jar) to a project that doesn't have a central corporate maven repository and that instead will have the custom JARs checked into SCM in the project directory tree. I was following this post to make it happen: Maven: add a dependency to a jar by relative path (awesome post btw).
What I did was:
Add local repository to pom.xml
install the file into the local repository
Add dependency to pom.xml
Based on what I see in m2eclipse, the library has been successfully recognized by Maven and added to the dependency list (or it'd be called ? : ? or something similar)
The problem is that Eclipse still doesn't see the referenced lib, so this still fails:
import gov.nih.nlm.ncbi.www.soap.eutils.*;
Pardon my maven newbiness, but what are changes / next steps I need to make to get to:
Have Eclipse see the library so that autocomplete works (and the import can be resolved)
Be able to compile the project
Be able to execute the jar produced by mvn package?
Thanks!
If you see the JAR under "Maven Dependencies" in your project, Eclipse should be able to see and use it. If it's not there, then m2eclipse wasn't able to resolve the dependency properly.
If it is missing, m2eclipse was unable to download the dependency from your local repository for some reason. Check the Maven 2 Console for errors and the Problem View.
Lastly, the JAR itself might be corrupt. Maven doesn't check for that, it simply adds the file to the classpath. If Eclipse can't open the JAR, you can also get the errors you mentioned.

Categories

Resources