Execute Jar File of a JavaFX project on Linux, or How to Get Rid of ClassNotFoundException ? English
>> 02 April 2009
Deploying a JavaFX stand alone application as a jar file, in NetBeans is really straightforward. All steps are explained on javafx.com web site.
http://javafx.com/docs/tutorials/deployment/
The question here, is it possible to deploy a "normal" jar file and use it as we usually do for jars (ie: java -jar fileName.jar) ?
If you have such kind of questions, this tutorial is for you.
Note :
This tutorial is only a demo. Please read carefully JavaFX license before packaging or distributing any files.
Create a JavaFX project in NetBeans
We need a very simple JavaFX project (HelloWorld)
File -> New Project
Select JavaFX -> JavaFX Script Application -> Next
Enter a name for your project in "Project Name", leave all other options -> Click on Finish
The project is created and Main.fx is opened
Note :
The package name is "helloworld" and when you build the project, the produced class will be "Main.class"
Verify Execution Model
Right click on your project -> Properties -> Run
Select "Standard Execution" in "Application Execution Model" (Default)
Verify Main Class
Click on "OK"
Build the Project
Right click on the project -> Build Project
The Project is Built
Two folder are added inside "NetBeansProject" (default projects folder) in "HelloWorld" folder, these are "dist" and "build"
"build" has compiled classes of the project
"dist" has a jar file containing the compiled classes
Execute the Jar file from the Command Line
Change directory and go to "dist" inside "HelloWorld" project
(The indicated PATH is on my own computer, change as needed)
$cd /home/kas/NetBeansProjects/HelloWorld/dist
Locate JavaFX-SDK
If you installed NetBeans plug-in as mentioned in my previous post in this Blog
http://java-javafx-iipt.blogspot.com/2009/03/javafx-111-netbeans-651-on-linux.html
You will find a "javafx-sdk" folder in this location
$HOME/.netbeans/6.5/javafx-sdk
Running the Jar
Using export or an absolute path to run the produced jar file (HelloWorld.jar).
cp stands for the CLASSPATH
helloworld.Main is the main class with the package name
$ export PATH=$HOME/.netbeans/6.5/javafx-sdk/bin:$PATH
$ javafx -cp HelloWorld.jar helloworld.Main
Trying java -jar HelloWorld.jar
Running the jar file as mentioned before is fine. But it's somehow the source of many problems especially if you give the absolute path to your javafx-sdk !!!. And it's also unusual for Java Developers.
If you try to run the Jar using "java -jar HelloWorld.jar", you will get some error messages. The main messages is : ClassNotFoundException. The program exists with "Could not find the main class: helloworld.Main. Program will exit" message.
This is a well known Exception, when developing Java programs. Isn't it ?
This normally means in Java that the class that contains main method was not found, or the "Main-Class" section in MANIFEST.MF, in the jar file is missing or has errors.
The problem here is different, let take a look at "MANIFEST.MF" and jar's contents.
As you can see, "Main-Class" is in "META-INF/MANIFEST.MF", and has the correct class name with it's package. A new line is inserted to satisfy specification at the end of the file.
The Main.class also is in the jar in the correct location
What is the problem here ?
The answer is simple : Java can not locate JavaFX run-time in the CLASSPATH. It needs indication about this location.
Is there a simple workaround ?
Yes. Normally if you specify the path to JavaFX run-time in the classpath, you'll get no more errors. A better way is to let NetBeans do the work. Please follow...
Copy javafx-sdk folder to a place of your choice.
$ cp -a $HOME/.netbeans/6.5/javafx-sdk $HOME/javafx-sdk
Note :
You can use the copied folder if you want to play with JavaFX using the command line, compiler and interpreter. Or if you want to use "javafx -cp fileName.jar packageName.MainClass" as above.
Right click on the project name in NetBeans -> Select Properties
Select "Libraries" -> Click on "Add JAR/Folder"
Inside the javafx-sdk/lib folder (copied above)
There are two folder "desktop" and "share"
Select all jar files inside "desktop" -> OK
Repeat Add JAR/Folder, and select all jars in "share" -> OK
Uncheck "Build Projects on Classpath"
Click on OK when all jars are added
Right click on your project -> Clean and Build Project
That's All :)
You will find in the "dist" folder HelloWorld.jar file and "lib" folder packaged for you by NetBeans. This contains the needed JavaFX run-time.
MANIFEST.MF was also updated with the new CLASSPATH
Let's try it
$ java -jar HelloWorld.jar
Et voilà. Application is running, no more errors.
0 comments:
Post a Comment