I have a project in NetBeans6.9.1.
It works fine from inside the IDE.
But when I try to run the jar, that NetBeans has automatically created under the dist directory, I get a NoClassDefFoundError
for classes inside my project. What am I doing wrong? Should I be using Ant or something (don't know Ant)
In eclipse I do a "create runnable jar", and the jar runs without issues. Is there something equivalent in NetBeans?
UPDATE: In the dist/myJar, I extracted the jar, and in the manifest, the current path and the root path of my project were missing. I added them manually, and re-created the jar from command line. And it works. But why doesn't NetBeans add these in the classpath of the jar's manifest.I do not understand
UPDATE 2 I found the problem. I think this is a serious NetBeans bug. I had done refactoring and changed the package names from myPackage.model to mypackage.model. But NetBeans did not do it correctly. It indeed changed the name of the package to mypackage as seen in the tree navigator, but the package name inside the file remained as myPackage.
The program executed fine inside the IDE and no errors were reported (although all the classes were declaring as belonging to myPackage and in the tree they were under mypackage), but when I tried to run the jar inside the dist directory I got a class not found exception. Today I noticed that the class was reported as myPackage/model instead of mypackage/model. I looked into the classes and the refactoring completely meshed up everything. I mannually changed the package name from inside my classes from myPackage to mypackage, and corrected all the imports (which were importing myPackage). Is this a known issue of NetBeans????
Thanks
With NetBeans your Java project will be either Maven or Ant based. What you want to do is to create a "fat" jar that has all it's dependencies included. For Maven you can add the following lines to your pom to accomlish that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>package.and.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
So for Maven it's a two-step thing:
You must tell Maven to produce a "shaded" (including dependencies) jar,
and you have to specify the main class (the one containing the static main(..)method to run.
If you're using Ant you might want have a look a this blog post.
I've been having this same prolem, and all I can say is that it seems to be a glitch with NetBeans 6.9.1, as it worked in 6.8 and 6.9.
For convenience's sake, you can open it in WinZip or WinRar and just change the manifest file from there without having to jar it yourself. That is what I do.
I solved following this post in an external blog:
You have to add two plugins (maven-assembly-plugin and maven-jar-plugin) to the build section of your pom.xml file. You build section would be similar to this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>(select version, i.e: 2.3)</version>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>(select version, i.e: 2.3.2)</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
NetBeans creates a self sufficient JAR file for your project when you execute the "Clean and Build" command from Menu or Tool Button.
Ideally the classpath of the Jar file shall not contain root folder of the project, NetBeans adds library JARS if you have added them to the project in the Class-Path property in the manifest.mf file automatically.
If you have any custom requirement you can even modify the manifest.mf file which gets included in the built JAR. The manifest.mf file is available from the Files panel in the project root directory.
For making the JAR executable with the java -jar command you shall specify the Main class in the project configuration. (If the project was created through NetBeans project wizard then this class is already defined, otherwise we have to do that manually)
Specify the main class through Menu option as follows:
File > Project Properties > Run (Category) > Main Class (textbox)
Please give details of your project like
Is the project created from NetBeans New Project wizard?
Is the project Ant based or Maven based?
Is the project free form project created from existing source?
This additional information will help me answer the question in specific details. Hope this has helped.
with regards
Tushar
Related
I am having an executable jar ,when I try to run this jar ,it is giving me an error saying one of the bean is not available.But however, if i unzip this jar file
and replace any of the class file and zip it back, the application starts without any issue.No matter which class is replaced, after zipping it back, the jar starts without any issue. Can anyone please tell what could be the issue here?
Please note that When the jar is created in local, it doesnt have any issue. But when I download the jar that had been uploaded to jenkins and try running it, It is giving this issue. As mentioned earlier if I replace any class file inside this non working jar and zip it back, even this jar starts working
Your char in local environment cold be placed in such manner that the executable jar can "see" the necessary dependencies.
You need to package all necessary dependencies into your executable jar in order to make it run when you move your jar to other location. You can utilize maven-assembly-plugin to solve this, just add the following in your maven's pom.xml in your build setup:
<!-- BUILD SETUP -->
<build>
<plugins>
....
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>path.to.your.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Hope it helps!
I am new to java and currently doing a java project. This is the instruction on how to submit the project. Could anyone tell me how to do this in either intelliJ or eclipse?
Please submit a Java Archive (jar file containing all java classes you have written). Your jar file should
contain:
a) Contain source code for all classes
b) Contain executable (byte code) for all classes
This question has been already answered here multiple times.
Since you also need to include the sources, you will have to change the resource patterns so that .java files are also copied to the compiler output and are therefore included in the .jar file.
By default .java files are excluded from copying, so you need to remove !?*.java; pattern that excludes them:
!?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj
becomes
!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj
Don't forget to change it back for your real world applications.
If you need a sample project for IntelliJ IDEA, you can download it from my another answer. It shows a more complicated example where additional dependent jars are included into the project using different ways (single jar and multiple jars).
If you you are using eclipse you can follow one of the below ways as per your requirements.
To Export the project you are working as jar:
1) Right Click on the project you are working > Select Export from context menu.
2) Select Java > JAR File
3) Select the project to export as JAR. Enter the name for the generating jar file and then click on finish.
If you are using Maven do following configurations to the pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.xxxxx.xxxxx</mainClass>
</manifest>
</archive>
<outputDirectory>C:\tmp</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<outputDirectory>C:\tmp</outputDirectory>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
I've a simple program build in IntelliJ and using maven that uses the dependency io.netty. I've added to my POM file:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.0.Beta1</version>
</dependency>
In order to compile and get a jar file I usually do:
Clean
Compile
Package
However I noticed that the dependency is not added to the jar, neither existing in the target folder (Or in any of it's sub folders) or added to the resources folder like usually happens.
In order to have the io.netty library to be added to the jar I have tried:
Setting the scope to provided and to compile.
Re-importing the pom file.
Deleting io.netty folder in the .m2/repository/ folder.
I have several other libraries linked including:
mysql-connector-java
slf4j-simple
trove4j
Thanks for reading.
For some odd reason I had changed my maven configuration a while ago. While I had not added any new libraries, the old ones still had their classes laying around therefor still being added to the jar.
I solved this issue by changing the build in my pom to:
<build>
<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.domain.Program</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Used as reference: http://mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/
Maven doesn't package all dependencies into a jar by default. You can use the assembly plugin to build a "jar with dependencies, as seen here:
How can I create an executable JAR with dependencies using Maven?
This question already has answers here:
Generate test-jar along with jar file in test package
(2 answers)
Closed 8 years ago.
I have maven project with main and test subfolders under folder src.
However, when I build maven jar, I dont see test files in jar file (I do see files that were there in src/main; I dont see test files from src/test)
[xx#localhost target]$ jar tvf cbm.jar | grep *Test*
[xx#localhost target]$
Here is my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<!-- https://maven.apache.org/plugins/maven-assembly-plugin/examples/index.html -->
<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>Main</mainClass>
</manifest>
</archive>
<finalName>afloat</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
EDIT
#azurefrog: I am having issues in production and need to run the test cases on production machine.
It is normal behavior. Maven crate jar from src (only production code).
If you want add test .class to jar you have to create your own maven plugin.
Check this
Guide for maven plugin development
Maven does not package test classes. What you would have to do is copy the maven project to the production machine and run mvn test from that machine.
The reason Maven behaves like this is that everything under src/test is not supposed to be used in production. It's test code, resources, etc. One common use of this is to put configuration files under src/test/resources such as Spring context files. If these files made it into the official jar file, it would be very bad in many, many cases.
That said, what you can do if you want to run test code is follow this process:
Build a maven project that uses this one as a dependency.
Write a some test code, either as JUnit with a standalone JUnit runner, or a simple class with a main method.
Use the Maven assembly plugin to generate a jar that has all dependencies in it. This GitHub project of mine uses the assembly plugin to build an uberjar for a groovy script.
I'm trying to create automated solution for building with maven. My vision is to have a Maven build, which creates JAR file from my project and then just copies all the dependencies as JARs to some sub-directory in "target" folder.
I do not want to use Shade or Assembly (so I do not want to extract the content of other JARs and include it in one "super-JAR", because the project is more complicated and it breaks when I'm including all the JARs in one file).
How can I do such build POM?
I don't see here any problem. Just create maven pom.xml with <packaging>jar</packaging>
By default it should not pack into your jar all dependent libraries.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Related with your latest comment, use this plugin to add the main class in the manifest:
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
You can use the maven-assembly-plugin to create a .zip file from a selection of other files in your directory. I have used this method to create distribution zip files for a project. There are several examples available in the maven-assembly-plugin documentation.