Netbeans 8.2 and maven - no main manifest attribute error - java

i am using netbeans 8.2 to create a simple java project. i've chosen Maven-Java Application. I've created a simple hello world java class. I've selected the java class as the main class from Properties -> Run and if i run the project from Netbeans, it print the hello world. After that, i do "Clean" and "Build with dependencies" the editor create a target folder with the jar file. When i go to that folder and execute "java -jar XXX.jar", i get the no main manifest attribute error. Do I have to manually update the POM.xml file? Am I missing a step?

I am answering my own question.
Apparently you have to manually update the pom.xml file. I've added the dependency for maven-jar-plugin and added below section to the pom.xml file.
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.XXX</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Related

openshift: no main manifest attribute in jar maven project

I am trying to deploy a maven project in minishift using the openjdk8 source to image strategy. The application is built and deployed, however, it fails at run-time with the following error:
Starting the Java application using /opt/run-java/run-java.sh ...
exec java -javaagent:/opt/jolokia/jolokia.jar=config=/opt/jolokia/etc/jolokia.properties -XX:+UseParallelGC -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MaxMetaspaceSize=100m -XX:+ExitOnOutOfMemoryError -cp . -jar /deployments/app-1.0-SNAPSHOT.jar
no main manifest attribute, in /deployments/app-1.0-SNAPSHOT.jar
I have this on my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I thought adding the manifest entry on the maven pom was enough.
Do I have to provide a separate manifest file?
Do I have to provide extra VM argument to specify the main class? if so, how do I do that in openshift?
Any other better alternative?
Add a new environment variable JAVA_MAIN_CLASS to your deployment config or via the Openshift user interface.
JAVA_MAIN_CLASS=com.mypackage.MyClass
That should resolve the issue.

PDFBox with Maven - java.lang.NoClassDefFoundError

When installing PDFBox with Maven, it places the libraries in the ~/.m2/repository directory.
My program complies fine with mvn package.
When I try to run it with
java -cp target/java-project-1.0-SNAPSHOT.jar com.example.sub.App
then I get
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
Should I also be specifying the libraries in ~/.m2/repository as part of the classpath? This seems a bit too tedious to do it this way. What is the recommended way to specify the classpath of my PDFBox library while using the library location(s) of Maven?
I wasn't able to find a nice solution with leaving the JAR files in ~/.m2, so the answer below is a workaround based on some other answers. I will be including more clarification though for those who are new to both PDFBox and maven as I am.
1) Add the following to your pom.xml file. If you already have a <build> and <plugins> section, just add the <plugin> section below to that. Otherwise you may need to add the whole code below within the <project> element:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>**REPLACE THIS WITH THE FULL URI OF YOUR MAIN CLASS**</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2) Make sure that you replace the text in the <mainClass> element to match the situation. For example, if your main() method is located in an App class in App.js, and your package name is com.example.sub. Then the above element should read:
<mainClass>com.example.sub.App</mainClass>
3) To compile your app, run mvn package
Note: I have seen some references online using mvn clean compile assembly:single instead of mvn package. I am not sure what the purpose of this is when mvn package seems to run just fine for me.
This will take your project and all of your dependencies and create a single JAR file in the target directory called something like this:
java-project-1.0-SNAPSHOT-jar-with-dependencies.jar
4) To run the project you can do this:
java -cp target/java-project-1.0-SNAPSHOT-jar-with-dependencies.jar com.example.sub.App
Make sure that you modify the above line to it your situation. In other words you may need to change both the name of the jar file and the name of the URI for your main class.

Setting classpath for a java project with many libraries and external resources in command line

I have the following project. It has some property files in the conf folder, some data in the data folder, some jar files in the lib folder and also some external libraries that are not shown in the photo due to size limitation. Imagine I want to run the RecDriver class. How exactly should I set the classpath so that I can run it in command line? This is how I did it but it does not work as it cannot fine some other files in the project.
C:\Users\myUserName\Downloads\librec-2.0.0\librec-2.0.0\core\src\main\java\net\librec\tool\driver> javac RecDriver.java
The project can be downloaded here:
https://github.com/guoguibing/librec
You can use bin/librec or bin/librec.cmd to run it from commandline.
If you want to build your launch command you can see those start scripts and adapt them for your purposes.
To run your app through command line, once you have the .class files in some dir (usually build) all you have to do is run your application with java -cp "path where jvm can find every .class that you project needs" MainClass.
The -cp flag only tells where to look for compiled .class files, since you are using IntellIJ you can see the command it runs when executing your program, there is a class path that it uses.
Class Path points to where your .class files are, they can be in separate folders, but you need to include every dir when giving the class path, separated by ";"
Example taken from another question in SO.
java -cp "Test.jar;lib/*" my.package.MainClass
Three things to do:
Use the Maven Shade Plugin to create a fat jar (jar with dependencies)
Use the Maven-Jar-Plugin to make the Jar executable
Set <project><build><finalName> to ${artifactId}
Now, after your build ran successfully, you can run your app with
java -jar target/YourArtifactId.jar
(Substitute your project's artifactId for "YourArtifactId")
Okay, here's the full setup.
Add a build section like this to your pom.xml (merge it with any existing one).
<build>
<plugins>
<!-- number 1 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
<!-- number 2 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<!-- number 3 -->
<finalName>${project.artifactId}</finalName>
</build>

Combinate .jar library with my .jar

I have a project in javaFX and I'm using ControllerFX Library , the problem here in the .jar file it's necessery to keep .jar file of the library with my .jar project.
is there a way to combine .jar of library with .jar of project using intellij idea ?
does Maven help in this situation? (I don't know exactly what maven can do)
thanks for answers
The jars that you use with your project are called as the dependent jars. You can bundle them while creating a jar of your own project.
Intellij Approach
File
|
Project Structure
|
Artifacts
|
create new artifact choose --> jar --> From modules with dependencies.
Next goto
Build
|
Build artifacts --> choose your artifact
Maven Approach
Create a Maven project and keep the packaging as jar.
Add the executable jar plugin and add your Main class details and your dependent jars
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

creating jar file of maven projects with netbeans

I am using netbeans to work on a project. I am using svn, so that I can commit to newer versions and revert to an older version in case I mess up. Now I want to create a jar file of this project but the build tab in the project properties does not display the packaging option, it only displays one option and that is of 'compile'.
If I create a new project (Java Application) it does show the packaging option and then I can easily create a jar file, but this maven project that I am working on does not work like this.
Please suggest me a way to create jar file out of my maven project.
Thanks,
Based on #yatskevich answer, you could go to your NetBeans Project Properties > Actions and add package to the Execute Goals of the following Actions:
Build project
Clean and Build project
Build with dependencies
Plus any other you feel should also package
I did this on NetBeans 7.2.1
PS: this will create the JAR on every change you make, so choose wisely where to add package. I don't mind it building a JAR for small projects on every build.
Open cmd (if you are on Windows) or any shell (if you are on Linux)
Navigate to your project directory (use cd command)
Run mvn clean package there.
Your jar will be in <project dir>/target.
Include maven-assembly-plugin plugin in your .pom file. It will instruct Maven to assemble your application with all it's dependencies.
Later when you will build your Netbeans project you will see your newly builded jar with all it's dependencies.
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.my.class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Check out to learn more about creating Jar file with Maven: https://javatutorial.net/create-java-jar-file-with-maven

Categories