是的,这是可能的。
其实也不是什么漂亮的办法……
您使用一个名为 lib 的文件夹并将您的 odbc jar 放在那里,使用 lib jar 的相对路径,例如“lib/jodbc.jar”
您可以将jar嵌入到一个名为resource的文件夹中,之后每次启动应用程序时,您都会查找文件夹lib/jodbc.jar,如果不存在,您可以创建该文件夹并将资源文件复制到您的应用程序中到文件夹并完成。
我在一些应用程序中使用了这种方法,到目前为止效果很好。我有同样的问题,用户添加类路径很困难,所以我只在我的应用程序中使用“便携式”jre 和库。
很抱歉没有显示示例,但我现在正在上课。
如果您需要更多示例,请告诉我,我可以在星期一回答。
Cya。
首先你需要在你的项目中添加这个库。
如果您使用的是 NetBeans,请进入您的项目属性,在您的 lib 中添加 jar,但首先在您的项目文件夹中放置一个名为 lib 的文件夹和您的 .jar,然后添加 jar 并选择相对路径.
复制资源:
//Create the /lib directory with the .jars and the service.exe
//The resource_path is the package where are your files...
String resource_path = "br/com/myservice/resources/";
Util.copyResource(this.getClass().getClassLoader(), resource_path, "PAF_Service.exe", currentDirectory);
resource_path = resource_path + "lib/";
String directory_lib = currentDirectory + "lib/";
File dir_lib = new File(directory_lib);
if(!dir_lib.exists()){
dir_lib.mkdir();
}
Util.copyResource(this.getClass().getClassLoader(), resource_path, "ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar", directory_lib);
Util.copyResource(this.getClass().getClassLoader(), resource_path, "log4j-1.2.17.jar", directory_lib);
Util.copyResource(this.getClass().getClassLoader(), resource_path, "WinRun4J.jar", directory_lib);
copyResource 方法:
public static boolean copyResource(ClassLoader classLoader, String resource_path, String resource, String destiny){
FileOutputStream fos = null;
Boolean retorno = false;
try {
InputStream is = classLoader.getResourceAsStream(resource_path + resource);
if(is == null){
System.out.println("Resource not found! >> " + resource_path + resource);
retorno = false;
}
fos = new FileOutputStream( new File(destiny, resource));
byte[] buffer = new byte[1024];
int read = -1;
while( (read = is.read(buffer)) != -1 ) {
fos.write( buffer,0,read);
}
fos.flush();
fos.close();
retorno = true;
} catch (FileNotFoundException ex) {
Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if(fos != null)
fos.close();
} catch (IOException ex) {
Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
}
}
return retorno;
}