Monday, August 26, 2019

Using Git, JavaFX and downloading projects from GitHub into Eclipse

Git is a distributed version control system that currently is widely used.  It is distributed in that it can work in a peer-to-peer fashion, rather than needing to synchronize with a central server.
https://git-scm.com/
https://en.wikipedia.org/wiki/Git
Note that Git is a system (or protocol, depending upon how you define it), rather than a specific piece of software.  Meaning, that while a client can be downloaded from the above location, there are other clients available, including the one built into Eclipse.

GitHub (https://github.com/) is a site where people can host their projects.  Projects hosted here can be public or private.

Obviously Git has a number of commands that support its workflow, but the simplest one to understand and the one that is most useful immediately is “clone”.  This does what the name implies, make a copy of a repository. 

Basic Java project

Open up the Git Repository view within Eclipse.  This is done via Window->Show View->Other->Git->Git Repositories .  This should open a pane in the lower left corner of the Eclipse window, under Package Explorer.  Since no repositories have yet been added, there should be three links in this view: "Add an existing local Git repository", "Clone a Git repository", "Create a new local Git repository".  There are also icons across the top of this view, three of which perform the same functions as the links.  (If a repository has been added to this view, the links will be gone, replaced by the repository list and just the icons will remain.) 

Click on "Clone a Git repository".  This should pop up a dialog box.  In the URI field, enter "https://github.com/cajanssen/basictext.git" (without the quotes).  This will autofill the Host and Repository path fields.  Click Next.  The Branch Selection dialog comes up.  Master should already be selected, if not select it.  Click Next.  The Local Destination dialog comes up.  The default local directory for the repository is likely C:\Users\<windowsusername>\git\basictext , which should be fine.  In the Projects section of this dialog box, check “Import all existing Eclipse projects after the clone finishes”.  This will create the project in Package Explorer as well.  Click Finish.

This should have added a repository to the Git Repositories view named basictext.  It should also have added a project in the Package Explorer.  Click on the new project in Package Explorer.  In the menus, click on the Run->Run As->Java Application .  (This functionality can also be accessed by Right Clicking on the project name in the Package Explorer.)  This should run the project which simply outputs two lines of text to the Console window.

JavaFX project

Since Oracle moved JavaFX into a library separate from the base JRE / JDK in version 9 and beyond, there are some additional steps to do to build and run JavaFX projects in Eclipse.  This includes projects that are downloaded from a Git repository.  Unfortunately, part of this will need to be done individually for each project.

Configure the Eclipse build environment to include JavaFX.


Assumption here is that the JavaFX SDK has already been downloaded, unpacked and placed in an appropriate directory on the local machine.  We will be roughly following the instructions that are here: https://openjfx.io/openjfx-docs/ after clicking the "JavaFX and Eclipse" link on the left side.  Following the instructions for "Non-modular projects": 
1)Create a new User Library via Window->Preferences->Java->Build Path->User Libraries.  (You may need to click on the leading “>” on Build Path to get the Build Path subtree to display.)  In the User Libraries area, click New...  Give the library a name, such as JavaFX11.  Click Ok.  With that newly created library selected, click Add External JARs...  Navigate to the JavaFX SDK lib directory and add the eight JAR files that are there.  Click Apply and Close.  This User Library is now available to be added to project build paths.

The next steps will need to be done for each project.  Therefore, let's pull down another project from Github to work with.  This project will include JavaFX functionality.  In the Git Repositories view in Eclipse, click the "Clone a Git Repository" icon.  This time, enter "https://github.com/cajanssen/basicFXOne.git" (without the quotes).  It should work similar to the clone procedure above. 

2)Add the newly created user library to the project build path.  This would need to be done for JavaFX projects whether they were pulled down via Git or if they were created locally.  Select the new project in the Package Explorer and go to Project->Properties->Java Build Path, Libraries tab.  Select Classpath in the list and this will cause the buttons on the right to become active.  Select Add Library...  Select User Library, click Next, select the created library (whatever you named it, probably JavaFX11).  Click Finish.  Click Apply and Close in the previous dialog box.

3)Run this downloaded project.  As above, this can be done by selecting the BasicJavaFXOne project in the Package Explorer and then selecting the command Run->Run As->Java Application.  It will likely need you to select the correct application from between a choice of two in the next dialog box.  Select the BasicJavaFXOne application.  Running the project will result in an error shown in the Console window. 

Error: JavaFX runtime components are missing, and are required to run this application

However, Eclipse will have auto-created a new Run Configuration named BasicJavaFXOne that can now be modified specifically for the new project.  There is a runtime modification that must be made to include the JavaFX library.  Go to Run->Run Configurations...  Select the new configuration BasicJavaFXOne.  Select the "(x)= Arguments" tab.  In the "VM arguments" box, something similar to the following text needs to be added

--module-path "\path\to\javafx-sdk-12.0.2\lib" --add-modules javafx.controls,javafx.fxml

(This text is taken from the openjfx.io page referenced above.)  This must be modified, however, to something local to your machine.  Specifically, the \path\to\javafx-sdk-12.0.2 portion should be custom, such as

--module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" --add-modules javafx.controls,javafx.fxml

Click Apply.  You can now run this application with this run configuration with the Run button.  The result should be a window popping up in the middle of the screen containing a filled in black circle.

No comments:

Post a Comment