0

i have 2 java applications connected to each other via LAN (wifi network)

the first one ServerApp.java

public class ServerApp {

public static void zzz(){
    System.out.println("hi");
}

public static void main(String[] args) {
    try {
        ServerSocket ss=new ServerSocket(6666);
        Socket s=ss.accept();
        DataInputStream dis = new DataInputStream(s.getInputStream());
        String str =(String)dis.readUTF();
        System.out.print("message : "+str);
        ss.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}
}

the second one ClientApp.java

public class ClientApp {

public static void main(String[] args) {
    try {
        Scanner in = new Scanner(System.in);
        System.out.print("send message to the server ?[y/n]:");
        String inputString=in.next();
        if ("y".equals(inputString)) {
            Socket s= new Socket("192.168.20.125", 6666);
            DataOutputStream dout=new DataOutputStream(s.getOutputStream());
            dout.writeUTF("hellow server\n");
            dout.writeUTF("zzz");
            dout.flush();
            dout.close();
            s.close();
        } else if ("n".equals(inputString)) {
            System.out.println("exit");
        } else {
            System.out.println("error: you should enter a valid value");
        }
    } catch (IOException e) {
        System.out.println(e);
    }
}
}

what happens is, the client app send a message to the server app via LAN using the server IP address - the server app have a method call zzz() so all I want is how do I make the client app call this method ( if possible )

thanks

mustafa-naeem
  • 37
  • 1
  • 10
  • Possible duplicate: [How do I invoke a Java method when given the method name as a string?](https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string) – Michal Lonski Apr 06 '18 at 08:24
  • @MichalLonski how to I make the "obj" indicate to the ServerApp ?? – mustafa-naeem Apr 06 '18 at 09:03

1 Answers1

1

@MichalLonski how to I make the "obj" indicate to the ServerApp

As it is static method you have to point ServerApp.class, like below:

public class ServerApp {

    public static void zzz() {
        System.out.println("hi");
    }

    public static void main(String[] args) throws Exception {
        String methodName = "zzz";

        java.lang.reflect.Method method = ServerApp.class.getMethod(methodName);
        method.invoke(ServerApp.class);
    }
}

You can change it to use not static, but instance methods. In order to do that you have to create an instance of ServerApp class, like this:

public class ServerApp {

    public void foo() {
        System.out.println("Hello there from non static method!");
    }

    public static void main(String[] args) throws Exception {
        String methodName = "foo";

        ServerApp app = new ServerApp();
        java.lang.reflect.Method method = app.getClass().getMethod(methodName);
        method.invoke(app);
    }
}

Edit:

If you want to specify also the class of which method you want to call, you can do it this way:

package com.example;

class Foo {

    public static void bar() {
        System.out.println("Hello there.");
    }
}

public class ServerApp {

    public static void main(String[] args) throws Exception {
        //read the class and method name from the socket
        String className = "com.example.Foo";
        String methodName = "bar";

        Class<?> clazz = Class.forName(className);
        clazz.getMethod(methodName).invoke(clazz);
    }
}
Michal Lonski
  • 752
  • 10
  • 20
  • really good point - but I want the ClientApp to invoke zzz() not the ServerApp itself - remember that ClientApp & ServerApp are 2 projects on two different computers connected to each other via LAN ( wifi network ) – mustafa-naeem Apr 06 '18 at 09:41
  • ((how to I make the "obj" indicate to the ServerApp)) I'm soooooo sorry I was mean the ClientApp – mustafa-naeem Apr 06 '18 at 09:45
  • What do you mean? Do you want to specify class and method name in the client app, send those names to server app and execute it there? – Michal Lonski Apr 06 '18 at 13:41
  • yes exactly -> look i can send a message from the ClientApp to the ServerApp easily , so how can i make the ClientApp invoke ( or call ) the method zzz() in the ServerApp so the method zzz() get executed inside the ServerApp because the ClientApp invoke it -- I know it's possible but don't know how exactly to do it, like if - for example - rather than send the message in string form, I send it in executable form ( of course the message will be the name of the method inside the ServerApp – mustafa-naeem Apr 06 '18 at 15:13
  • Isn't that what I just answered? You send the method and class name (as string message) from the ClientApp to the ServerApp which executes it via reflection. Eh. – Michal Lonski Apr 06 '18 at 18:24
  • You just are a genius yes perfect - thanks you man you made my day and saved my life :)))))) – mustafa-naeem Apr 06 '18 at 20:51