Note that NetBeans has the possibility to create different project groups. This is very handy if you have different projects that you want to put together.
In the Project tree view example, you see that there's a Projects tab on top with Pi4J V2 as title.
To make such a project group, select File -> Project Groups...
After selecting this, you will see a new window where you can see an overview of the already defined groups.
You can select a group or also create a new one. That's what you have to do if you want to combine related projects together in one group.
If you select New Group... a new window will pop up where you can define your new group and what should be in by default.
You can give a name to the group. The name can contain spaces.
When you create a new group you have different possibilities:
Free Group: such group has no connections whatsoever with existing groups
Project and All Required Projects: this is a kind of "master" group with different sub groups in it.
Folder of Projects: you can take the content of a whole folder and turn it into a group
I go for the first choice: Free Group.
You can add the currently open projects to it or not. Best is to untick this option if you want a new group without any legacy in it.
The other option is to save the project list automatically. This is the more handy choice and you can leave this one ticked. It will automatically add new projects to the group.
Once the choice is made, press the Create Group and you're done. You will end up with a clean new group where you can start creating projects which will be automatically listed/be part of the new group just created.
When you select the File -> Project Groups... option again in the File menu, you'll see the new project has been added to the list of projects
Launch NetBeans and select File -> New Project or use the icon in the toolbar.
Then select Java with Ant as category and Java Application as Projects.
Press the Next button.
In the next screen, give a name for the project. Also change the project location if needed.
The moment you start typing the project name, other items in the dialogue will change accordingly, resulting in a separate folder in which the project will be placed.
Press the Finish button once everything is put in place. NetBeans will start creating the framework for this project. See next section.
After a couple of moments, NetBeans will show you the layout of the new project you created. As you can see in the image on the right side, there's already an existing project present.
The new project has two directories: one for the source code and one for the libraries.
The source code contains one Java file with the main() method in it. This is the starting point.
The library contains one member, the default JDK. In this example, the default Java SDK is 11 on my system.
Note that NetBeans has the possibility to create
Once we have the initial framework of our new project, we have to convert it to a project that is capable of running on a Raspberry Pi. The first thing we have to do, is to add the Pi4J libraries to the Libraries section that you can see in the image above.
But first, we have to make a library that contains all the necessary .jar files that are needed to create a basic Pi4J project that can run on the RPi.
To create a new library, select Tools -> Libraries. A new window will open. You will see a list of existing libraries which will contain one or more class paths, depending on how the library is composed. One such example is the Java EE 7 Endorsed API Library (don't ask me what this is, I have absolutely no clue where this could be used).
The library that is going to be created will also contain different class paths, since it will contain all the important Pi4J libraries it needs.
But first, we need to make those libraries. How this is done is explained here.
Once we have the necessary .jar files, we can create a new library.
Press the New Library... button and give it a name. Leave the library type as is.
Press the OK button and the empty, new library will be added to the list of libraries.
Now press the Add JAR/Folder... button and point to the location where the .jar files have been built. Select the following .jar files:
pi4j-core.jar
pi4j-library-pigpio.jar
pi4j-plugin-pigpio.jar
pi4j-plugin-raspberrypi.jar
slf4j-api.jar
slf4j-simple.jar
It should look like the 4th image on the right of this section (you can widen the screen if you want).
If you have source .jar or documentation .jar files, you can select the tabs Sources and Javadoc respectively and add them using the same way of working as for the Classpath tab.
Press the OK button to close this window. We have now made an own library with the name MyLibrary.
I already made a library with exactly the same content, called Pi4J_V2, so we will delete the MyLibrary library and use the one already made.
Now that we have our own library, it's time to put it to work. Go back to the project in NetBeans and right-click on the Libraries section in the Projects tree view.
Select Add Library... and choose Pi4J_V2 as library to add to your project. If you expand the Libraries section you will see the different Pi4J libraries have been added to your project. From now onwards, you can start using the content of those libraries.
That's what's going to happen in the test project we're creating.
We already have the framework of our test project. Now change the content of the static main() method with the below content:
// TODO code application logic here
final Console console = new Console();
console.box("Geert");
console.title("<-- The Pi4J Project -->", "Minimal Example Project");
Context pi4j = Pi4J.newAutoContext();
PrintInfo.printLoadedPlatforms(console, pi4j);
PrintInfo.printDefaultPlatform(console, pi4j);
PrintInfo.printProviders(console, pi4j);
PrintInfo.printRegistry(console, pi4j);
This will call a few of the methods in one of the libraries we just added. When NetBeans complains about classes it can't find, use the Alt-Enter key combination to add the necessary imports to your application.
It's not a lot yet, but it's a first attempt.
We also need another class called PrintInfo which will provide the necessary static methods used in the code above.
Now that all the code is in place, we can compile the project. By default the project will be compiled for Windows. Our purpose is to compile on the Windows machine and then transfer it to the RPi.
To do this, we have to select the correct platform first. We created our platform here (link still to be added) so now we can make use of it.
Select the project, right-click and select Properties
In the next screen, select the Run option
In the Configuration section, type in a name of your preference
In the Runtime Platform, select the RPi platform you created (very important!).
Press the OK button
The next build should now result in transferring the project to the RPi and start running it. When you choose for the debug build, you should now be able to put breakpoints and step through your code!