Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run Stand Alone Java Application from server to any Client

I have Developed an Stand alone application (.jar) which is working on Linux environment, I want to Keep this Application on a server(Linux) and want to access through other systems.

Please suggest if this is Possible.

like image 711
Code Hungry Avatar asked Dec 06 '25 20:12

Code Hungry


2 Answers

Have you considered Java Webstart ? It'll allow clients to download your application from a webserver and run it locally.

It's traditionally used with GUI (Swing etc.) apps, but I've used it to run daemon and server processes locally.

It'll handle application updates automatically so your clients will only download a version if they'll need it. Otherwise they'll access their locally cached version.

like image 79
Brian Agnew Avatar answered Dec 08 '25 10:12

Brian Agnew


Linux system implements the Berkeley socket API, so yes, you can open communication for the other machines.

For this, you can use package java.net. For socket connection we can use: Socket, ServerSocket, and SocketAddress.

Socket is used for the client, ServerSocket is used to created a socket server, and SocketAddress is used to provide information that will be used as a target socket.

As the illustration, please find the projects below:

First Project SocketServerApp.java - build this and then run java -jar SocketServerApp.jar

package socketserverapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServerApp {

    public static void main(String[] args) throws IOException {
        //we defines all the variables we need
        ServerSocket server = null;
        Socket client = null;
        byte[] receivedBuff = new byte[64];
        int receivedMsgSize;   

        try 
        {
            //activate port 8881 as our socket server
            server = new ServerSocket(8881);
            System.out.println("Server started");

            //receiving connection from client
            client = server.accept();
            System.out.println("Client connected");           
        } 
        catch (IOException e) 
        {
            System.out.println(e.getMessage());
            System.exit(-1);
        }

        //prepare data stream
        InputStream in = client.getInputStream();
        OutputStream out = client.getOutputStream();

        //greet user if there is a client connection
        String data;
        data = "Hello from the Server!";
        out.write(data.getBytes());

        //accepting data from client and display it in the console.
        java.util.Arrays.fill(receivedBuff, (byte)0);
        while (true) {
             receivedMsgSize = in.read(receivedBuff);
             data = new String(receivedBuff);

             //if client type "exit", then exit loop and close everything
             if (data.trim().equals("exit"))
             {
                 out.write(data.getBytes());
                 break;
             }

             java.util.Arrays.fill(receivedBuff, (byte)0);
             System.out.println ("Client: " + data);
        }

        //close all resources before exiting
        out.close();
        in.close();
        client.close();
        server.close();
    }
}

Second project is the SocketClientApp.java - build this and then run java -jar SocketClientApp.jar

package socketclientapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClientApp {

    public static void main(String[] args) throws IOException {
        Socket client = null;
        InputStream in = null;
        OutputStream out = null;
        byte[] receivedMsg = new byte[64];

        try {
            client = new Socket("localhost", 8881);
            in = client.getInputStream();
            out = client.getOutputStream();
        } catch (UnknownHostException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        } catch (IOException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }

        String fromServer;
        String fromUser;

        in.read(receivedMsg);
        fromServer = new String(receivedMsg);
        System.out.println("Server: " + fromServer);

        fromUser = "Hello from Client";
        System.out.println("Sent to server: " + fromUser);
        out.write(fromUser.getBytes());

        fromUser = "exit";
        out.write(fromUser.getBytes());
        System.out.println("Sent to server: " + fromUser);

        out.close();
        in.close();
        client.close();
    }
}

Short to say this is a TCP/IP Communication. This type of communication method is very usual in an enterprise that has many kinds of softwares.

Hope that helps.

like image 43
Christian Tjhai Avatar answered Dec 08 '25 09:12

Christian Tjhai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!