我有一个 Project1ExeJarA.jar 文件,其中包含 log4j-1.2.17.jar
我将 Project1ExeJarA.jar 复制到Android Studio项目的jar文件夹中,然后将此Grab添加到Gradle构建路径中。
由于此行代码而导致错误:
Logger logger = Logger.getLogger("Class1.class");
Eclipse IDE中的Project1,用于生成 Project1ExeJarA.jar :
package com.package1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import org.apache.log4j.Logger;
public class Class1 {
Logger logger = Logger.getLogger("Class1.class");
public void method2() {
logger.error("logger error from method2");
logger.info("logger info from method2");;
}
public void method3() {
System.out.println("System.out.println from method3");
}
public void method4() {
java.util.logging.Logger loggerA = java.util.logging.Logger.getLogger("Project1");
loggerA.log(Level.INFO, "java.util.logging.Logger Level.INFO from method4");
}
}
现在将以上代码导出到“可运行的Jar文件”中
在Android项目中,在lib中添加jar Project1ExeJarA.jar ,然后在Gradle中提及。
现在使用以下代码单击某些按钮:
public void connect2(View view) {
Log.i(TAG, "connect2 start");
Class1 class1 = new Class1();
Log.i(TAG, "connect2 method2");
class1.method2();
Log.i(TAG, "connect2 method3");
class1.method3();
Log.i(TAG, "connect2 method4");
class1.method4();
Log.i(TAG, "connect2 end");
}
在Logcat中出现以下错误:
I/com.example.connect3.MainActivity: connect2 start
System.err: log4j:WARN Error during default initialization
System.err: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;
System.err: at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:140)
System.err: at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:104)
System.err: at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:805)
System.err: at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:768)
System.err: at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:648)
System.err: at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:514)
System.err: at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:580)
System.err: at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:526)
System.err: at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
System.err: at org.apache.log4j.LogManager.getLogger(LogManager.java:228)
System.err: at org.apache.log4j.Logger.getLogger(Logger.java:104)
System.err: at com.package1.Class1.<init>(Class1.java:16)
System.err: at com.example.connect3.MainActivity.connect2(MainActivity.java:25)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
System.err: at android.view.View.performClick(View.java:6294)
System.err: at android.view.View$PerformClick.run(View.java:24770)
System.err: at android.os.Handler.handleCallback(Handler.java:790)
System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
System.err: at android.os.Looper.loop(Looper.java:164)
System.err: at android.app.ActivityThread.main(ActivityThread.java:6494)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "java.beans.Introspector" on path: DexPathList[[zip file "/data/app/com.example.connect3-KtUJ95u2gbuveuiLcKx_Zg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.connect3-KtUJ95u2gbuveuiLcKx_Zg==/lib/x86, /system/lib, /vendor/lib]]
System.err: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
System.err: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
System.err: ... 24 more
I/com.example.connect3.MainActivity: connect2 method2
System.err: log4j:WARN No appenders could be found for logger (Class1.class).
System.err: log4j:WARN Please initialize the log4j system properly.
System.err: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I/com.example.connect3.MainActivity: connect2 method3
I/System.out: System.out.println from method3
I/com.example.connect3.MainActivity: connect2 method4
I/Project1: java.util.logging.Logger Level.INFO from method4
I/com.example.connect3.MainActivity: connect2 end