I'm trying to learn JavaFX and maybe create a few "learner" games. I always do my development in Eclipse, rather than NetBeans which the JavaFX team is clearly trying to push.
Can anybody point me in the direction of a how-to for building a JavaFX project in Eclipse, or at least building a JavaFX project without NetBeans? Everything I've found so far either uses NetBeans, or they're running a one-file project in Eclipse (witch won't work for larger projects). Idealy, I'm looking for somebody who's set up a simple Ant script that builds a JavaFX project, since I assume that's the end-game for this situation.
I was able to find where to download the Eclipse JavaFX plugin. It provides syntax highlighting support, plus some "snippets". I can even use it to run simple hello world type JavaFX apps, but I cant seem to get it to automatically build multi-file JavaFX projects. Even if I could, I still can't seem to write a correct ant script to jar up the JavaFX project.
Also, I found this site that talks about what you can do to use a javafxc Ant task created by Sun(?), but I'm not having any luck trying to use what they talk about.
Thanks
Ross
If you create new project in NB there is folder called nbproject. This folder contains build-impl.xml. This file contains this target:
<target if="src.dir" name="-compile-fx">
<taskdef classname="com.sun.tools.javafx.ant.JavaFxAntTask" classpath="${platform.bootcp}" name="javafxc"/>
<javafxc bootclasspath="${platform.bootcp}" classpath="${build.classes.dir}:${javac.classpath}" compilerclasspath="${platform.bootcp}" debug="${javac.debug}" deprecation="${javac.deprecation}" destdir="${build.classes.dir}" excludes="${excludes}" fork="yes" includeJavaRuntime="false" includeantruntime="false" includes="**/*.fx" source="${javac.source}" sourcepath="" srcdir="${src.dir}" target="${javac.target}">
<compilerarg line="${javac.compilerargs}"/>
</javafxc>
</target>
This is good start to create ant for Eclipse. I'm not sure how building works for Eclipse, but there could be limitations. The com.sun.tools.javafx.ant.JavaFxAntTask is located in SDK, not in compiler jar. Good luck!.
I have read in article about JavaFX that there is an Eclipse extension available for it. According to that article it is not as mature as NetBeans support for FX but should be better than nothing...
I've created a partial Ant script for the build process. I can tell that it's actually compiling the JavaFX classes and Jaring them up. However, I think I'm missing an Ant task where I create the JNLP object that is needed in the Applet that I'm also missing (but managed to fake).
Since I didn't explicitly state it, I did not get this to a working state, so don't expect to get there just by doing what I did ;)
This Ant script is as far as I've gotten. I've left out everything but the important parts for brevity...
<project name="RABfx" default="all" basedir=".">
...
<property environment="env"/>
<property name="java.home" value="${env.JAVA_HOME}" />
<property name="jfx.home" value="${env.JAVAFX_HOME}" />
<path id="compile.classpath">
<fileset dir="${java.home}/lib">
<include name="**/*.jar" />
</fileset>
<fileset dir="${jfx.home}/lib">
<include name="**/*.jar" />
</fileset>
...
</path>
<taskdef classname="com.sun.tools.javafx.ant.JavaFxAntTask" name="javafxc">
<classpath refid="compile.classpath" />
</taskdef>
...
<target name="compile">
<javac srcdir="${src}" destdir="${src.classes}" includes="**/*.java">
<classpath refid="compile.classpath" />
</javac>
<javafxc srcdir="${src}" destdir="${src.classes}" includes="**/*.fx" executable="${jfx.home}/bin/javafxc.exe">
<classpath refid="compile.classpath" />
</javafxc>
...
</target>
<target name="build">
<jar jarfile="${src.jar}">
<fileset dir="${src.classes}" />
</jar>
</target>
...
</project>
Also, my "faked" Applet...
...
<script src="http://dl.javafx.com/dtfx.js"></script>
<script>
javafx(
{
archive: "RABfx.jar",
width: 440,
height: 560,
code: "TicTacToe.Main",
name: "TicTacToe"
}
);
</script>
...
After I asked this question, an official (I think?) JavaFX plugin for Eclipse was released. Go to the JavaFX for Eclipse page. I installed the plugin and everything automagically worked!
Either use the JavaFxAntTask mentioned before or generate the project in NetBeans and open it in Eclipse and then you can just run Ant targets. Or wait for 1.1 which will hopefully come with full Eclipse and IDEA support.
Related
I'm trying to bundle my .jar to a MacOSX app bundle, using app bundler.
I'm following this tutorial.
It says to add a lib folder to the high-level project directory, but I don't know what that means. I've been looking everywhere for it, and I cannot find out what it is. That's my only problem I have, anyone know?
EDIT:
Here is my build.xml file:
<project name="Rage Mage" basedir=".">
<taskdef name="ragemage"
classname="com.oracle.appbundler.AppBundlerTask"
classpath="lib/appbundler-1.0.jar" />
<target name="bundle-RageMage">
<delete dir="appBundle" failonerror="false"/>
<mkdir dir="appBundle"/>
<bundleapp outputdirectory="bundle"
name="Rage Mage"
displayname="Rage Mage"
icon="res/icon.icns"
identifier="ragemage.src.Window"
mainclassname="ragemage.src.Window">
<classpath file="dist/ragemage_1.1.1.jar" />
</bundleapp>
</target>
Thanks!
Okay, so, after having a little play around, this is what I understand...
Download Java Application Bundler and place it in the lib directory of your project. You will need to create this directory...
Create a new Ant script into your project directory, call it what ever you like...Also, take the time to read through the AppBundler Task Docs
The ant script should be based on the following skeleton...
<project name="ButtonDemo" default="bundle-buttonDemo" basedir=".">
<taskdef name="bundleapp"
classname="com.oracle.appbundler.AppBundlerTask"
classpath="lib/appbundler-1.0.jar" />
<!-- See the lib reference here, this is why you need to use the lib directory! -->
<target name="bundle-buttonDemo">
<delete dir="appBundle" failonerror="false"/>
<mkdir dir="appBundle"/>
<bundleapp outputdirectory="appBundle"
name="ButtonDemo"
displayname="Button Demo"
identifier="components.ButtonDemo"
mainclassname="components.ButtonDemo">
<!-- The following is important and should point to your build -->
<classpath file="dist/ButtonDemo.jar" />
<!-- You can have multiple instance of classpath if you 3rd party or
dependent jars in different locations -->
</bundleapp>
</target>
</project>
Build your project
Run the ant script, using (something like) ant -f {You App Bundler script}
The app bundle, in this case ButtonDemo.app will be created in appBundle directory. If you can, browse the contents of the ButtonDemo.app/Contents/Java and make sure all your required Jar files are there...
Happy bundling!
Updated based on updated build.xml file
1- There is no default target specified by the project tag. Think of this like your "main class" or "main" method, without, ant has no idea what you want to run...
<project name="Rage Mage" basedir="." default="bundle-RageMage">
2- The name of the taskdef is significant and you use it in the any script to identify what ant should do when it hits your tag reference...
So based on your example, you either need to change the name of the taskdef from ragemage to bundleapp or change the bundleapp tag to ragemage...
Either change this...
<taskdef name="bundleapp"
classname="com.oracle.appbundler.AppBundlerTask"
classpath="lib/appbundler-1.0.jar" />
or this (in target bundle-RageMage)
<ragemage outputdirectory="bundle"
name="Rage Mage"
displayname="Rage Mage"
icon="res/icon.icns"
identifier="ragemage.src.Window"
mainclassname="ragemage.src.Window">
<classpath file="dist/ragemage_1.1.1.jar" />
</ragemage>
Personally, I'd leave it as bundleapp, but that's me...
3- The delete, mkdir and outputdirectory attribute of bundleapp are related...
<delete dir="appBundle" failonerror="false"/>
<mkdir dir="appBundle"/>
<bundleapp outputdirectory="bundle"...
Either, make them all appBundle or bundle, what every you want...
4- You main class is unlikely to be ragemage.src.Window and is probably going to be Window
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
I need to use some java library in MATLAB. I did use it in MATLAB (Unix version), but now I have to do it on MATLAB (Win64), as well. As far as I know, this is a project developed in unix. I did simply compile it using
ant
command in Ubuntu.
Since I'm a beginner to java compiling, I thought of installing ant on Windows and running
c:\java\ant\bin\ant
command in the path. However when I do this, it says that:
build.xml:22: Javadoc failed: java.io.IOException:
Cannot run program "javadoc.exe": CreateProcess error=2
Here is the build.xml file:
<project default="all">
<target name="all" depends="doc,jar" />
<target name="compile">
<mkdir dir="build/classes" />
<mkdir dir="build/examples" />
<javac includeantruntime="false" debug="on" srcdir="src/main/java"
destdir="build/classes" target="1.5" />
<javac includeantruntime="false" srcdir="src/main/example/"
classpath="build/classes" destdir="build/examples" />
</target>
<target name="jar" depends="compile">
<jar destfile="dist/java_websocket.jar">
<fileset dir="build/classes" includes="**/*.class" />
</jar>
</target>
<target name="doc">
<delete dir="doc" />
<javadoc sourcepath="src/main/java" destdir="doc" />
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>
When I try to search about this issue, I've come up with so many answers that are about Java Eclipse compilations, which I'm a total stranger to.
If you use maven to compile these projects, thats fine. I just need some guidance.
Here is the github link of the java project that I'm trying to compile (on Win64), if you would like to see any further information: https://github.com/TooTallNate/Java-WebSocket
And here is the build instructions (I'm guessing for Unix): https://github.com/TooTallNate/Java-WebSocket/blob/master/README.markdown
Note that I sadly use Windows 8 (x64) and I currently have jdk1.7.0_21 installed on the path "C:\Program Files\Java\jdk1.7.0_21" which is also the system variable JAVA_HOME. ANT_HOME variable is "c:\java\ant"
Please take a moment from your precious time and help me to find a solution, if you have any knowledge about this issue. Any help will be greatly appreciated.
Best regards.
If it compiles on ubuntu with just ant, there is no need for either maven or eclipse. The problem is likely that the PATH variable does not include the jdk. Look into c:\Programs\Java and find the javadoc.exe. Its directory must go into PATH. To set the dir in PATH, go to (roughly, have no windows around) computer / properties / advanced / environment variables / system and find the PATH variable. Append the directory you found by using semicolon as the separator.
Greeting, I'm trying to put some Beanshell script in my Ant build.xml file. I've followed the Ant manual as well as I can but I keep getting "Unable to create javax script engine for beanshell" when I run Ant. Here is the test target I wrote mostly from examples in the Ant manual:
<target name="test-target">
<script language="beanshell" setbeans="true">
<classpath>
<fileset dir="c:\TEMP" includes="*.jar" />
</classpath>
System.out.println("Hello world");
</script>
</target>
My beanshell "bsh-2.0b4.jar" file is on the script task's classpath the way the manual recommended. Hope I have the right file. I'm working in c:\TEMP right now.
I've been googling and trying for a while now. Any ideas would be greatly appreciated. Thanks.
First, you need jsr-engines.zip from here:
https://scripting.dev.java.net/servlets/ProjectDocumentList
Inside, you'll find jsr223/beanshell/build/bsh-engine.jar. Some searching implied that you need to download bsh-2.05b.jar. I found it here:
http://beanshell.org/bsh-2.0b5.jar
The more easily findable bsh-2.0b4.jar also seemed to work, but it printed a message that implied it was experimental.
Currently (2012) you need only 1 jar to fire the script task for BeanShell:
bsh-2.0b5.jar
Previously I also thought of the following, as mentioned by Ant Manual, Library Dependencies chapter:
bsf-2.4.0.jar
commons-logging-api-1.1.jar
But it looks like bsf is not needed for bsh, at least in my environment.
Once the jar is given to ant, the script task runs smoothly. There are 2 possible scenarios for getting the jars and making them available to ant.
Manual download way
Download the jars above. I provided the links from maven repository. Once you have all the jars downloaded, make them available to ant. There are
at least 3 ways to do it:
Put it in java library path
Put it in ant library directory
Give the correct classpath to script task.
I find the last method the best, because it is most easily ported between
different systems. The ant file for the script task could look as follows:
<project default="t1" >
<property name="bsh.path"
location="/mnt/q/jarek/lang/java/ant/stackoverflow/bsh-2.0b5.jar" />
<target name="t1">
<script language="beanshell" classpath="${bsh.path}">
javax.swing.JOptionPane.showMessageDialog(null, "Hello, Script!");
</script>
</target>
</project>
Automatic download method, employing Ivy
The manual method is not perfect when you want to distribute your build script. Then you would like a way to make sure the jars are present in the destination system. For distributing builds there's no better tool than ivy. Ivy will download the jars and put them in classpath for you. The problem is that there appears another dependency, which is ivy itself. But providing ivy.jar is quite easy and that is the last dependency we need to supply explicitly.
One may ask why to provide ivy.jar, while we could simply download bsh.jar in the same way. The answer is flexibility. When you have the ivy.jar, you get any jar you wish with a single step being adding it to the ivy.xml file. And there is an agreed universal location for the ivy.jar file, while for other file we would have to think of a suitable directory.
Below comes the full example that downloads ivy and then all the necessary dependencies. Ivy download script is based on Installation chapter of Ivy reference.
Then a simple ivy.xml file is needed, which is given after the sample build.xml.
Original auto-download ivy script has a disadvantage of always checking the ivy url, even if ivy.jar is already in the expected location. This may be overriden by specifying -Doffline=true. I prefer to add another target to the build file and to do the http check only if we don't already have the ivy.jar. This is the way the script here works. To observe what ivy actually downloaded, set IVY_HOME environment variable to a directory of your choice. It will be created and filled with ivy stuff.
build.xml:
<project default="t1"
xmlns:ivy="antlib:org.apache.ivy.ant" >
<property name="ivy.install.version" value="2.2.0" />
<property environment="env" />
<condition property="ivy.home" value="${env.IVY_HOME}">
<isset property="env.IVY_HOME" />
</condition>
<property name="ivy.home" value="${user.home}/.ant" />
<property name="ivy.jar.dir" value="${ivy.home}/lib" />
<property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />
<target name="check-ivy">
<condition property="ivy.present">
<available file="${ivy.jar.file}" type="file" />
</condition>
</target>
<target name="download-ivy" unless="ivy.present">
<mkdir dir="${ivy.jar.dir}"/>
<!-- download Ivy from web site so that it can be used even without any special installation -->
<get src="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}" usetimestamp="true"/>
</target>
<target name="init-ivy" depends="check-ivy, download-ivy">
<!-- try to load ivy here from ivy home, in case the user has not already dropped
it into ant's lib dir (note that the latter copy will always take precedence).
We will not fail as long as local lib dir exists (it may be empty) and
ivy is in at least one of ant's lib dir or the local lib dir. -->
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
</target>
<target name="ivy-libs" depends="init-ivy" >
<ivy:cachepath pathid="path.from.ivy" log="download-only" />
</target>
<target name="t1" depends="ivy-libs" >
<script language="beanshell" classpathref="path.from.ivy">
javax.swing.JOptionPane.showMessageDialog(null, "Hello, Script!");
</script>
</target>
</project>
ivy.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"http://ant.apache.org/ivy/schemas/ivy.xsd">
<info organisation="example.com" module="testing-script-task" />
<dependencies>
<dependency org="org.beanshell" name="bsh" rev="2.0b5" />
<!-- <dependency org="bsf" name="bsf" rev="2.4.0" /> -->
</dependencies>
</ivy-module>
The Ant plug-in "org.apache.ant_1.7.0.v200803061910" have all the jar files needed
Don't use beanshell language. Use javascript instead, as it runs on jdk6 without any additional jars. Rebse told me that.
Javascript is also allowed to use java classes, for example java.lang.System.out.println()
what is the best practice to create a .jar file from a java project??
Some points to consider:
Create proper MANIFEST.MF, which should contain:
Build date;
Author;
Implementation-version;
Use a script/program - like an Ant task - which takes care of properly creating a manifest file.
Here's a simple ant file which builds a java project, adapted from the Ant tutorial:
<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="Implementation-Version" value="1.0"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>
</project>
Read up on the basics of Sun's JAR format at this tutorial. In my opinion you should learn the basics -- namely MANIFESTs, the SDK tools -- before jumping into third party tools such as the following.
The Jar command line tool distributed with the Sun JDK:
jar -cf myjar.jar base_src_folder
Where base_src_folder is the parent directory holding your source code.
Alternatively you could use a build tool such as Apache Ant. It has features such as the JAR task to do this for you.
Apache Maven, for projects which are dependent on other projects.
Admittedly, Maven may be overkill for learning projects, but it is invaluable for larger, distributed ones.
Eclipse IDE is the best way for new comers. Just select the project right click export File, select jar anf filename.
Apache Ant
Ant is not quite easy to begin with. Instead, I would suggest using an IDE, for instance NetBeans, which creates an Ant script under the hood. All you have to do is to hit "build", and you get a .jar file as result.