Importing javaFX (.jar) files into a Netbeans Maven Project - java

I am trying to import javafx (.jar) files into a maven project. I have made a project with Netbeans (Java with Ant -> Java Application) and want to use the same code in Netbeans (Java with Maven -> Java Application). With Ant, I can easily import and use the (.jar) files, but this seems to be a blocked functionality with Maven.
Any solution to this?
I have looked up on several posts and guides online with no luck.
NOTE: Implementing .jar files within maven have been solved. The code does not work though.
Current Error: "Exception running application com.mycompany.ep_game_ref.Game Command execution failed."
package com.mycompany.ep_game_ref;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Game extends Application
{
public static String[] args;
public static Stage primarystage;
public static ObservableList<Items.Item> items = FXCollections.observableArrayList();
public static MainCentral startGame = new MainCentral();
public static Room.Introduction intro = new Room.Introduction();
public static Settings.Difficulty SETTINGS = new Settings.Difficulty();
public static Player player = new Player();
#Override
public void start(Stage stage)
{
this.primarystage = stage;
this.primarystage.setScene(new Scenes.Welcome().setWelcomeScene());
this.primarystage.getIcons().add(new Image("images/aq_logo.png"));
this.primarystage.setResizable(false);
this.primarystage.show();
}
public static void main(String[] args) {
Application.launch();
}
}

You can use JavaFX in a different way:
File>New Project>Maven>JavaFX Application.

Related

How to solve JavaFX runtime components are missing on VS code IDE

I'm using JavaFx on visual studio code IDE. I always get this error:
JavaFX runtime components are missing and are required to run this application
I've already added the VM args and the javafx libraries.
Also some basic FX codes are compiled with ease, but once I use the FXMLLoader class, I get the aforementioned error.
package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application
{
#Override
public void start(Stage stage)
{
try
{
Parent root = FXMLLoader.load(getClass().getResource("/interfaces/SignIn.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
In launch.json, adding a new entry for the vmArgs including the --module-path with the local path to the JavaFX SDK and --add-modules with the required JavaFX modules:
"vmArgs": "--module-path /path/to/javafx/lib --add-modules javafx.controls,javafx.fxml"

Eclipse Could not find or load main class

Have been trying to set up JavaFX with Eclipse on Ubuntu. The code doesn't have an issue importing the javafx classes anymore (this took a LONG time to get working), but when running some simple test code, Eclipse can't seem to find the main class even when I right click on the file directly and run as Java Application.
package mainpackage;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Window");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Error: Could not find or load main class mainpackage.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

How to Fix NoClassDefFoundError in JavaFX?

I'm trying to build a small user interface using JavaFX, But I'm getting an error like this:
Error: Could not find or load main class myApp Caused by:
java.lang.NoClassDefFoundError: javafx/application/Application
This is my code:
and im using jdk 12.0.2
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class myApp extends Application{
public static void main(String[] args) {
Phonebook mybook = new Phonebook();
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Scene scene = new Scene(group, 600, 300);
scene.setFill(Color.GRAY);
primaryStage.setTitle("Phone Book");
primaryStage.setScene(scene);
primaryStage.show();
}
This is the Libraries and jdk I'm using:
Image1
I think JavaFX is not a part of the JDK > 9 any more. (Your version is 12.x.x)
Probably this could help you:
https://openjfx.io/openjfx-docs/#install-javafx
(Assuming you are using Maven) If this does not help, try to clean and build your Application.
Sometimes Maven does not recognize newly added dependencies.

javafx import cannot be resolved in eclipse

I am starting an assignment and just installed javafx from the Eclipse marketplace, after I installed and created a javafx project with fxml however in main the imports are showing errors saying "the import javafx cannot be resolved". I am new to Java and Eclipse and have tried to search for similar questions but they all seem to be different cases.
I would appreciate any help thank you.
Below is the code, and all the imports are showing errors:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Student.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I have figured that it is the library JavaFx SDK which is not able to be added to the project, is anyone good with Eclipse can help solve this problem? I tried configure build path and removing the JavaFx SDK and readding, still does not work.

NFC on Android using JavaFX Ports

I have been recently experimenting with JavaFXPorts and I have been trying to use it to build a native Android app. For the application I'm building I am trying to bake in NFC support but there doesn't seem to be much information out there about this. The only useful guide I found so far was some sample code written by johanvos on his BitBucket Repo here.
The problem I am facing now is using this code snippet, Netbeans keeps on reporting that the android libraries I'm trying to reference don't exist.
This is a some of my code:
package com.afropolymath.waitress;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.ReaderCallback;
import android.nfc.Tag;
import android.os.Bundle;
import javafxports.android.FXActivity;
public class Waitress extends Application implements ReaderCallback {
private Stage stage;
private StackPane rootLayout;
#Override
public void start(Stage stage) {
this.stage = stage;
this.initLayout();
Context ctx = FXActivity.getInstance();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(ctx);
nfcAdapter.enableReaderMode(FXActivity.getInstance(), this, NfcAdapter.FLAG_READER_NFC_A, Bundle.EMPTY);
}
public void initLayout() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/RootLayout.fxml"));
rootLayout = (StackPane) loader.load();
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(rootLayout, visualBounds.getWidth(), visualBounds.getHeight());
stage.getIcons().add(new Image(Waitress.class.getResourceAsStream("/assets/icon.png")));
stage.setScene(scene);
stage.show();
} catch (IOException e) {
}
}
#Override
public void onTagDiscovered(Tag tag) {
}
}
And these are the errors I'm getting:
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:12: error: package android.content does not exist
import android.content.Context;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:13: error: package android.nfc does not exist
import android.nfc.NfcAdapter;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:14: error: package android.nfc.NfcAdapter does not exist
import android.nfc.NfcAdapter.ReaderCallback;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:15: error: package android.nfc does not exist
import android.nfc.Tag;
^
/Users/chidieberennadi/NetBeansProjects/Waitress/src/main/java/com/afropolymath/waitress/Waitress.java:16: error: package android.os does not exist
import android.os.Bundle;
^
Any ideas on what might have been the problem?
While Johan Vos' NFC project works, it is based on the use of android and dalvik dependencies on the main package:
dependencies {
compile files("${ANDROID_HOME}/platforms/android-21/android.jar")
compile "org.javafxports:jfxdvk:8u60-b3"
}
But JavaFXPorts it's intended to create projects that can be deployed on different platforms, so now if you create a project using the Gluon's Plugin for NetBeans, you'll find four different folders to place the code:
Source Packages [Java], for all the common code, shared with all the platforms
Desktop/Java Packages, for Java code, only available running on Desktop
Android/Java Packages, for Java or Android code, only available running on Android
Ios/Java Packages, for Java code, only available running on iOS.
Also, if you check the project dependencies, you'll notice that the android.jar is available only for Android.
This means that you should create your regular project on the main package, and add the android part only in the Android package. In order to call the android class, you'll need to provide some mechanism on the main package to create an instance of it.
If you check the HelloPlatform sample, you'll notice that a PlatformService is used to load the classes depending on the platform, while PlatformProvider is an interface with the method/s that can be called from the main package, but will have the implementation given to each platform.
With this idea, but using Class.forName() instead of the service, Gluon Charm-Down library implements different native services.
You can have a look also at the GoNative sample and post that explains it in detail.
In case you want to implement a new service, like the NFC for Android,
these are the possible classes required.
Main package
NFCPlatform
public abstract class NFCPlatform {
public abstract NFCService getNFCService();
}
NFCService
public interface NFCService {
boolean isAvailable();
boolean isEnabled();
StringProperty tagIdProperty();
// other methods
}
Android Package
AndroidNFCPlatform
public class AndroidNFCPlatform extends NFCPlatform {
private AndroidNFCService nfcService;
#Override
public NFCService getNFCService() {
if (nfcService == null) {
nfcService = new AndroidNFCService();
}
return nfcService;
}
}
AndroidNFCService
Your implementation of the NFC service. Here we can use Android API. NetBeans won't complain.
This is a minimal implementation:
public class AndroidNFCService implements NFCService, ReaderCallback {
private final NfcAdapter nfcAdapter;
private StringProperty tagId;
public AndroidNFCService() {
NfcManager manager = (NfcManager) FXActivity.getInstance().getSystemService(FXActivity.NFC_SERVICE);
nfcAdapter = manager.getDefaultAdapter();
}
#Override
public boolean isAvailable() {
return nfcAdapter != null;
}
#Override
public boolean isEnabled() {
return isAvailable() && nfcAdapter.isEnabled();
}
#Override
public StringProperty tagIdProperty() {
if (tagId == null) {
tagId = new SimpleStringProperty();
}
return tagId;
}
#Override
public void onTagDiscovered(Tag tag) {
Platform.runLater(() ->
tagIdProperty().set(new String(tag.getId())));
}
}
Finally, you can use this service on your main class, once you get an instance of the NFCPlatform.
public static NFCPlatform getNFCPlatform() {
try {
if ("android".equals(System.getProperty("javafx.platform", "desktop"))) {
return (NFCPlatform) Class.forName("<your package>.AndroidNFCPlatform").newInstance();
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.out.println("Error " + ex);
}
return null;
}
#Override
public void start(Stage stage) {
NFCService nfcService = getNFCPlatform().getNFCService();
if (nfcService != null) {
System.out.println("Available: " + nfcService.isAvailable());
...
}
}

Categories