Creating a jar from maven project Intellij - java

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.

Related

Java Could not find or load main class when running jar file

I have a springboot application, it runs just fine in intellij. I'm trying to create an executable jar to run the application on other machines. I followed this
tutorialhttps://www.jetbrains.com/idea/guide/tutorials/hello-world/packaging-the-application/
down to every step, and managed to build my jar. but when i try to run the jar with
java -jar nameofjar.java
I get a Could not find or load main class error.
I unzipped the jar file with java c and the classpath correctly points to the main class, so im really lost as to what i have to fix here to get java to detect the main class.
I've tried a bunch of solutions from here:
Error: Could not find or load main class in intelliJ IDE
such as deleting .idea or Rebuild Project or doing mvn clean package but nothing seems to work.
Using java -c to open the jar file and verify that the main classpath is indeed correct
Main class and file structure:
This run config works perfectly fine in intellij
here is my manifest.mf:
Manifest-Version: 1.0
Main-Class: com.owl.PosApi.OwlPosApiApplication
If you build jar artifact using IntelliJ IDEA, it may be incomplete or manifests from the other included dependencies can override your custom one with the main class defined.
Related answers:
https://stackoverflow.com/a/42200519/104891
https://youtrack.jetbrains.com/issue/IDEA-116209#comment=27-656589
https://stackoverflow.com/a/45169655/104891
https://stackoverflow.com/a/42660624/104891
The proper solution would be to build the artifact using Maven. It may work out of the box with mvn clean package or you may need to modify pom.xml to build the fat jar with all the dependencies per this document.

How do you include a jar in the classpath so Maven recognizes it without specifying the jar by name in the POM?

I am working with a proprietary maven plugin that generates connection code for artifacts created in a third party application. The proprietary plugin requires different settings in the POM file depending on the artifact it is generating the code for. I am attempting to make it so my Java application doesn't require a code change in the POM between deploying to TEST and deploying to PROD that way I can use the same jar for my application in the TEST and the PROD deployment. The issue is the connection artifacts and thus the POM code for the plugin will be different between TEST and PROD.
The solution I have in the works is to run the code generating plugin for TEST and for PROD separately then jar the generated code and use a script to include the correct generated code jar in the classpath depending on which environment the Java application jar is being deployed to.
I was able to jar the generated libraries and include the jar in the classpath which makes Eclipse happy with the dependencies, but when I attempt to run a "mvn compile" and generate the Jar for my Java application Maven isn't recognizing the generated code jar I added to the classpath which prevents the spring-boot-maven-plugin from creating my application jar.
All the solutions I have seen so far for getting maven to recognize a jar seem to involve specifying the name of the jar in the POM which I wanted to avoid since the names of the TEST and PROD generated code jars will be different (ex. generated_TEST.jar, generated_PROD.jar) and the whole point of this is to not modify any code between the TEST and PROD deployments so I can use the same Java application jar in both TEST and PROD.
Is there a way to get maven to include any jars on the classpath without having to specify the jar by name in the POM?
EDIT:
This might end up being easier than I thought. I used Eclipse to export the generated code to a jar, then I ended up renaming the jar from Generated.jar to Generated-TEST.jar. I added Generated-TEST.jar to the classpath in Eclipse and tried to run "mvn compile" but the jar was not recognized. When I tried changing the name of the jar back to Generated.jar and added it to the classpath I was able to run "mvn compile" successfully. I am not sure why changing the name of the jar made a difference as I put the appropriate name in the classpath both times.

Simple Json ClassNotFoundException when executing JAR built by gradle

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.

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.

Create Jar With External Jar Libraries (Intellij 2017) [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.

Categories