Deploying a JavaFX 2 application with referenced jars, using Ant - deployment

I have written a JavaFx 2 application (using Eclipse on a Windows platform) and now I want to deploy it to a "clickable" jar-file. My application uses resource-code from a separate jar-file (in Eclipse, this resource code is a separate Project from the JavaFx application project).
With my Ant build.xml, I have compiled the code for both the application and the resource code and created two jar-files:
fxdemo.jar - A jar for my JavaFx application code
guifw.jar - A jar for the resource code referenced by the JavaFx application.
As a last step (I thought), using the JavaFX Ant tasks, I wanted to bundle these two jar-files to a "clickable" jar that starts my JavaFX application. I tried doing just that with the below extract from my build.xml.
<target name="deployFx" depends="fxdemo.jar" description="Releases FxDemo">
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath=".:${fxgui.javaHome}\lib\ant-javafx.jar"/>
<copy file="${fxgui.lib_guifw_path}" tofile="delivery/lib/guifw.jar"/>
<fx:application id="FxDemoGUI" name="Fx Demo GUI" MainClass="com.demo.main.MainGUI"/>
<fx:resources id="jars">
<fx:fileset dir="delivery/lib" includes="fxdemo.jar"/>
<fx:fileset dir="delivery/lib" includes="guifw.jar"/>
</fx:resources>
<fx:jar destfile="deploy/fxDemoGui.jar">
<!-- Define what to launch -->
<fx:application refid="FxDemoGUI"/>
<fx:platform javafx="2.1+">
<fx:jvmarg value="-Xms32m"/>
<fx:jvmarg value="-Xmx32m"/>
<property name="com.util.fxguifw.setup" value="com/util/fxguifw/demo/demo.properties"/>
<property name="user.language" value="en"/>
<property name="user.country" value="GB"/>
<property name="CSS_ID" value="NIGHT"/>
</fx:platform>
<fx:resources>
<fx:fileset dir="delivery/lib" includes="fxdemo.jar"/>
<fx:fileset dir="delivery/lib" includes="guifw.jar"/>
</fx:resources>
<manifest>
<attribute name="Implementation-Vendor" value="${fxgui.vendor}"/>
<attribute name="Implementation-Title" value="${fxgui.title}"/>
<attribute name="Implementation-Version" value="1.0"/>
</manifest>
<fileset dir="delivery"/>
</fx:jar>
However, afterwards when I try to start the application (by either clicking the jar or starting from command line with java -jar appname.jar) it seems as the application can not find the Main class:
JavaFX Launcher Error
Unable to find class: com.demo.main.MainGUI
C:\Program Files\Java\jdk1.7.0_09\bin>java -jar C:\MEKMAN\Clearcase_Views\wmarekm_ss_gambau\amb_c2_prototype\javafx\prototypeGUI\deploy\fxDemoGui.jar
java.lang.ClassNotFoundException: com.demo.main.MainGUI
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.javafx.main.Main.getAppClass(Main.java:506)
at com.javafx.main.Main.launchApp(Main.java:622)
at com.javafx.main.Main.main(Main.java:805)
When I studie the created MANIFEST.MF (in the created jar-file) it looks pretty much as what I expected.
Manifest-Version: 1.0
JavaFX-Version: 2.1+
implementation-vendor: MyVendor
implementation-title: MyfirstJavaFxDeploy
implementation-version: 1.0
JavaFX-Application-Class:com.demo.main.MainGUI
JavaFX-Class-Path: fxdemo.jar guifw.jar
Created-By: JavaFXPackager
Main-Class: com/javafx/main/Main
... but then again, it doesn't work so obviously I have done something wrong.
I also tried including the classes-directory (the output folders from each of the two Eclipse/projects) by adding:
<fileset dir="../guifw/classes"/>
<fileset dir="classes"/>
Then, the launcher does find my main class (com.demo.main.MainGUI) but failes to run correctly because it lacks the -D argument that I tried to specify with:
<property name="com.util.fxguifw.setup" value="com/util/fxguifw/demo/demo.properties"/>
So, if you have read this far, my questions are:
Why can't the launcher find my main class in the referenced jar (fxdemo.jar)?
What have I done wrong when it comes to specify my -D arguments to the application?
Best regards

