【问题标题】:Cant get this simple client/server program to run无法运行这个简单的客户端/服务器程序
【发布时间】:2021-12-02 21:02:41
【问题描述】:

我的任务是让这个简单的客户端/服务器程序运行,它将圆的半径发送到服务器并返回区域。我实际上只是复制并粘贴了代码并尝试运行它,但我收到了这个错误。我在下面包含了客户端和服务器端的代码。谢谢。

错误:无法找到或加载主类 Client_Server.Client_Side 引起: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);
  }
}

【问题讨论】:

  • 阅读 openjfx.io 上的入门文档并关注它。
  • 或者用更亲切的话说,您缺少运行此项目所需的 JavaFX 库。在 Google 上搜索从哪里获取它以及如何添加它。 JavaFX 是一些 Java 运行时环境和开发工具包的默认设置,但没有随现代 JVM(我认为是 Java 8 及更高版本)提供。
  • 感谢您的输入,但我已经包含了 JavaFX 库,这也是我如此困惑的部分原因,
  • 如果您遵循上述文档,您将不会收到此错误。如果没有关于您实际在做什么的更多信息,很难就您可以在设置中更改哪些内容来纠正它提出建议。也许最简单的解决方案是使用预打包 JavaFX 的liberica,然后您就不需要“包含 JavaFX 库”了。

标签: java javafx


【解决方案1】:

如果您是学生,您可以获得 Intellij 的 Ultimate 版本,它简化了新项目的设置..

我刚刚测试了你的代码,它可以工作..

我创建了一个新的 JFX 项目..我添加了正确的 SDK..因为 JFX 是 Java 11 的最低要求..所以我下载了它并创建了项目..然后我在预先创建的包 1 中创建了 2 个普通类对于 Client_Side 和一个对于 Server_Side .. 然后首先运行服务器类并且它可以工作

【讨论】:

    【解决方案2】:

    当您似乎在管理编译时,您的运行时环境缺少一个 JavaFX 分发。您可以使用 JavaFX 安装运行时环境,例如 Liberica、Correto 或 Oracles 1.8-Runtime。

    否则,您也可以运行您的类并将 OpenJFX-JAR 添加到您的类路径中。有关如何设置类路径的更多信息,请查看Oracle Docs

    另一种选择是在您的操作系统上安装 OpenJFX。您也可以从 Maven 存储库下载 OpenJFX JAR。但是,您必须获取正确平台(mac、linux 或 win 分类器)的 JAR。

    【讨论】:

    • 现代 JavaFX 发行版设计为 run from the module path, not the class path
    • 有人可能会争辩说,正确的解决方案就是有效的解决方案?。 Java 的模块化系统颇具争议,Classpath 将继续存在,并将继续工作。但是,感谢您对此的意见,当然您可以对此进行投票,尽管我认为 Stack Overflow 最初并不是关于意见的。
    • 如果原始发布者尝试仅在类路径上使用 JavaFX 库直​​接运行 Client_Side 和 Server_Side 应用程序,则会失败“错误:缺少 JavaFX 运行时组件”。要以这种配置运行应用程序,需要创建两个Launcher 类来启动每个应用程序。执行将生成有关不受支持的配置的警告,但适用于 JavaFX 17。
    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多