Executing a class file sent over the network in java

五迷三道 提交于 2020-02-02 13:01:08

问题


I am trying to send a class file to offload "work" from a client to a server. I send a class file "MyClass.class" and receive it as "MyFooClass.class" successfully.

I need to execute the main() of MyFooClass at the server side and return the result.

I am trying to load the MyFooClass with a classloader but get the ClassNotFoundException.

Kindly help.

Aditya


回答1:


Unless you are doing RMI, the 'server' side won't have the class in its classpath and therefore fail with a ClassNotFoundException.

Now the bigger questions is why are you trying to send the whole class? I think it would be easier to have the class on the server and then send a text representation (json, xml, yaml, csv) of the state of the class on the client and then have the server read the representation, create the class(es) it needs and execute the work.




回答2:


Yes, you should not rename the class. The classloader will search for MyFooClass.class only but available is MyClass.class..So it is throwing an exception saying the expected MyClass.class is not available.




回答3:


The classname and the filename have to match. So you cannot simply rename the file.



来源:https://stackoverflow.com/questions/5269291/executing-a-class-file-sent-over-the-network-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!