Exclude JUnit from Eclipse exported JAR - java

I'm using Eclipse JUnit integration which includes the JUnit library automatically into my project. The problem is that when I export my project using the Runnable JAR file destination, it includes JUnit.
Is there any way to exclude JUnit (and ideally the tests too) from the exported JAR?

If you're creating your JAR by right clicking on your project and selecting export and then picking JAR File, you can remove your tests from the export by unchecking your test folder. See this related discussion and this example.

I've found a solution to the problem by using Ant within Eclipse and the following build.xml:
<project>
<target name="jar">
<jar destfile="out.jar" basedir="bin">
<zipgroupfileset dir="lib" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="com.example.Main" />
</manifest>
</jar>
</target>
</project>

You can remove the JUnit package from '.classpath' file.Then export the jar file again

Related

How to convert a java project to a plugin project in Ant?

I am currently trying to write a build.xml which will convert a normal java project say com.example.normal to com.example.plugin.jar.
I have the code for a basic build.xml which creates a jar of the source project. But normally creating a jar file is not the same as creating a plugin jar file. For this reason I need to create a plugin jar file using ant and not just a normal jar file which cannot act as a plugin.
This is the example code snippet for creating the jar file.
<jar destfile="generatedPlugins/com.example.normal.jar">
<fileset dir="JavaSource/com.example.normal"/>
</jar>
Manually, I can create a plugin with the following steps:
Right Click on project > Export > Plugin Development > Deployable
plug-ins and fragments.
In other words, I just need to automate this process using Ant. Any idea how to proceed?
Thanks!
This can't really be done with Ant alone. You should use Tycho or PDE Build to build bundle (plug-in) JARs. Take note that Tycho is the modern, preferred option; I'm not sure that PDE Build is even actively maintained or used any more.
You could try manually editing the build.xml, adding something like
<!-- This builds a .jar file, Assuming you have a set of .class files
created by some "compile" target. -->
<target name="build" depends="compile">
<!-- We need to set up class path for any required libraries so that we
can add it to the manifest file later. -->
<path id="build.classpath">
<pathelement location="${lib.location}"/>
</path>
<!-- Now we convert the class path we just set up to a manifest class
path - we'll call it manifest.cp. We also need to tell it where our
.jar file will be output. -->
<manifestclasspath property="manifest.cp" jarfile="${jar.output.dir}/filename.jar">
<!-- We just give it the classpath we set up previously. -->
<classpath refid="build.classpath"/>
</manifestclasspath>
<!-- Now we can make the .jar. It needs to know the base directory of
our compiled .class files. -->
<jar destfile="${jar.target}/filename.jar" basedir="${class.target}">
<!-- Set up the manifest file with the name of the main class and
the manifest class path we set up earlier. -->
<manifest>
<attribute name="Main-Class" value="name of main class"/>
<attribute name="Class-Path" value="${manifest.cp}"/>
</manifest>
</jar>
</target>
You can generate Ant scripts from the PDE tools by right-clicking on a relevant manifest file (e.g. plugin.xml) in the project and selecting PDE Tools --> Create Ant Build File.
This link from the Eclipse Mars documentation explains in detail.

Issue with class-path in manifest file during deployment in Tomcat

I am building the java project using Ant and I am using Ivy for the dependency management. There are two options in ant build for resolving the dependencies during the deployment. One is the lib mode, if this mode is enabled, all the dependent jars will be downloaded into project's WEB-INF/lib folder. Other is manifest mode which does not download any jar. During deployment, MANIFEST.MF (which has the class-paths to all the dependent jars) file will be used for resolving the jars.
Project is running properly when it is in lib mode, but when manifest is enabled, the build is successful but the deployment fails.
Please tell me how to fix this or what could be the reasons for this?
Note: Tomcat server is VWL enabled by default.
Thanks in advance.
Without an example one must speculate what your code looks like. The following example demonstrates the use of the ANT manifestclasspath task to construct a classpath suitable for a jar manifest file. The ivy "retrieve" task populates a relative direcotory containing the jars:
<target name="build" depends="compile">
<ivy:retrieve pattern="${dist.dir}/lib/[artifact].[ext]"/>
<manifestclasspath property="jar.classpath" jarfile="${dist.jar}">
<classpath>
<fileset dir="${dist.dir}/lib" includes="*.jar"/>
</classpath>
</manifestclasspath>
<jar destfile="${dist.jar}" basedir="${build.dir}/classes">
<manifest>
<attribute name="Main-Class" value="${dist.main.class}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
</jar>
</target>
The following is a detailed example:
How to avoid copying dependencies with Ivy

