Simple Json ClassNotFoundException when executing JAR built by gradle - java

I recently developed a simple JavaFX Game which used Simple Json. The dependency has been defined in Gradle dependencies and the game runs fine with Gradle run command. But when I create a jar file using - Gradle Jar and run it the following error shows up:
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
Here is the picture of my build.gradle file:
How do i Fix this?

This is the default package behavior. The package phase of tools such as Gradle and Maven only copy the application sources (Java classes and resources from src directory), and NOT include files from dependencies.
Therefore the jar file produced by default does not have dependency classes or resources.
There are many solutions to run a java program from the jar file with dependencies:
Place the app jar as well as dependency jars in a directory (lib) and add it to the classpath when running the main class.
java -cp lib/*.jar com.game.MainClass
Create a fat (uber) jar. Gradle plugins like Shadow can generate a single jar with all dependencies packed in it. Just specify the main class in Manifest.
java -jar myAppFat.jar
Use plugins like Launch4J to generate Windows executables. This is great if you want to ship simple "click to run" apps to users.

Related

Creating a jar from maven project Intellij

I created a new maven project in IntelliJ and set packaging to jar and when I build it, there is a jar file target folder. But when I run the file I get the error as
no main manifest attribute, in QeToolHelper-0.1-SNAPSHOT.jar
I have created jar file using maven-assembly-plugin. I have added these lines in pom.xml for that.
Tried few methods described in other similar questions but to no avail.
Have you looked at SpringBoot and associated plugins. It can be used to build a standalone java applications.
With regards to your specific issue, that error is symtomatic of a failure to declare main class in your manifest. The main-class attribute in the manifest tells the jvm what class to load from the built jar. Looks like the settings in your maven config are failing to generate the resources correctly.
I was able to solve this issue by creating jar package from maven.
Running the build package created the jar in target directory.

java.lang.NoClassDefFoundError: com.google.maps.GeoApiContext$Builder

I'm using the Java library for the Google Maps API but cannot find the code at runtime. I've added the repository and dependencies to build.gradle as detailed in the readme. Everything compiles fine, the import statement autocompletes, I can open the source code in Eclipse through "View Declaration," and I can see Google Maps in the Java build path.
import com.google.maps.GeoApiContext;
// Throws exception!
GeoApiContext.Builder builder = new GeoApiContext.Builder();
Why is this library visible at compile time but not runtime?
ETA: The application is packaged as an EAR file and deployed to a web server.
With the scarse details you provided, my guess is that you built a jar file and then ran java -jar myApplication.jar.
The default jar file only contains the classes of you own application, and not its dependencies. If you want to make an executable jar, you have several options. Some of the more popular are:
Use the shadow plugin, which will repackage the jar to make it "fat" with all its dependencies.
Use the standard distribution plugin that will create a zip (or tar) of your project and its dependencies. It will also a script for setting the classpath correctly.

How to export external jars along with the project while creating jar of a project [duplicate]

How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?
You have 2 options here:
extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
link the dependent jars via the Manifest.MF and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into out\single and out\linked directories.
Relevant configurations:
If you are using maven to build your application then this is not the correct way to add external library. You should either
Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
Use system path like explained here.
Option 1 is prefered since you don't have to keep jar in your project.

Using JPA, JavaFX and Maven, error of NoClassDefFoundError: javax/persistence/Persistence Intellij

I did a Maven project and I use the JavaFX style in Intellij IDEA, I had all the jar necessary to run my project and when I execute in IDE everything works perfectly but when I build the jar file and run appear this error:
You just package your code and not dependencies into your jar. So the dependencies are not in the classpath when you just run your jar file.
There are different ways of how to package the application. For example create an exe or add the necessary jars to the classpath.

How to create a jar file for the eclipse plugin project that should be able to download and run without eclipse software?

I m doing an eclipse plugin project to create an IDE. I need to create a jar file for the plugin project in which i have four plugin packages which was created by me. Now I need these to be created as a single jar file and the user should be able to download the jar file and run my plugin project without the eclipse software.
EDIT-
You cannot run Eclipse plugin outside of Eclipse, because you need the Equinox runtime container. you could run a plugin using the eclipse executable, and as an application, see:
http://wiki.eclipse.org/FAQ_How_do_I_create_an_application%3F
You're effectively creating an an org.eclipse.core.runtime.applications extension point.
You could also publish a plugin as part of an Eclipse application and then export it as an executable so that it can be run aside from Eclipse. This still bundles the Equinox runtime and plugin together though.
Also, check out "Running it outside of Eclipse" section here.
-END of EDIT
Generally, all you need to run an executable jar file is the jvm (java) and your code with all the classpath dependencies. You can use "Runnable Jar Export Wizard" available in Eclipse IDE when you right-click your project.
You can put all the dependencies inside your jar (for example you can create a lib directory in your project and put all your dependency jars inside). Also you will need to specify the dependecy location in the MANIFEST file that will be generated for your executable jar (if you use the wizard the MANIFEST file will contain your dependencies).
To run your executable jar you will need to execute:
java -jar jar-file
Good Luck!

Categories