0

This is my code (I removed a lot of graphics logic as to see if it would run a most basic file)

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

class Client extends Application {
    public static String host = "127.0.0.1";
    public BufferedReader fromServer;
    public PrintWriter toServer;
    public Scanner consoleInput = new Scanner(System.in);

    public Client() {
    }
    
    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage displayStage) {     
        try {
            System.out.println("HELLO");
        } catch (Exception e) {
            System.err.println("Client connection error!"); 
            e.printStackTrace();
        }
    }
}

but the error messages are really bizarre and I'm not sure what exactly it means; it mentions a public access but afaik, public means anything can access it and I don't use reflection but its throwing errors regarding reflection and I'm really stumpted as to what could be causing this error.


Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class eHillsClient.Client
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891)
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access a member of class eHillsClient.Client with modifiers "public"
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@18/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more

1 Answers1

0

Make Client class public because a main class in JavaFX Applicaions must have a public access modifier to be picked by getConstructor() method in Application class.In normal cases it wouldn't cause an error but your main class will not be an entry point to the program(in JavaFx Applicaions the entry points is Applicaion class).

You can further read here

Yaphet17
  • 123
  • 9
  • Does Client not automatically default to public if no access identifier is given? – Arron Starmer Apr 26 '22 at 10:20
  • No you must explicitly add public access modifier. – Yaphet17 Apr 26 '22 at 10:23
  • 1
    @ArronStarmer looks like time to work through a tutorial on java language basics :) – kleopatra Apr 26 '22 at 10:23
  • Ah thanks. My memory has not served me well (for the default mode which this is the first time I've dealt with it) – Arron Starmer Apr 26 '22 at 10:27
  • 1
    The explanation in this is not really correct. The JavaFX launcher (invoked by `Application.launch()`) uses reflection to create an instance of the `Application` subclass, and for *that* reason the class must be `public`. It has nothing to do with being an entry point: the `main(…)` method is invoked without problem. I don’t know why you are referring to “compilation errors”: there are no compilation errors here. – James_D Apr 26 '22 at 10:46