How to set up a Java workspace without an IDE

I usually code with IDEs like Intellij Idea or Eclipse, but due to several reasons, I no longer have access to an IDE, and I'd like to be able to code in Java on a remote Linux machine through a ssh terminal.
Making simple progams with only a few classes is easy, but some of my projects have several libraries and several separate .java files. I also need to export to a .jar file.
For example, I have the following file organisation:
project/
src/
a.java
b.java
c.java
libs/
lib1.jar
lib2.jar
out/
export_here.jar
someconfig.conf
The java app consists of the a, b, and c .java files, uses libraries lib1 and lib2, and the file someconfig.conf needs to be inside the export jar.
I want to know how to easily compile and build a project such as this.
In other words, I just want to know how to export my project into a runnable jar the right way.
I expect this can be done with a few commands. If so, I plan to make a shell script to automate everything.
Thanks in advance!
As suggested by other users you need to use a build management tool to do this like Ant, Maven etc. I have used Ant quiet frequently to do these kind of automated tasks. In order to install and Use Ant you can refer How To Install Apache Ant
After that the main task is to write your automation script and that is called a build xml in ant world. Here is a sample build.xml file you can refer to start with:
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="oata.HelloWorld"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>
</project>
And for more information on the above sample You can visit this
In General you can read more at How to create build.xml
After creating your build.xml you can run ant by ant <path to build.xml> or ant in case your build.xml lies in current directory
Hope this helps you in right direction

Java project build jar and depenedencies

I am trying to create an executable from a Java project in eclipse. I understand that the first step would be creating a JAR file so after searching the site I was able to compile the following Ant build.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="GraphBuild" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<manifest file="MANIFEST.MF">
<attribute name="Main-Class" value="GraphEditor" />
</manifest>
<jar jarfile="GraphEditor.jar" basedir="." includes="*.class"
manifest="MANIFEST.MF" />
</target>
</project>
When I run the GraphEditor.jar using the following command
java -jar GraphEditor.jar
I receive java.lang.NoClassDefFoundError because I have some referenced libraries in my project and it seems that the loader is not able to find them. I tried setting the -cp variable with no luck.
Appreciate you help.
With maven you can easily create an executable jar with all dependencies.
Maybe you want to use maven instead of ant.
How can I create an executable JAR with dependencies using Maven?
You need to attach the dependent jars appropriately and point to directory of your class files correctly.
Something like:
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="${lib.dir}/lib1.jar ${lib.dir}/lib2.jar"/>
</manifest>
</jar>
Can you add the Class-Path variable with the dependent jar files with appropriate location and try?
If I read your question properly I would first like to say you cannot use the -cp and -jar. If you declare a classpath when creating the jar (in ant #see http://www.tomred.net/tomred-java-jar-meta-inf-manifest.html) for example so that it is in the MANIFEST then you need not include the -cp.
If you want to test your jar with a classpath you should do as you have but swap the -jar for -cp as below.
java -cp lib/;deps/;GraphEditor.jar org.package.to.RunClass
Simply -jar and -cp are multiply exclusive. Note: ";" separation in windows ":" in linux

Class-Path in jar file with combined libraries

I am trying to create a jar file that is "self contained" concerning the libraries it needs.
Therefore, I created the following ant file:
<project name="srv" default="prod">
<target name="prod">
<jar destfile="build/ServerApplication.jar" basedir="bin/">
<restrict>
<name name="**/*.class"/>
<archives>
<zips>
<fileset dir="lib/" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
<manifest>
<attribute name="Class-Path" value="." />
<attribute name="Main-Class" value="my.package.ServerApplication" />
</manifest>
</jar>
</target>
</project>
But, when I try to launch the application using
> java -jar ServerApplication.jar
I get an error
No suitable driver found for jdbc:mysql://localhost/db?user=root&password=
I reckon this is because the com.mysql.jdbc package inside the jar can't be found. Why is this? When I set the Class-Path to ., shouldn't the jar be able to find all classes inside that jar?
I opened the jar in a decompiler, and all the classes are where they need to be, see here:
What is going wrong here? My MANIFEST looks like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_07-b10 (Oracle Corporation)
Main-Class: my.package.ServerApplication
Class-Path: .
You must explicitly write the names of the jars if you want them included separated by spaces. Otherwise I would recommend simply launching your jar and providing the class path as a parameter manually:
java -cp . -jar ServerApplication.jar
See here for more information.
I think you should avoid setting classpath at all, and since you are packaging everything in uber-jar - it should work.
Maybe you could try the excellent eclipse plugin which is fatjar.
It had used it many times for including all dependencies of Java Swing based applications.

Categories