I studied/tested the fix presented in post (2012-apr-12 03:32) in the link from #Anders Petersson:
Link from #Anders Petersson
From what I can see, this is a workaround that unbudles any used jar-file (in my case; guifw.jar & demofx.jar) within the resulting jar file (fxDemoGui.jar), much like adding the classes-folders from my two Eclipse-projects (as described in the question).
I adjusted the example to my build.xml and got it to work after one slight addition:
<target name="dist" depends="fxdemo.jar">
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath=".:${fxgui.javaHome}\lib\ant-javafx.jar"/>
<copy file="${fxgui.lib_guifw_path}" tofile="delivery/lib/guifw.jar"/>
<fx:jar destfile="predeploy/fxDemoGui.jar">
<!-- ADDITION -> Adds the Launcher's Main class to the resulting jar (com.javafx.main.Main)! -->
<fx:application id="FxDemoGUI"
name="Fx Demo GUI"
mainClass="com.demo.main.MainGUI"/>
<fileset dir="delivery"/>
</fx:jar>
</target>
In the dist-target from the example, I had to add:
<fx:application id="FxDemoGUI"
name="Fx Demo GUI"
mainClass="com.demo.main.MainGUI"/>
Without it, the resulting jar-file did not have the necessary com.javafx.main.Main-class and hence, failed to start.
So, this solution presents a useful workaround for my question 1)
Still, I'd be grateful if anyone comes up with a solution on how to keep the jar-files intact within the resulting jar/file.

Related

Modify Manifest.mf classpath using Ant

I need help to modify the classpath in my JAR manifest.mf file. Here's my scenario:
I am trying to make Netbeans build multiple JAR files for this project. I managed to create an Ant target that build the various JAR files. I used ant-contrib's for task and Netbeans built-in -pre-jar target:
<property name="multipleJar.basePackage" value="com.mycompany.myproject"/>
<property name="multipleJar.baseDirectory" value="com/mycompany/myproject"/>
<target name="-pre-jar">
<!-- Packing mutiple jars -->
<dirset dir="build/classes/${multipleJar.baseDirectory}" includes="*" id="elementid"/>
<for list="${toString:elementid}" delimiter=";" param="filename">
<sequential>
<delete file="dist/multiple/lib/${multipleJar.basePackage}.#{filename}.jar"/>
<jar destfile="dist/multiple/lib/${multipleJar.basePackage}.#{filename}.jar"
filesetmanifest="skip"
basedir="build/classes/"
includes="**/${multipleJar.baseDirectory}/#{filename}/*">
</jar>
</sequential>
</for>
</target>
The JAR generation seems OK, but now i have to modify the Manifest.mf file on the MAIN jar, so these new (dynamically generated) jar files are included in the ClassPath.
Please Advise.
I recommend using the manifestclasspath ANT task.
For an example see:
Cannot find Main Class in File Compiled With Ant

Using eclipse in order to find the opencv native library doesn't work

