无法运行这个简单的客户端/服务器程序



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

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

如果你是一名学生,你可以获得Intellij的终极版本,它简化了新项目的设置。。

我刚刚测试了你的代码,它很有效。。

我创建了一个新的JFX项目。。我添加了正确的SDK。。对于JFX,Java 11是最低要求。。所以我下载了它并创建了这个项目。。然后我在预先创建的包中创建了两个普通类,一个用于Client_Side,另一个用于Server_Side。。然后首先运行服务器类,它就可以运行

当您似乎在管理编译时,您的运行时环境错过了JavaFX发行版。您可以使用JavaFX安装运行时环境,例如Liberica、Correto或Oracles 1.8-Runtime。

否则,您还可以运行您的类并将OpenJFX JAR添加到类路径中。有关如何设置类路径的更多信息,请参阅Oracle文档。

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

相关内容

最新更新