java.net member

Rechercher dans ce site

Showing posts with label How to. Show all posts
Showing posts with label How to. Show all posts

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.

Read more...

Exécuter le fichier Jar d'un projet JavaFX, comment finir avec "ClassNotFoundException" ?

Déployer une application JavaFX "stand alone", sous forme de Jar ne pose pas de problèmes particuliers

Le tutoriel sur le site de javafx.com explique tout et en détail.

http://javafx.com/docs/tutorials/deployment/

Mais comment faire, si on veut exécuter le jar normalement sous la forme de
java -jar nomDeFichier.jar et sans donner tous les fichiers nécessaires sur la ligne de commande ?



Note :
Ce petit tutoriel est une démo en local. Lisez attentivement la licence de JavaFX, concernant la distribution de fichiers.


Nous commençons par créer un petit projet JavaFX dans NetBeans


File -> New Project



Sélectionnez JavaFX -> JavaFX Script Application -> Next



Donnez un nom à votre projet dans "Project Name", le nom du projet d'exemple est HelloWorld.

Laissez les autres options comme tel -> Finish



Le projet est créé et Main.fx est ouvert dans l'éditeur


Note :
Le nom du package dans l'exemple est "helloworld", la classe produite va porter le nom :

"Main.class"



Verifier les configurations d'exécution

Un clic droit sur le projet -> Properties -> Run

Assurez-vous que le mode d'exécution est "Standard Execution" (Par défaut)





Construire (Build) le projet


Un clic droit sur le projet -> Build Project