I'm attempting to use opencv on an ubuntu installation and am following this tutorial. Everything seemed fine and it even listed amongst the installed parts so I proceeded into this tutorial. This went well up until the moment where you have to add opencv as a user library as it was not amongst the (completely empty) list of libraries found. I have looked throughout the opencv folder and can't find anything like a library either am I missing something? Any idea on how to fix this?
Note that it did manage to produce .jar file however the library is still missing. If it matters opencv is installed in usr/local/src.
If in order fix this you require any extra information feel free to ask in the comments.
This might have something to do with the problem:
ulap:/usr/local/src/opencv-2.4.8/opencv/build/bin$ ant -DocvJarDir=path/to/dir/containing/opencv-248.jar -DocvLibDir=/usr/local/src/opencv-2.4.8/opencv/build/bin /opencv_java248/native/library
Buildfile: /usr/local/src/opencv-2.4.8/opencv/build/bin/build.xml
BUILD FAILED
Target "/opencv_java248/native/library" does not exist in the project "SimpleSample".
Total time: 0 seconds
thijs#thijs-ulap:/usr/local/src/opencv-2.4.8/opencv/build/bin$ ant -DocvJarDir=path/to/dir/containing/opencv-248.jar -DocvLibDir=/usr/local/src/opencv-2.4.8/opencv/build/bin /opencv_java248/native/library
Buildfile: /usr/local/src/opencv-2.4.8/opencv/build/bin/build.xml
BUILD FAILED
Target "/opencv_java248/native/library" does not exist in the project "SimpleSample".
Total time: 0 seconds
This is my build.xml:
<property name="src.dir" value="src"/>
<property name="lib.dir" value="${ocvJarDir}"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="${ant.project.name}"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<sysproperty key="java.library.path" path="${ocvLibDir}"/>
<classpath>
<path refid="classpath"/>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="rebuild" depends="clean,jar"/>
<target name="rebuild-run" depends="clean,run"/>
running
ulap:/usr/local/src/opencv-2.4.8/opencv/samples/java/ant$ ant -DocvJarDir=/usr/local/src/opencv-2.4.8/opencv/build/bin rebuild-run
gave me:
clean:
compile:
[mkdir] Created dir: /usr/local/src/opencv-2.4.8/opencv/samples/java/ant/build/classes
[javac] Compiling 1 source file to /usr/local/src/opencv-2.4.8/opencv/samples/java/ant/build/classes
jar:
[mkdir] Created dir: /usr/local/src/opencv-2.4.8/opencv/samples/java/ant/build/jar
[jar] Building jar: /usr/local/src/opencv-2.4.8/opencv/samples/java/ant/build/jar/SimpleSample.jar
run:
[java] Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java248 in java.library.path
[java] at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
[java] at java.lang.Runtime.loadLibrary0(Runtime.java:844)
[java] at java.lang.System.loadLibrary(System.java:1051)
[java] at SimpleSample.<clinit>(Unknown Source)
[java] Could not find the main class: SimpleSample. Program will exit.
[java] Java Result: 1
rebuild-run:
BUILD SUCCESSFUL
Total time: 1 second
According to the first tutorial (how to build OpenCV from source), the result should be a JAR file and a .so native lib in the bin/ directory of your OpenCV directory. In the "SETTING UP ECLIPSE FOR USING OPENCV (JAVA) IN UBUNTU" tutorial, you must create the User Library by following the steps in the tutorial. That process involves browsing your file system to select the OpenCV JAR that was produced by the first tutorial and then selecting the .so file as the native library. Eclipse will not automatically "find" your OpenCV library, you have to configure it to know about it, that's what the second tutorial is doing.
I think you were running the wrong command with ant. You can read the manual here: https://ant.apache.org/manual/running.html
Below is the format to run ant:
ant [options] [target [target2 [target3] ...]]
So maybe your command line should look like:
ant -DocvJarDir=/usr/local/src/opencv-2.4.8/opencv/build/bin rebuild-run
given that /usr/local/src/opencv-2.4.8/opencv/build/bin contains the opencv-248.jar
As shown on your build.xml file that the property ocvJarDir is pointing to the location of the classpath requires to build the project:
<property name="lib.dir" value="${ocvJarDir}"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
And you need to specify the target "rebuild-run" in the command line since your build.xml does not have a default target specified.
Try installing 2.4.6 version. I have similar problems when installing in VS. Older version worked. Maybe it's the same in Eclipse, too.

Unable to deploy JavaFX applicaiton with external libraries in a JAR

I'm developing a JavaFX application in Eclipse. The app has external libraries, like log4j and others and runs perfectly from Eclipse. I've tried deploying it from Eclipse as a Runnable jar through the Eclipse built-in feature, and it runs well on some computers, while on others it would give me an obscure Undefined Link error. After digging around it seems JavaFX needs to be specially packaged either through an ANT script or through the javafxpackager application.
For the life of me I can't seem to deploy a runnable jar through either of those methods. Trying either of those methods, I get an Exception. Things I've tried:
Build the JavaFX application through an ANT script, below is a snippet of the relevant parts of the build script
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${javafx.sdk.path}/jre/lib" includes="jfxrt.jar"/>
</path>
<target name="compile" depends="clean">
<echo>Compiling the source</echo>
<mkdir dir="${classes.dir}"/>
<!-- Copy over the misc files into the classes dir -->
<copy todir="${classes.dir}/bundles">
<fileset dir="${src.dir}/bundles"/>
</copy>
<copy todir="${classes.dir}/css">
<fileset dir="${src.dir}/css"/>
</copy>
<copy todir="${classes.dir}/img">
<fileset dir="${src.dir}/img"/>
</copy>
<copy todir="${classes.dir}/views">
<fileset dir="${src.dir}/views"/>
</copy>
<copy todir="${classes.dir}/bundles">
<fileset dir="${src.dir}/bundles"/>
</copy>
<copy file="${src.dir}/log4j2.xml" todir="${classes.dir}"/>
<javac target="1.7" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" debug="on">
</javac>
</target>
<target name="jar" depends="compile">
<echo>Creating the main jar file</echo>
<mkdir dir="${distro.dir}" />
<fx:jar destfile="${distro.dir}/main.jar" verbose="true">
<fx:platform javafx="2.1+" j2se="7.0"/>
<fx:application mainClass="${main.class}"/>
<!-- What to include into result jar file?
Everything in the build tree-->
<fileset dir="${classes.dir}"/>
<!-- Define what auxilary resources are needed
These files will go into the manifest file,
where the classpath is defined -->
<fx:resources>
<fx:fileset dir="${distro.dir}" includes="main.jar"/>
<fx:fileset dir="." includes="${lib.dir}/**" type="jar"/>
<fx:fileset dir="." includes="."/>
</fx:resources>
<!-- Make some updates to the Manifest file -->
<manifest>
<attribute name="Implementation-Vendor" value="${app.vendor}"/>
<attribute name="Implementation-Title" value="${app.name}"/>
<attribute name="Class-Path" value="${lib.dir}"/>
<attribute name="Implementation-Version" value="1.0"/>
</manifest>
</fx:jar>
</target>
Compiling/generating the jar through the ant script, I get a window pop up for half a second and "Exception in Application start method" in the console, with no stack trace. I've tried runnning java with the -XX:-OmitStackTraceInFastThrow flag, but it still won't give me a stack trace.
Running through javafxpackager gives the following error:
RenderJob.run: internal exception
java.lang.UnsatisfiedLinkError: com.sun.prism.d3d.D3DContext.nSetBlendEnabled(JZ
Z)I
at com.sun.prism.d3d.D3DContext.nSetBlendEnabled(Native Method)
at com.sun.prism.d3d.D3DContext.initState(D3DContext.java:84)
at com.sun.prism.d3d.D3DResourceFactory.(D3DResourceFactory.java:5
7)
at com.sun.prism.d3d.D3DPipeline.createResourceFactory(D3DPipeline.java:
147)
at com.sun.prism.d3d.D3DPipeline.getD3DResourceFactory(D3DPipeline.java:
153)
at com.sun.prism.d3d.D3DPipeline.findDefaultResourceFactory(D3DPipeline.
java:179)
at com.sun.prism.d3d.D3DPipeline.getDefaultResourceFactory(D3DPipeline.j
ava:201)
at com.sun.prism.GraphicsPipeline.getDefaultResourceFactory(GraphicsPipe
line.java:97)
at com.sun.javafx.tk.quantum.QuantumRenderer$3.run(QuantumRenderer.java:
143)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:47
1)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at com.sun.prism.render.RenderJob.run(RenderJob.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:615)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(Quantu
mRenderer.java:98)
at java.lang.Thread.run(Thread.java:724)
You can deploy your application using javafxpackager. If you deploy a self contained application, the Java Runtime Environment will be bundled with your application and it will run on any machine whether the right version of Java is installed or not.
(You should have javafxpackager in your jdk directory under the bin folder.)
Look for your compiled files (.class) in your eclipse workspace folder. They should be in the bin directory.
Create a createjar folder and inside it create a classes and a out folder. Copy all of your .class files in the classes folder. Also include in the classes folder all jar needed by your application.
Now on the command line go inside the createjar directory and run this command :
"C:\path\to\jdk\jdk1.7.0_25\bin\javafxpackager.exe" -createjar -appclass package.MainClass -srcdir classes -outdir out -outfile NameOfYourJar -classpath "" -v
This should create a runnable jar in the out directory. You will need it for the next step.
Now create a deploy folder alongside the createjar folder.
Inside the deploy folder make to other directories dist and packages.
In the dist folder copy your freshly created jar from above plus all the dependencies/ressources it will need.
Go back on the command line (if you ever left it) and go in the deploy directory. Run the following command from there :
"C:\path\to\jdk\jdk1.7.0_25\bin\javafxpackager.exe" -deploy -native -outdir packages -outfile NameOfYourApp -srcdir dist -srcfiles NameOfYourJar.jar -appclass package.MainClass -name "Name of You Application" -title "Title of your application"
Once it is done it should have created all you need to deploy your app including native .exe file to run on Windows platform. This is the self contained application ! To be able to run it you need to go into bundles\NameOfYourApp\app and paste here all dependencies/ressource your app needs.
At last, double click on YourApp.exe and it should run even on machines without Java installed.
With ant files you have two main options: <fx:jar> and <fx:deploy>. The former just produces a jar that allows you to refer to your dependencies if they are in the same file as the jar. You can unpack the jar and look at the MANIFEST file produced to see how they are added to the javafx classpath.
<fx:deploy> allows you to pull in dependencies, but requires that you build a platform-specific installer. It can't be done using a jar. Apparently NetBeans allows you to create a stand-alone jar but I have not tested with that.
you do not package javafxrt.jar with your app
e(fx)clipse would have produced the ant-script for your if you use it

