2016-12-13 7 views
1

クライアントマシン上で実行されるEclipse RCPアプリケーションがあります。いくつかのサードパーティ製のjar(Database connector Jarsなど)をクラスパスにインポートし、クラスパス内のjarで再起動できるようにするには、RCPアプリケーションが必要です。実行中のRCPアプリケーションのクラスパスに外部jarファイルをインポートする方法

私はどこでも見ようとしましたが、私はそれのためのチュートリアルを見つけることができません。次のコードを使用してjarをロードしようとしました:

urls = new URL[] { new URL("jar", "", 
       "file:" + "C:\\Users\\Jars\\mysql-connector-java-5.1.38.jar" + "!/") }; 
     URLClassLoader cl = URLClassLoader.newInstance(urls, this.getClass().getClassLoader()); 
     Class<?> loadedClass = cl.loadClass("com.mysql.jdbc.Driver"); 

しかし、これは1つのクラスを読み込みます。 jarファイル内のすべてのクラスをロードしようとしても、jarファイルが本当にRCPアプリケーションのクラスパスにないため、内部依存関係を解決できません。

Bundle-ClassPath: Jars/mysql-connector-java-5.1.38.jar 

しかし、私は、ツールを使用してjarファイルをパッケージ化することはできません。

それを行うための通常の方法は、MANIFEST.MFファイル内のjarのパスを追加し、瓶とツールをパッケージ化することです。

ほとんどの記事は、プラグインにパッケージをパッケージし、依存関係を提供すると言っています。しかし、私はクライアントマシンでそれを行うことはできますか?クライアントがちょうど私に瓶のパスを提供していますか?

また、OSGIフレームワークOSGI Tutorial by Vogelについても読んでいます。しかし、私はそれを理解することが困難であると私はそれが私の要件を満たしていないと思う。

これらはSQLDeveloperのようないくつかのRCPアプリケーションであり、さまざまなJDBC jarをそのクラスパスにインポートしてから、そのクラスパスでJARを使用して再起動する機能があります。だから私はそれが可能だと思います。

誰でも助けてくれますか?または、私をリンクに再登録しますか?事前に感謝

答えて

1

あなたのRCPアプリケーションでこれを拡張することができます。このコードは、特定のフォルダの場所にあるすべてのjarファイルを読み取ります。 RCPベースのアプリケーション、特に ロードジャークラス

public static synchronized void loadAllJars() { 
     String path = System.getProperty("user.dir"); 
     System.out.println(path + "//jars" + " : Jar Path"); 
     System.out.println(System.getProperty("java.library.path") + " : Home path"); 
     File jarFile = new File(path + "//jars"); 
     for (File file : jarFile.listFiles()) { 
      System.out.println("Loding jar : " + file.getName()); 
      try { 
       URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader(); 
       URL url = file.toURI().toURL(); 
       for (URL it : Arrays.asList(loader.getURLs())) { 
        if (it.equals(url)) { 
         return; 
        } 
       } 
       Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }); 
       method.setAccessible(true); 
       method.invoke(loader, new Object[] { url }); 
      } catch (final java.lang.NoSuchMethodException | java.lang.IllegalAccessException 
        | java.net.MalformedURLException | java.lang.reflect.InvocationTargetException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

用のスタンドアロンアプリケーション、特に

import java.io.File; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class MainClass { 

public static void main(String[] args) { 
    File jarFile = new File("Jar file location"); 
    for (File file : jarFile.listFiles()) { 
     loadLibrary(file); 
    } 

    loadLibrary(jarFile); 
    connectToDataBase(); 
} 

private static void connectToDataBase() { 
    try { 
     Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver"); 
     Connection con = DriverManager.getConnection("jdbc:hive://172.22.75.***:10000/DBNamE", "****", 
       "***"); 
     Statement preparedStatement = con.createStatement(); 
     preparedStatement.executeQuery("use rapid"); 
     ResultSet resultSet = preparedStatement.executeQuery("select count (*) from flight"); 
     while (resultSet.next()) { 
      System.out.println(resultSet.getString(1)); 
     } 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public static synchronized void loadLibrary(java.io.File jar) { 
    try { 
     java.net.URLClassLoader loader = (java.net.URLClassLoader) ClassLoader.getSystemClassLoader(); 
     java.net.URL url = jar.toURI().toURL(); 
     for (java.net.URL it : java.util.Arrays.asList(loader.getURLs())) { 
      if (it.equals(url)) { 
       return; 
      } 
     } 
     java.lang.reflect.Method method = java.net.URLClassLoader.class.getDeclaredMethod("addURL", 
       new Class[] { java.net.URL.class }); 
     method.setAccessible(
       true); /* promote the method to public access */ 
     method.invoke(loader, new Object[] { url }); 
    } catch (final java.lang.NoSuchMethodException | java.lang.IllegalAccessException 
      | java.net.MalformedURLException | java.lang.reflect.InvocationTargetException e) { 
     e.printStackTrace(); 
    } 

} 
} 

については 接続Dbの

public class ConnectToDataBase { 

    public static void connectToDataBase() { 
     Connection con = null; 
     String url = "jdbc:mysql://localhost:3306/employees"; 
     try { 
      LoadJarUtil.loadAllJars(); 
      Properties properties = new Properties(); 
      properties.put("user", "****"); 
      properties.put("password", "****"); 
      @SuppressWarnings("unchecked") 
      Class<Driver> driver = (Class<Driver>) Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver", false, 
        ClassLoader.getSystemClassLoader()); 
      Connection connection = driver.newInstance().connect("jdbc:hive://172.22.***:10000", properties); 
      System.out.println("Connected"); 
     } catch (Exception err) { 
      err.printStackTrace(); 
     } 
    } 

} 
+0

にこれは完璧に動作します。しかし、相互に依存し合う複数のジャーがあればどうでしょうか?これらのジャーも正しい順序でインポートできますか? –

+1

はい、動作します。私は複数のジャーでテストしました。 –

関連する問題