English 中文(简体)
• 如何将 Java服务器/Client代码编成CMD
原标题:How to compile this Java Server/Client code in CMD

I use Windows and I used JGrasp to save this code. The code you see below is perfect and correct (except I added a package name on the top of each class), because it is example code I took off a powerpoint which was made from the course textbook we use. It is a program where a 客户 has to type a circle radius, the server receives the client s circle radius and then calculates the circle area, sending the cirlce area back to the client. How do I compile AND run this program in command prompt? I never learned about java packages before by the way.

服务器

package program;
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 服务器 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("服务器"); // 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
        服务器Socket serverSocket = new 服务器Socket(8000);
        Platform.runLater(() ->
          ta.appendText("服务器 started at " + new Date() +  
 ));

        // Listen for a connection request
        Socket socket = serverSocket.accept();

        // Create data input and output streams
        DataInputStream inputFrom客户 = new DataInputStream(
          socket.getInputStream());
        DataOutputStream outputTo客户 = new DataOutputStream(
          socket.getOutputStream());

        while (true) {
          // Receive radius from the client
          double radius = inputFrom客户.readDouble();

          // Compute area
          double area = radius * radius * Math.PI;

          // Send area back to the client
          outputTo客户.writeDouble(area);

          Platform.runLater(() -> {
            ta.appendText("Radius received from client: " 
              + radius +  
 );
            ta.appendText("Area is: " + area +  
 ); 
          });
        }
      }
      catch(IOException ex) {
        ex.printStackTrace();
      }
    }).start();
  }

  /**
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
   */
  public static void main(String[] args) {
    launch(args);
  }
}

客户

package program;
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 客户 extends Application {
  // IO streams
  DataOutputStream to服务器 = null;
  DataInputStream from服务器 = 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("客户"); // 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
        to服务器.writeDouble(radius);
        to服务器.flush();

        // Get area from the server
        double area = from服务器.readDouble();

        // Display to the text area
        ta.appendText("Radius is " + radius + "
");
        ta.appendText("Area received from the server is "
          + area +  
 );
      }
      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
      from服务器 = new DataInputStream(socket.getInputStream());

      // Create an output stream to send data to the server
      to服务器 = new DataOutputStream(socket.getOutputStream());
    }
    catch (IOException ex) {
      ta.appendText(ex.toString() +  
 );
    }
  }

  /**
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
   */
  public static void main(String[] args) {
    launch(args);
  }
}
问题回答

您可以选择一个像D:/ 这样的家庭名录。 之后

  1. 制作新记名的方案(按你的包裹名称排列)

  2. 服务器和客户。 java 加入方案

  3. CMD 开放,扎根

  4. 执行:javac program/Server.java (maybe programServer.java ondows

  5. execute: java program.Server

  6. it s run!
  1. Compile the Java file to a *.class file : javac TheJavaFile. java. 这将产生“JavaFile”。 班级档案。

  2. Then execute Java file: java TheJavaFile

希望这一帮助。 并确保java的档案在javac的同一夹中,通常位于JDK。

  1. Make sure that Java is already installed in the system withjava -version command.
  2. Create two files named as Server.java and Client.java with content in your question.
  3. Compile code using the Java compiler. javac Client.java Server.java
  4. make a directory named program, that is your package name, and move Client.class and Server.class to this directory.
  5. run your server code with java program.Server and client code with java program.Client.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签