-1

My assignment is to get this simple client/server program to run, it sends the radius of a circle to a server and the area is returned. I literally just copied and pasted the code and tried to run it but I get this error. I have included the code for both client and server side below. Thanks.

Error: Could not find or load main class Client_Server.Client_Side Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

package Client_Server;

import java.io.*;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Client_Side extends Application
{
  // IO streams
  DataOutputStream toServer = null;
  DataInputStream fromServer = null;
  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {
    // Panel p to hold the label and text field
    BorderPane paneForTextField = new BorderPane();
    paneForTextField.setPadding(new Insets(5, 5, 5, 5)); 
    paneForTextField.setStyle("-fx-border-color: green");
    paneForTextField.setLeft(new Label("Enter a radius: "));
    
    TextField tf = new TextField();
    tf.setAlignment(Pos.BOTTOM_RIGHT);
    paneForTextField.setCenter(tf);
    
    BorderPane mainPane = new BorderPane();
    // Text area to display contents
    TextArea ta = new TextArea();
    mainPane.setCenter(new ScrollPane(ta));
    mainPane.setTop(paneForTextField);
    
    // Create a scene and place it in the stage
    Scene scene = new Scene(mainPane, 450, 200);
    primaryStage.setTitle("Client"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
    
    tf.setOnAction(e -> {
      try {
        // Get the radius from the text field
        double radius = Double.parseDouble(tf.getText().trim());
  
        // Send the radius to the server
        toServer.writeDouble(radius);
        toServer.flush();
  
        // Get area from the server
        double area = fromServer.readDouble();
  
        // Display to the text area
        ta.appendText("Radius is " + radius + "\n");
        ta.appendText("Area received from the server is "
          + area + '\n');
      }
      catch (IOException ex) {
        System.err.println(ex);
      }
    });
  
    try {
      // Create a socket to connect to the server
      Socket socket = new Socket("localhost", 8000);
      // Socket socket = new Socket("130.254.204.36", 8000);
      // Socket socket = new Socket("drake.Armstrong.edu", 8000);
      // Create an input stream to receive data from the server
      fromServer = new DataInputStream(socket.getInputStream());
      // Create an output stream to send data to the server
      toServer = new DataOutputStream(socket.getOutputStream());
    }
    catch (IOException ex) {
      ta.appendText(ex.toString() + '\n');
    }
  }

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

package Client_Server;

import java.io.*;
import java.net.*;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class Server_Side extends Application {
  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {
    // Text area for displaying contents
    TextArea ta = new TextArea();
    // Create a scene and place it in the stage
    Scene scene = new Scene(new ScrollPane(ta), 450, 200);
    primaryStage.setTitle("Server"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
    
    new Thread( () -> {
      try {
        // Create a server socket
        ServerSocket serverSocket = new ServerSocket(8000);
        Platform.runLater(() ->
          ta.appendText("Server started at " + new Date() + '\n'));
  
        // Listen for a connection request
        Socket socket = serverSocket.accept();
  
        // Create data input and output streams
        DataInputStream inputFromClient = new DataInputStream(
          socket.getInputStream());
        DataOutputStream outputToClient = new DataOutputStream(
          socket.getOutputStream());
  
        while (true) {
          // Receive radius from the client
          double radius = inputFromClient.readDouble();
  
          // Compute area
          double area = radius * radius * Math.PI;
  
          // Send area back to the client
          outputToClient.writeDouble(area);
  
          Platform.runLater(() -> {
            ta.appendText("Radius received from client: " 
              + radius + '\n');
            ta.appendText("Area is: " + area + '\n'); 
          });
        }
      }
      catch(IOException ex) {
        ex.printStackTrace();
      }
    }).start();
  }

  public static void main(String[] args) {
    launch(args);
  }
}
dheeke
  • 37
  • 4
  • Read the documentation on getting started at openjfx.io and follow it. – jewelsea Dec 02 '21 at 21:06
  • Or in kinder words, you're missing the JavaFX library that is required to run this project. Search on Google where to get it and how to add it. JavaFX came as a default with some few Java Runtime Environments and Development Kits, but is not shipped with modern JVMs (Java 8 and up i think) – JayC667 Dec 02 '21 at 21:43
  • Thanks for the input, but I HAVE included the JavaFX library, this is partly why I'm so confused, – dheeke Dec 02 '21 at 22:03
  • If you followed the mentioned documentation, you would not receive this error. Without more information on what you are actually doing, it is hard to make a recommendation of what you can change in your setup to correct it. Perhaps the easiest thing solution is to use [liberica](https://bell-sw.com/pages/downloads/#/java-17-lts%20/%20current), which comes with JavaFX pre-packaged, then you don't need to "include the JavaFX library". – jewelsea Dec 03 '21 at 01:41

2 Answers2

-1

If you are a Student you can get the Ultimate version of Intellij which simplifies settings for new projects..

Ijust tested your code and it works..

I created a new JFX project.. I added the right SDK.. for JFX is Java 11 the minimum.. so I downloaded it and created the project.. then I created 2 normal classes in the pre-created package one for Client_Side and one for Server_Side .. then run first the server class and it works

krym
  • 9
  • 1
-1

As you seem to manage compilation, your Runtime Environment misses an JavaFX-Distribution. You could either install an Runtime Environment with JavaFX, for example Liberica, Correto or Oracles 1.8-Runtime.

Otherwise, you may also run your class and add OpenJFX-JARs to your classpath. For more information about how to set the classpath, have a look at the Oracle Docs.

Another option is to install OpenJFX on your operating system. You can download the OpenJFX JARs from Maven repositories, as well. However, you must get the JARs for the correct platform (mac, linux or win classifier).

edean
  • 498
  • 3
  • 11
  • Modern JavaFX distributions are designed to be [run from the module path, not the class path](https://stackoverflow.com/questions/67854139/javafx-warning-unsupported-javafx-configuration-classes-were-loaded-from-unna). – jewelsea Dec 03 '21 at 01:37
  • One might argue, that a correct solution is a solution, that works . Javas modularization system is quite controversial, Classpath is here to stay and does and will work. However, thanks for your opinion on that and of course you got all right to vote about that, even though, I think, Stack Overflow is not about opinions in the first place. – edean Dec 03 '21 at 08:57
  • 1
    If the original poster tries to directly run the Client_Side and Server_Side applications with JavaFX libraries only on the classpath, it will fail "Error: JavaFX runtime components are missing". To run the applications in such a configuration, it would be necessary to create two [Launcher](https://github.com/openjfx/samples/blob/master/CommandLine/Non-modular/Maven/hellofx/src/main/java/hellofx/Launcher.java) classes to launch each application. Execution will generate warnings about the unsupported configuration but will work with JavaFX 17. – jewelsea Dec 03 '21 at 12:01