2017-01-31 4 views
-1

私のクライアントがサーバーコンピューターからクラスファイルをロードする必要のあるチャンネルがあります。彼らはIPの同じ範囲にあります。私はサーバーとクライアントマシンで共通の2つのインターフェイスを持っています。また、サーバーマシン上にインターフェイスを実装するクラスです。クライアントマシンで次のコードを使用しますが、実行するとClassNotFoundExceptionが表示されます。クラスローダーを使用してクラスファイルをサーバーからクライアントにロードする方法

URL url = new URL("file:///E:/Computing/Master/classes/"); 
URLClassLoader ucl = new URLClassLoader(new URL[]{url}); 
Class clazz = ucl.loadClass("com.counter.controller.Action"); 
ProcessAlgorithm iAction = (ProcessAlgorithm) clazz.newInstance(); 

この場合のクラスローディングのプロセスはどれですか?

+0

私は質問があります。あなたの質問はどこですか? :D – Matt

+0

あなたはここで質問をすることになっています。あなた自身の質問に本当に答えることができますが、まず質問をする必要があります –

+0

Javaの組み込みRMIはこのクラスの配布を使って動作しますが、私は良い考えではないと警告します。コードよりもデータをできるだけ多く配布することをお勧めします。 –

答えて

1

解決策が見つかりました。ここで共有したいと思います。まず、この仕事はネットワーククラスの読み込みです。この名前でjavadocで見つけることができます。

URL url = new URL("file:///E:/Computing/Master/classes/"); 
URLClassLoader ucl = new URLClassLoader(new URL[]{url}); 
Class clazz = ucl.loadClass("com.counter.controller.Action"); 
ProcessAlgorithm iAction = (ProcessAlgorithm) clazz.newInstance(); 

あなたはそのURLを変更してもに「HTTP」二つの別々のコンピュータの間には、httpプロトコルはありませんが: は、実際には次のコードを使用して、リモートコンピュータからクラスファイルをロードする方法はありません。まあ、正しい方法で始めることができます。

192.168.10.1(サーバー)と192.168.10.2(クライアント)のIPを持つ2台のコンピューターがあるとします。クライアントがサーバーディスクからディスクにコピーしてはならないクラスファイルがあります。したがって、まず、JVM(サーバーとクライアント)の両方で同じインターフェースを定義し始めます。

package org.counter.biz; 

public interface ProcessAlgorithm { 

    int doProcess() ; 

} 

したがって、このインターフェイスはサーバーとクライアントで共通です。第二に、あなたのメインクラスは、サーバー上で定義されたとのインタフェースを実装する必要があります。

package org.counter.biz; 

public class Action implements ProcessAlgorithm { 

    @Override 
    public int doProcess() { 

     /* something to do */ 

    } 
} 

をそして最後に、クラスファイルは、ソケットまたはソケットチャネル上でクライアントに送信する必要があります。ここでは、自分のサーバーでSocketchannelを使用し、クライアントでSocketchannelを使用します。 (実際には、あなたが最初のソケットを介して2台のリモートコンピュータを接続する方法を知っている必要があります。)クライアントにクラスファイルのバイトを送信する

サーバー側のコード:

private void sendAlgorithmFile(SocketChannel client, String filePath) throws IOException { 

     ByteBuffer buffer = ByteBuffer.allocate(8192); 
     buffer.clear(); 

     /*file path like E:\\classes\\Action.class*/ 
     Path path = Paths.get(filePath); 
     FileChannel fileChannel = FileChannel.open(path); 
     int bytes = 0; 
     int counter = 0; 


     do { 
      bytes = fileChannel.read(buffer); 
      if (bytes <= 0) 
       break; 
      counter += bytes; 
      buffer.flip(); 
      do { 
       bytes -= client.write(buffer); 
      } while (bytes > 0); 
      buffer.clear(); 
     } while (true); 


     fileChannel.close(); 

    } 

を経由してファイルを送信するために多くの方法がありますソケット。それは私のコードであり、その正しさが検証されています。

ファイルを受信し、クライアントのディスクに保存されていないクラスに変更するクライアント側。

その後
package org.counter.biz; 

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

public class MyClassLoader extends ClassLoader { 

    private Socket clientChannel; 
    private int count = 0; 

    public MyClassLoader(Socket channel){ 
     this.clientChannel = channel; 
    } 

    @Override 
    protected Class findClass(String className){ 

     Class myClass = null; 

     InputStream inputStream = null; 

     try { 
      inputStream = clientChannel.getInputStream(); 
     }catch (IOException e){e.printStackTrace();} 


     byte[] bytes = new byte[8192]; 
     byte[] myBytes = null; 

     try { 
      while ((count = inputStream.read(bytes)) > 0){ 
       myBytes = new byte[count]; 
       System.arraycopy(bytes, 0, myBytes, 0, count); 
       myClass = defineClass(className, myBytes, 0, myBytes.length); 
      } 
      inputStream.close(); 
     }catch (IOException io){} 


     return myClass; 

    } 

} 

public class Client { 
    public static void main(String[] args) throws Exception{ 
    MyClassLoader myClassLoader = new MyClassLoader(clientSocket); 
    Class clazz = myClassLoader.findClass(null); 
    ProcessAlgorithm iAction = (ProcessAlgorithm) clazz.newInstance(); 
    } 
} 

次に、あなたが何か質問があれば、私は答えるためにここにいるこの

iAction.doProcess(); 

のようなクラスを使用することができます。 :)

関連する問題