Deux dossiers sont ajoutés dans le projet : (Dans l'exemple NetBeansProject/HelloWorld) "dist" et "build"

"build" contient les classes compilées
"dist" contient un fichier jar contenant les classes compilées.




Exécutez HelloWorld.jar

Changez de dossier pour aller dans le dossier "dist" dans le projet (dans l'exemple /home/kas/NetBeansProjects/HelloWorld/dist), à modifier selon votre projet.

$cd /home/kas/NetBeansProjects/HelloWorld/dist




Localisez le dossier de JavaFX SDK

Si vous avez suivi mon article concernant l'installation de NetBeans-6.5.1 et son plug-in de JavaFX

(http://java-javafx-iipt.blogspot.com/2009/03/javafx-111-et-netbeans-651-sur-linux.html), vous trouverez le dossier "javafx-sdk" dans le chemin suivant

$HOME/.netbeans/6.5/javafx-sdk



A partir de la ligne de commande (dans un terminal) en utilisant export ou en indiquant le chemin complet (absolu) vers le dossier "bin" de "javafx-sdk",

tapez :


$ export PATH=$HOME/.netbeans/6.5/javafx-sdk/bin:$PATH

$ javafx -cp HelloWorld.jar helloworld.Main




L'application dans le jar est exécutée sans aucun problème, mais cette méthodes surtout avec un chemin absolu et beaucoup d'arguments sur la ligne de commande est une source d'erreurs et un peu étrange pour les habitués de java -jar nomFichier.jar !!!

Et si on essaye de lancer le jar avec java ?

$java -jar HelloWorld.jar

Cette commande engendre des message d'erreurs, notamment "ClassNotFoundException". Le programme quitte avec le message "Could not find the main class: helloworld.Main. Program will exit".

Cette Exception est parmi les plus connues pour les développeurs Java, elle signifie normalement que la classe contenant la méthode "main" n'a pas été trouvée, soit suite à l'absence de telle classe ou si la section Main-Class dans le fichier MANIFEST.MF est absente ou mal renseignée.



Le problème ici est différent. Il suffit de jeter un œil sur le fichier
"MANIFEST.MF" et sur le contenu du jar pour constater que l'absence de la classe Main.class n'est pas à l'origine de problème.


Le fichier "MANIFEST.MF" est bien renseigné. Une nouvelle ligne est laissée à la fin du fichier.



La class « Main.class " existe et en bonne place dans le fichier .jar


La raison de messages d'erreur est tout simplement l'absence dans la CLASSPATH du chemin de bibliothèques composant le JavaFX run-time.

L'ajout du chemin vers des fichiers jar du dossier "lib" dans javafx-sdk dans la CLASSPATH, règle le problème. Il est même possible de demander à NetBeans de faire le nécessaire pour vous :)



Copiez le dossier javafx-sdk dans un dossier de votre choix


$ cp -a $HOME/.netbeans/6.5/javafx-sdk $HOME/javafx-sdk

Note :
Cette copie vous permet aussi d'utiliser JavaFX à partir de la ligne de commande.



Un clic droit sur le projet d'exemple -> Properties



Sélectionnez "Libraries" -> un clic sur "Add JAR/Folder"

A l'intérieur de javafx-sdk/lib on trouve deux dossiers

"desktop" et "share"

Sélectionnez tous les fichiers jar dans "desktop" -> OK

Répétez l'opération -> Add JAR/Folder, sélectionnez les fichier jar dans "share" -> OK

Dé-sélectionnez la case "Build Projects on Classpath"

Un clic sur "OK" pour terminer





Un clic droit sur le projet -> Clean and Build Project



C'est tout :)

Maintenant le dossier "dist" contient le fichier jar (HelloWorld.jar). Le dossier "lib" contient tous les fichier nécessaires à l'exécution du HelloWorld.jar, préparés pour vous par NetBeans. NetBeans a aussi mis à jour le fichier MANIFEST.MF pour refléter la nouvelle CLASSPATH




Essayons :

$ java -jar HelloWorld.jar

Et voilà. L'application est lancée sans aucun messages d'erreur

Read more...

JavaFX-1.1.1, NetBeans-6.5.1 on Linux - English

>> 27 March 2009

JavaFX 1.1.1 and NetBeans-6.5.1 are availables.

Linux users must wait !!!

In this tutorial I have installed JavaFX plug-in for NetBeans-6.5.1 on Linux


Download all “.nbm” files on NetBeans site :



http://updates.netbeans.org/netbeans/updates/6.5.1/uc/final/stable/patch3/javafx2/



Quit NetBeans


All the work to be done on the following file :

org-netbeans-modules-javafx-sdk-mac.nbm




Create a work directory (for instance TMP), copy org-netbeans-modules-javafx-sdk-mac.nbm into TMP

$ cp org-netbeans-modules-javafx-sdk-mac.nbm TMP



Unzip "org-netbeans-modules-javafx-sdk-mac.nbm"

$ unzip org-netbeans-modules-javafx-sdk-mac.nbm




Edit "info/info.xml"

delete "org.openide.modules.os.MacOSX"





Delete all files in "META-INF"



Modify "org-netbeans-modules-javafx-sdk-mac.jar ".

Path to this file :

netbeans/modules/org-netbeans-modules-javafx-sdk-mac.jar (in TMP)



Unzip the file using “jar”


$ jar xvf org-netbeans-modules-javafx-sdk-mac.jar



After unjar we get two directories :

"org" and "META-INF".




Delete in "META-INF/MANIFEST.MF" :

"org.openide.modules.os.MacOSX,"


Note :
This line is in the required modules

OpenIDE-Module-Requires: org.openide.modules.os.MacOSX, org.openide.mo
dules.ModuleFormat1



Re-archive jar content

Tape on one line :

$ rm *jar; jar cvfm org-netbeans-modules-javafx-sdk-mac.jar META-INF/MANIFEST.MF org



Make all files executable in


netbeans/javafx-sdk/bin (TMP)



$ chmod u+x netbeans/javafx-sdk/bin/*




Delete the downloaded "org-netbeans-modules-javafx-sdk-mac.nbm" in TMP directory and Zip all content of TMP


$ rm org-netbeans-modules-javafx-sdk-mac.nbm
$ zip -r org-netbeans-modules-javafx-sdk-mac.nbm *



Use the modified "org-netbeans-modules-javafx-sdk-mac.nbm with the other “.nbm”



Start NetBeans

Tools -> Plugins



If you enter JavaFX in Search nothing is displayed (not installed yet)



Select "Downloaded" tab

click on "Add Plugins"




Browse to the directory of the downloaded “.nbm” files and the modified "org-netbeans-modules-javafx-sdk-mac.nbm"


Select all “.nbm” files -> OK




click on "Install"






Read the licence

Click on "Install"



A Warning message !!!

This message is displayed because there are no more signatures in TMP/META-INF (files deleted above)

click on "Continue"



Restart NetBeans

click on "Finish"



Now if you select "Tools" -> “Plugins” -> “Installed” and enter JavaFX in “Search”

JavaFX is displayed




Create a JavaFX project in NetBeans

"File" -> New Project



Select "JavaFX" -> JavaFX Script Application



Give your project a name

Click on "Finish"



JavaFX project is created and Main.fx is opened in the editor



Run

Select “Main.fx” -> right click -> Run File



Et voilà



Enjoy your JavaFX :)

Read more...

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP