No class found exception while using json-lib in android - java

I am taking the input from the web, which is an Xml file and converting into a Json data using the library json-lib . I have created a user library and added the following jars into it:-
json-lib-2.3-jdk15.jar
commons-collections.jar
commons-lang.jar
commons-logging.jar
commons-beanutils.jar
ezmorph-1.0.6.jar
xom-1.1.jar
But still gives the following error:-
08-04 13:58:31.642: ERROR/dalvikvm(484): Could not find class 'net.sf.json.xml.XMLSerializer$CustomElement', referenced from method net.sf.json.xml.XMLSerializer.addNameSpaceToElement
Can anyone help me out in resolving this issue.

Either you have a sdk level / jdk level conflict. I mean dalvik can't get the byte code of the CustomElement class of your librairy as it is compiled with to recent features for your SDK like annotations for instance.
Or there is a conflicting librairy json-lib in some other of your jars or lib folders.
(the 3 first comments are not relevant, it's just the way inner classes are compiled, using a $)
Regards,
Stéphane

Since android already support json org.json a different json library may conflict. (You can download the jar here)
Try to use this library instead of an external library on android.
BTW: You can also use this library if you need on any java code (not only android)

Related

How to use google/api/annotations.proto with Android

I have a problem with proto file in my project
I had import in my proto file:
import "google/api/annotations.proto";
I am getting following error when building the project.
Import "google/api/annotations.proto" was not found or had errors.
How can I use this import in my project? Should I add something to my build.gradle?
On non-Android, you could add this dependency to your build.gradle:
compile 'com.google.api.grpc:proto-google-common-protos:1.12.0'
However, Android uses Protobuf "Lite" instead of full Protobuf and there's not a pre-generated library with Lite for this proto. There is an open issue about this.
However, a workaround discussed for the well-known protos can be used here as well. Namely, use a protobuf dependency instead of a compile dependency. This will generate the code as part of your build.
protobuf 'com.google.api.grpc:proto-google-common-protos:1.12.0'
Unfortunately, this solution only really works for applications. If two libraries use this "solution" they must never be included into the same application as they will have duplicated (and potentially have different versions of) the generated classes.

NoSuchMethodError after cleaning the project

I'm currently getting this error:
java.lang.NoSuchMethodError: org.json.JSONObject.keySet()Ljava/util/Set;
at ee.ut.cs.Parser.accessLint(Parser.java:39)
I have tried cleaning the project to no awail.
I suspect I have an error in the src/plugin/parse-htmlraw/build.xml while creating the jar file but I'm not certain. I understand that this error is because the function does not exist at runtime, but the object is created which means that the class is there, just not that function. I decompiled the .class file in created jar and it has the necessary functions.
Code is available at https://github.com/jaansusi/WCAGgrader
Q: What is wrong with the build that produces this error?
The problem is that even if I put the necessary class files in the jar I create, they are not linked correctly and the class that's called in the jar can't locate functions inside the other classes. The class object JSONObject is created but the functions inside the JSONObject class can't be found.
If you do not find the problematic version, there is a possibility you get it (especially if you are using Spring) from the following dependency -
<artifactId>android-json</artifactId>
<groupId>com.vaadin.external.google</groupId>
excluding it worked for me,
An easy way of analyzing dependencies is the maven-helper plugin in Intellij, see here
Check for the version you have used.
There might be a case where 2 different versions are being used which in turn causes this error.
To their own maven local repository com\Google\code\gson\gson, see if there are two or more version about json, will have to do is to delete the old, and remember to look at any other place in the project is introduced into the old version of the dependence, if any, change the old version of the dependence to the new version is perfectly solved this problem

CoAP Example for android Use Californium

I'm trying to build a CoAP server in android, and i's using californium's example.
https://github.com/curioustechizen/Californium/tree/4ace5b85974a0646aaaa6ab43c9314d1c5a438e1
But I got error with Http.java in package ch.ethz.inf.vs.californium.layers
it shows
The type org.apache.http.entity.ContentType cannot be resolved. It is indirectly referenced from required .class files
I'm thinking the problem is about the wrong .jar file
the library I imported is like this:
apache-mimne4j.core-0.7.2.jar californium-0.18.7-final.jar
commons-codec-1.6.jar commons-io-2.4.jar commons-logging-1.1.1.jar
google-http-client-1.10.3-beta.jar google-oauth-client-1.10.1-beta.jar
gson-2.1.jar guava-11.0.1.jar httpclient-4.2.1.jar
httpclient-4.3.2.jar httpcore-nio-4.2.1.jar jsr305-1.3.9.jar
junit-3.8.1.jar junit-4.8.1.jar mockito-core-1.8.5.jar
hope anyone can tell me whats the problem or how to fix it, and also ask to some open source about CoAP's android example.
I think you are missing a http-components jar there.
Do you use maven?
Please note: your fork is too old (2 years).
You can find the actual Californium version at https://eclipse.org/californium/
It does not have android support but I see your fork only removes a lot of files and adds an android manifest at most - so it could be done too.

Java/JAXB/JAK error in Matlab using Marshaller

I'm having a Problem with a Java program that I am developing that is using a jar file that was created by the Mathworks JA builder. In this Matlab program, it is using JAK to create a KML file for Google Earth. I have had no problem on the development computer with the full Matlab ide. However when taken over to another machine that only has the MCR I get the following error:
javax.xml.bind.PropertyException: name: com.sun.xml.bind.namespacePrefixMapper value: de.micromata.opengis.kml.V_2_2_0.kml$NameSpaceBeautyfier#15cb235
at javax.xml.helpers.AbstractMarshallerImpl.setProperty(Unkown Source)
at com.sun.xml.internal.bind.V2.runtime.MarshallerImpl.setProperty(Unkown Source)
at de.micromata.opengis.kml.V_2_2_0.kml.createMarshaller(kml.java:642)
at de.micromata.opengis.kml.V_2_2_0.kml.marshal(kml.java:682)
Is this something that is related to the Classpath?
Thanks for any help.
Do you have the same version of the JAXB libraries on both systems? Looks like it might be a versioning conflict. Searching on your error I came across this page: http://timepassguys.blogspot.com/2011/12/jaxb-exception-javaxxmlbindpropertyexce.html
Maybe that solution is something you can try?
You are using the JAXB implementation that is included in the JDK (com.sun.xml.internal.bind), but are specifying the property for the Metro JAXB implementation (com.sun.xml.bind). Note that Metro JAXB undergoes a package rename when it is included in the JDK.
Options
You could use the com.sun.xml.internal.bind.namespacePrefixMapper property.
You could use a jaxb.properties file to specify the use of the Metro JAXB implementation.
For More Information
http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String() in Java EE application

I am developing a Java EE application in which I need Base64 Encoding/Decoding
So I added commons-codec-1.5.jar in WEB-INF/lib folder of my application and used
import org.apache.commons.codec.binary.Base64;
in the Java file.
During compile time, when I type Base64, it shows encodeBase64String method is available. But during runtime it is throwing an exception like this:
java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String
I have the JAR in the buildpath, but still I don't understand why it throws me the above error.
That method was introduced in Commons Codec 1.4. This exception indicates that you've an older version of Commons Codec somewhere else in the webapp's runtime classpath which got precedence in classloading. Check all paths covered by the webapp's runtime classpath. This includes among others the Webapp/WEB-INF/lib, YourAppServer/lib, JRE/lib and JRE/lib/ext. Finally remove or upgrade the offending older version.
Update: as per the comments, you can't seem to locate it. I can only suggest to outcomment the code using that newer method and then put the following line in place:
System.out.println(Base64.class.getProtectionDomain().getCodeSource().getLocation());
That should print the absolute path to the JAR file where it was been loaded from during runtime.
Update 2: this did seem to point to the right file. Sorry, I can't explain your problem anymore right now. All I can suggest is to use a different Base64 method like encodeBase64(byte[]) and then just construct a new String(bytes) yourself. Or you could drop that library and use a different Base64 encoder, for example this one.
Some Google tooling such as GWT has an embedded version of commons-codec with a pre-1.4 Base64 class. You may need to make such tooling JARs inaccessible to your code by refactoring your project such that only the parts of your code that need that tooling can see the dependency.
#Adam Augusta is right, One more thing
Apache-HTTP client jars also comes in same category as some google-apis.
org.apache.httpcomponents.httpclient_4.2.jar
and commons-codec-1.4.jar both on classpath, This is very possible that you will get this problem.
This prove to all jars which are using early version of common-codec internally and at the same time someone using common-codec explicitly on classpath too.
Download this jar
It resolved my problem, this is 1.7.
I faced the same problem with JBoss 4.2.3 GA when deploying my web application. I solved the issue by copying my commons-codec 1.6 jar into C:\jboss-4.2.3.GA\server\default\lib
You need the Apache Commons Codec library 1.4 or above in your classpath.
This library contains Base64 implementation.
Try add 'commons-codec-1.8.jar' into your JRE folder!
Simply create an object of Base64 and use it to encode or decode, when using org.apache.commons.codec.binary.Base64 library
To Encode
Base64 ed=new Base64();
String encoded=new String(ed.encode("Hello".getBytes()));
Replace "Hello" with the text to be encoded in String Format.
To Decode
Base64 ed=new Base64();
String decoded=new String(ed.decode(encoded.getBytes()));
Here encoded is the String variable to be decoded

Categories

Resources