Send anonymous class via sockets? (Object..Stream in Java)

So now I have a server that works with ObjectInputStreamand ObjectOutputStream.

The problem is that I have a custom (anonymous) class that extends java.lang.Date, which I try to send to the client and then compile.

Therefore, nowhere do I define a class on the client side, but I want to compile the class programmatically. I tried many different methods, but every time I get it ClassNotFoundException, because the class is not on the client side initially.

Class<?> dateClass = (Class<?>) in.readObject(); //This is where the CNF Exception occurs
Compiler.compileClass(dateClass);
+5
source share
1 answer

Java , JVM, . , Class, , Class .

, Class , JVM, - . - .

, - . - , , ( jar , ). , , , URLClassLoader, , ClassLoader.defineClass .

PS: . , ( VM ):

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
        Serializable payload = new Serializable() {
            @Override
            public String toString() {
                return "hello from the anonymous class";
            }
        };
        oos.writeObject(payload);
        oos.writeObject(payload.getClass());
    }

    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
        System.out.println(in.readObject());
        System.out.println(in.readObject());
    }
+7

All Articles