Automate jar export across projects in eclipse

I have a Google Web Toolkit project for which i include a second project into the build path. This other project has a lot of my commonly used functions and classes. Building the project works fine but to avoid run time errors i have to jar the second project into the WAR folder of the GAE project.
The process of jar/exporting the second project into the first is a manual process that i do with a right click in eclipse.
Is there a way to automate the process of jar/exporting to WAR of the second project into a location on the first?
Would i do this with an ANT script?
Or is there a way this can be done within Eclipse itself.
Thanks for you help
You are in for some reading mate :)
Step 1 : Create a JAR of the first project ( Read this). In the jar task you could put as the path of your GAE's folder lib as the destfile like so :
<target name="jar">
<jar basedir="bin" destfile="../GAE_PROJECT/war/WEB-INF/lib/${project-name}.jar">
<manifest>
<attribute name="Built-By" value="${builder}" />
<attribute name="Built-On" value="${build-info.current-date}" />
<attribute name="Built-At" value="${build-info.current-time}" />
</manifest>
</jar>
</target>
Step 2 : Create the WAR for your GAE project ( Read the official documentation here ). The documentation shows how the jars in the lib are included :
<fileset dir="war/WEB-INF/lib">
<include name="**/*.jar" />
</fileset>

How can I automate compiling a large Java project?

I'm working on an automation project for my employer. We have a pool for each revision of our source code. When you download a revision, you need to create a directory structure with a bunch of third party includes to eventually build the project. I've automated this entire process up to the point of having my script (.bat) compile each particular runnable java application. There are many applications to this single project, and the directory listing looks something like this:
Proj Name
-variousincludesfolder1
-variousincludesfolder2
-variousincludesfolder3
-variousincludesfolder4
-runnableapplicationsandmoreincludes
-con.java
Right now, I'd like to do an automated compiling of con.java, but I don't know where to begin. People have suggested I try Ant, but any automated Ant file generation I get using Eclipse seems only enough to build con.java while an active project file exists. Is there anyway to automate this without using eclipse, to the point of having the batch file generate a .jar itself?
This is definitely a job for Ant. Don't rely on Eclipse-generated Ant files; read through the manual and write one yourself. (You'll likely find out that Ant does things you didn't think of doing in your build script, too.)
To be more specific, here is the documentation for the jar task.
You can define wildcard and pattern matches to include/exclude all sorts of files and folders in your build. Take a look at the Ant manual to see how things like filesets work with include and exclude filters.
Also, read the tutorial.
Here is a simple build file that looks to compile all java files and reference all jars. Place it in the top level directory:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"
href="http://www.ibm.com/developerworks/xml/library/x-antxsl/examples/example2/ant2html.xsl"?>
<project name="Proj Name" default="build" basedir=".">
<property name="src.dir" value="${basedir}" description="base folder where the source files will be found. Typically under /src, but could be anywhere. Defaulting to root directory of the project" />
<property name="build.dir" value="build" description="Where to put build files, separate from src and resource files." />
<path id="master-classpath">
<fileset dir="${basedir}" description="looks for any jar file under the root directory">
<include name="**/*.jar" />
</fileset>
</path>
<target name="build" description="Compile all JAVA files in the project">
<javac srcdir="${src.dir}"
destdir="${build.dir}/classes"
debug="true"
deprecation="true"
verbose="false"
optimize="false"
failonerror="true">
<!--master-classpath is defined above to include any jar files in the project subdirectories(can be customized to include/exclude)-->
<classpath refid="master-classpath"/>
<!--If you want to define a pattern of files/folders to exclude from compilation...-->
<exclude name="**/realm/**"/>
</javac>
</target>
</project>