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
Related
When I run my jar file via terminal I have no issues. However, when I put it on my client's computer it gave me the following error (when I tried it through terminal:
Error: could not find or load main class base.Main.
Caused by: NoClassDefFoundErrors javafx/application/Application
Normally I'd assume Main wasn't included. However, I exported the ANT XML as well (included below) and base.Main is where my main should be. I'm also confused because I can run the executable jar through the terminal on my machine with no issues.
I'm using the export runnable jar feature in STS. I've tried exporting multiple times. I've tried changing export settings (cycling through the options). I've verified my client had a valid JDK even though I doubt that would be the issue.
*I've spent two hours searching through answers. While I'm sure there are similar questions out there, I have not yet found when that exactly matches the issue I'm having. If you've seen one feel free to post it.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for
Project BLIS with Jar-in-Jar Loader">
<!--this file was created by Eclipse Runnable JAR file Export
Wizard-->
<!--ANT 1.7 is required-->
<!--define folder properties-->
<property name="dir.buildfile" value="."/>
<property name="dir.workspace" value="${dir.buildfile}"/>
<property name="dir.jarfile" value="/Users/me/Documents"/>
<target name="create_run_jar">
<jar destfile="${dir.jarfile}/BLIS.jar">
<manifest>
<attribute name="Main-Class"
value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
<attribute name="Rsrc-Main-Class" value="base.Main"/>
<attribute name="Class-Path" value="."/>
<attribute name="Rsrc-Class-Path" value="./ spring-jdbc-3.2.11.RELEASE.jar spring-jdbc-3.2.11.RELEASE.jar mysql-connector-java-
8.0.13.jar joda-time-2.10.1.jar"/>
</manifest>
<zipfileset src="jar-in-jar-loader.zip"/>
<fileset dir="${dir.workspace}/BLIS/bin"/>
</jar>
</target>
</project>
I know that expecting things to "just work" is unrealistic. The problem is I'm not sure where I messed up. Is it because Main-Class is the resource loader instead of base.Main? I haven't used this exporter before, but I assume it "loads the resource" before checking if main exists.
Any help would be appreciated.
This must be mainly due to the unavailability of javafx.application.Application class in the classpath in the client's computer even though it is there in your machine.
Oracle by default contains the javafx, but OpenJDK does not. If it is the OpenJDK, then you will need to separately install openjfx.
Better check Java version in both machines and compare.
Hope this will help you in resolving your issue.
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.
I am new to Java & Eclipse. I have a window-based app with dialogs, that works fine inside of the IDE. However, when I try to export a JAR to make it executable outside the IDE (and thus redistributable), I execute it with the command:
java -jar MyLibrary-app.jar
I get the exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons:
no swt-win32-4427 in java.library.path
no swt-win32 in java.library.path
Can't load library: C:\Users\jay.imerman\.swt\lib\win32\x86\swt-win32-4427.dll
Can't load library: C:\Users\jay.imerman\.swt\lib\win32\x86\swt-win32.dll
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:327)
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:236)
at org.eclipse.swt.internal.C.<clinit>(C.java:21)
at org.eclipse.swt.widgets.Display.<clinit>(Display.java:138)
at org.jimerman.MyLibrary.MainWindow.open(MainWindow.java:51)
at org.jimerman.MyLibrary.MainWindow.main(MainWindow.java:40)
I don't know what the difference is between exporting a JAR and a Runnable JAR, I tried both. On the former, I also tried including the src from the swt project, as well as another Java class library project that I reference. I am used to visual Studio, and the Setup project, which detects dependencies and collects references for a deployment. What am I missing? If I were to use something like Maven, how do I even go about learning what all the terminology and concepts are, to even understand what it means and what I need to build a redistributable file?
You have an option to export to runnable-jar in eclipse and save ant.xml. It should look like
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project MyLibrary-app">
<!--this file was created by Eclipse Runnable JAR Export Wizard-->
<!--ANT 1.7 is required -->
<target name="create_run_jar">
<jar destfile="MyLibrary-app" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="MainClass"/>
<attribute name="Class-Path" value="."/>
</manifest>
<fileset dir="bin"/>
<zipfileset excludes="META-INF/*.SF" src="{Eclipse_HOME}/plugins/org.eclipse.swt.win32.win32.x86_x.x.x.vxxxxa.jar"/>
</jar>
</target>
</project>
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.
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