繁体   English   中英

通过lan在其他Java应用程序上调用方法

[英]calling method on other java application via lan

我有2个Java应用程序通过LAN(WiFi网络)相互连接

第一个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);
    }
}
}

第二个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);
    }
}
}

发生的情况是,客户端应用程序使用服务器IP地址通过LAN向服务器应用程序发送消息-服务器应用程序具有方法调用zzz(),因此,我想要做的就是如何使客户端应用程序调用此方法(如果可能的话) )

谢谢

@MichalLonski如何使“ obj”指示给ServerApp

由于它是static方法,因此必须指向ServerApp.class ,如下所示:

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);
    }
}

您可以将其更改为不使用静态方法,而是使用实例方法。 为此,您必须创建ServerApp类的实例,如下所示:

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);
    }
}

编辑

如果还要指定要调用的方法的类,则可以通过以下方式进行操作:

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM