Reputation: 143
I have an Eclipse RCP application that run on clients machine. I need the RCP application to be able to import some third party jars (like Database connector Jars) to its class-path and then restart with the jars in the class-path.
I have tried to look everywhere but i cannot find a tutorial for it. I tried loading the jar using the following code:
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");
but this loads a single class. Even when i try loading all the classes in the jar, i am not able to get the internal dependencies resolved as the jar is not truly in the class-path of the RCP application.
The normal way to do it is by adding the path of the Jar in the Manifest.mf file and packaging the tool with the jar:
Bundle-ClassPath: Jars/mysql-connector-java-5.1.38.jar
But i cannot package the jar with the tool.
Most of the articles say to package the jars into plugins and provide the dependency. But can i do it at a client machine where the client just provides me with the path to the jar?
I have also read about the OSGI framework OSGI Tutorial by Vogel. But i am find it difficult to understand and i think it does not meet my requirement.
Ther are some RCP applications like SQLDeveloper those have the capablity of importing various JDBC jars in thier class-path and then restart with the jars in thie class-path. So i think it is possible.
Can anybody help me with this. Or redirrect me to a link? Thanks in advance
Upvotes: 1
Views: 767
Reputation: 88
Please find following code.You can extend this in your RCP application.This code reads all the jar files in a particular folder location. For standalone application specifically
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();
}
}
}
For RCP based application specifically Load jar Class
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();
}
}
}
Connect to 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();
}
}
}
Upvotes: 1