应用程序在intelliJ中运行,但.jar未运行

时间:2019-05-27 20:37:08

标签: java sockets networking jar

我已经使用Java中的套接字制作了两个应用程序(服务器和客户端)。它在 intelliJ 中工作正常,但是当我制作.jar文件时,正在运行服务器应用程序时,客户端应用程序未打开。但是,当我关闭服务器应用程序时,客户端应用程序将打开。

客户:

package sample;

import com.mysql.cj.jdbc.MysqlDataSource;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;


public class Main extends Application {
    static Stage stage;
    static boolean ch = true;
    @Override
    public void start(Stage primaryStage) throws Exception {

        Networking.connect("127.0.0.1", 5000);
        //login scene : opening scene
        //to change the value of stage to primaryStage
        stage = primaryStage;
        if(ch) {

            Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
            stage.setTitle("Electo");
            stage.setScene(new Scene(root));
            stage.show();
            //stage.getIcons().add(new Image(logo));
        }
        stage.setOnCloseRequest(event -> {
    try {
            Networking.output.writeUTF("Exit");
        }
        catch (IOException e)
        {
            Alert alert = new Alert(Alert.AlertType.ERROR, "Server error. Try restarting the app"
                    , ButtonType.OK);
            alert.showAndWait();
        }});
    }

    public static void main(String[] args) {
        launch(args);
    }
    //function to connect to database


    public Stage getStage()
    {
        return stage;
    }

    }

服务器:

package sample;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Networking {

    public static void connect(int port)
    {
        try
        {
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.print("Server is started");

            while(true)
            {
                Socket socket;
                    socket = serverSocket.accept();
                    DataInputStream input = new DataInputStream(socket.getInputStream());
                    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
                    Thread thread = new ServerThread(socket, input, output);
                    thread.start();
                    System.out.print("Client Accepted");
            }
        }
        catch(IOException exception)
        {
            exception.printStackTrace();
            Alert alert = new Alert(Alert.AlertType.ERROR, "Server error. Try restarting the application"
                    , ButtonType.OK);
            alert.showAndWait();
        }
    }

}

这是服务器线程类的run()方法

public void run() {
        String line = "";
                while(true) {
            try {
                line = input.readUTF();
                if(line.equalsIgnoreCase("Exit"))
                {
                    this.socket.close();
                    break;
                }
                doWork(line);
            } catch (IOException e) {
                Alert alert = new Alert(Alert.AlertType.ERROR, "Server error. Try restarting the app"
                        , ButtonType.OK);
                alert.showAndWait();
            }


            }
        }

客户端网络类:

package sample;

import com.mysql.cj.conf.DatabaseUrlContainer;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Networking {
    private static Socket socket = null;
    public static DataOutputStream output = null;
    public static DataInputStream input = null;

    public static void connect(String Address, int port) {
        try {
            socket = new Socket(Address, port);
            System.out.print("Connected");
            input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            output = new DataOutputStream(socket.getOutputStream());

        } catch (IOException e) {
            Alert alert = new Alert(Alert.AlertType.ERROR, "Server error. Try restarting the app"
                    , ButtonType.OK);
            alert.showAndWait();
            Main.ch = false;
        }
    }
}

添加:这是我在命令提示符下运行Client.jar时的例外情况。

ConnectedException in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at sample.Main.start(Main.java:33)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        ... 1 more
Exception running application sample.Main

1 个答案:

答案 0 :(得分:0)

use Tie::Array::CSV qw();
tie my @file, 'Tie::Array::CSV', 'servers.csv';

for my $server (@file) {
    next if 'ip' eq $server->[0]; # skip table header
    my $ping_result = rand > 0.5 ? 'Yes' : 'No'; # fake ping
    $server->[1] = $ping_result; # update file
}