我编写了一个Java类加载器来加载.jar
文件中的类。
但是当我加载一个类时,我得到一个ClassNotFoundException
。 .jar
文件位于文件夹资产中。在操作之前,我将.jar
文件复制到另一个目录中。
为什么会发生这种情况?如何从jar文件中加载类?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
try {
final File dexInternalStoragePath = new File(getDir("lib", Context.MODE_PRIVATE), SECONDARY_DEX_NAME);
(new PrepareDexTask()).execute(dexInternalStoragePath);
if (dexInternalStoragePath.exists()) {
final File optimizedDexOutputPath = getDir("outdex", Context.MODE_PRIVATE);
DexClassLoader cl = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(),
optimizedDexOutputPath.getAbsolutePath(), null, getClassLoader());
try {
JarFile jarFile = new JarFile(dexInternalStoragePath.getAbsolutePath());
Enumeration<?> e = jarFile.entries();
URL[] urls = { new URL("jar:file:" + dexInternalStoragePath.getAbsolutePath() + "!/") };
URLClassLoader cl1 = URLClassLoader.newInstance(urls);
while (e.hasMoreElements()) {
JarEntry je = (JarEntry) e.nextElement();
if (je.isDirectory() || !je.getName().endsWith(".class")) {
continue;
}
String className = je.getName().substring(0, je.getName().length() - 6);
className = className.replace('/', '.');
if (className.equals("com.common.lvl.LibraryProvider")) {
LibraryInterface lib = (LibraryInterface) Class.forName(className, true, cl1).newInstance();
//Class<?> libProviderClazz = cl1.loadClass(className);
//LibraryInterface lib = (LibraryInterface) libProviderClazz.newInstance();
lib.CheckLVL(this, "hello");
}
}
// Class<?> libProviderClazz =
// cl.loadClass("com.common.lvl.LibraryProvider");
// LibraryInterface lib = (LibraryInterface)
// libProviderClazz.newInstance();
// lib.CheckLVL(this, "hello");
} catch (Exception ex) {
Logger.Instance().WriteLine(this, ex.getMessage());
}
}
} catch (Exception ex) {
}
}
private boolean prepareDex(File dexInternalStoragePath) {
BufferedInputStream bis = null;
OutputStream dexWriter = null;
try {
bis = new BufferedInputStream(getAssets().open(SECONDARY_DEX_NAME));
dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath));
byte[] buf = new byte[BUF_SIZE];
int len;
while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
dexWriter.write(buf, 0, len);
}
dexWriter.close();
bis.close();
return true;
} catch (IOException e) {
if (dexWriter != null) {
try {
dexWriter.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return false;
}
}
private class PrepareDexTask extends AsyncTask<File, Void, Boolean> {
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
}
@Override
protected Boolean doInBackground(File... dexInternalStoragePaths) {
prepareDex(dexInternalStoragePaths[0]);
return null;
}
}
答案 0 :(得分:0)
添加你的jar文件,如下所示
转到 - &gt; eclipse中的项目属性 - &gt; java构建路径 - &gt;图书馆 - &gt;添加外部Jar ,然后浏览并找到您的jar文件。