Java json parsing exception - java

I'm trying to run this example I found on the internet. When it gets to the following line it terminates with the following error. Error and sample-json.txt pasted below example code. Am I missing something obvious?
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
Imports:
package com.discursive.answers;
import java.io.InputStream;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
public class JsonParsing {
public static void main(String[] args) throws Exception {
InputStream is = JsonParsing.class.getResourceAsStream( "sample-json.txt");
String jsonTxt = IOUtils.toString( is );
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
double coolness = json.getDouble( "coolness" );
int altitude = json.getInt( "altitude" );
JSONObject pilot = json.getJSONObject("pilot");
String firstName = pilot.getString("firstName");
String lastName = pilot.getString("lastName");
System.out.println( "Coolness: " + coolness );
System.out.println( "Altitude: " + altitude );
System.out.println( "Pilot: " + lastName );
}
}
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
Disconnected from the target VM, address: '127.0.0.1:59138', transport: 'socket'
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
st java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at com.discursive.answers.JsonParsing.main(JsonParsing.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
Here is the sample-json.txt file that is opened.
{'foo':'bar',
'coolness':2.0,
'altitude':39000,
'pilot':{'firstName':'Buzz',
'lastName':'Aldrin'},
'mission':'apollo 11'}

Yep, it's definitely a classpath issue. You can compile your project without third-party libraries existing, but you cannot run it without those libraries on your classpath.
I have copied your code to my local system and verified that I receive the same (initial) Exception that you get. After grabbing the commons-lang-2.6.jar file and putting it on my classpath, I'm now receiving the following exception:
$ java com.so.Q8187623
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at net.sf.json.AbstractJSON.<clinit>(AbstractJSON.java:53)
at net.sf.json.util.CycleDetectionStrategy.<clinit>(CycleDetectionStrategy.java:36)
at net.sf.json.JsonConfig.<clinit>(JsonConfig.java:65)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
at com.so.Q8187623.main(Q8187623.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 5 more
So just follow your way down the rabbit-hole adding classes to your classpath...

The immediate error looks like it's because you're missing commons-lang.jar from your classpath. However, given that the class which can't be found is an exception, I strongly suspect that when you've fixed that error, you'll immediately get another one... in the form of a NestedRuntimeException being thrown.

I bumped into the same issue. I was using commons-collections 3.2.2 with commons-lang 2.6.
The issue got resolved after downgrading commons-collection to 3.2.1.

Related

Solr Insertion Using java

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import java.io.IOException;
public class FeedIntoSolr{
public static void main(String[] args) throws IOException, SolrServerException {
HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr");
for(int i=0;i<1000;++i) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("cat", "book");
doc.addField("id", "book-" + i);
doc.addField("name", "The Legend of the Hobbit part " + i);
server.add(doc);
if(i%100==0)
server.commit(); // periodically flush
}
server.commit();
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
at FeedIntoSolr.main(FeedIntoSolr.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpRequestBase
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
You are missing httpclient's jar in your classpath.
Ensure you have this jar in your classpath and you should get rid of this error.
You need to add http client implementation
You can update your pom.xml with maven
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
Or download it at https://hc.apache.org/downloads.cgi and add it to the classpath in Eclipse with Project > Properties > Build Path
Thanks to all for responding..I got the answer of question asked by me.
1)first of all you need to add the jar files which is already available in solr folder(you will see when you unzipped it)
2)Jar Files in the folder solr-5.2.1->dist( add only solr-solrj-5.2.1.jar in classpath) and solr->dist->solrj-lib(add all jar in classpath)
3)you have to add two another file commons-logging-1.2.jar and slf4j-simple-1.7.12.jar
This is working for me..

Error using apache -common-validator --java.lang.NoClassDefFoundError: org/apache/commons/digester/Rule

I am using following jars-
commons-validator-1.4.0 ; commons.logging-1.2 ; commons.digester-1.8 ;commons.beanutils-1.8.3
commons.collections-3.2.1
but keeps on getting same error at line -
Caused by: java.lang.ClassNotFoundException: org.apache.commons.digester.Rule
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:216)
This is the code i am writing -
InputStream in = this.getClass().getResourceAsStream("validator-name-required.xml");
// Create an instance of ValidatorResources to initialize from an xml file.
ValidatorResources resources = new ValidatorResources(in);
Please help its been a day i am stuck into this error !! Thanks in advance
Finally i am able to do this. I have used the following jars -
commons-beanutils-1.8.3.jar
commons-collections-3.2.1.jar
commons-digester-1.8.jar
commons-logging-1.1.1.jar
commons-validator-1.4.0.jar
and this works :)
Its look like that "org.apache.commons.digester.Rule" class was not found, Version 2.1 of digester have Rule.class, download this version and try.

Unable to run java with an external library

I saw this post but I wasn't clear on what the resolution was.
I'm trying to run a java program from command line utilizing an external library. Here is the total output of my session:
grifter#host:~/java$ cat ExtractTest.java
import java.io.IOException;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
public class ExtractTest {
public static void main(String[] args) throws IOException {
System.out.println("About to open PDF");
File fh = new File("/home/grifter/test.pdf");
PDDocument d = PDDocument.load(fh);
System.out.println("Here");
PDFTextStripper ts = new PDFTextStripper();
System.out.println(ts.getText(d));
}
}
grifter#host:~/java$ javac -cp .:/home/grifter/pdfbox-1.8.5/pdfbox/target/pdfbox-1.8.5.jar ExtractTest.java
grifter#host:~/java$ java -classpath .:/home/grifter/pdfbox-1.8.5/pdfbox/target/pdfbox-1.8.5.jar ExtractTest
About to open PDF
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.pdfbox.pdfparser.BaseParser.<clinit>(BaseParser.java:68)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1219)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1187)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1158)
at ExtractTest.main(ExtractTest.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 5 more
Could someone please point me in the right direction?
Any help is appreciated.
pdf box uses dependencies, which also needs to be added to your class path. You can read about the dependecies here
The jar commons-logging and the dependencies is missing in your classpath.
You can download it here

java.lang.NoClassDefFoundError when running with cmd

I have some problems testing a program from a java programming book. I wrote the code in eclipse and succeded running it, but when I compile the .java file via the command line and then run it with java ConsoleEcho, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: ConsoleEcho (wrong na
me: einausgabe/ConsoleEcho)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The code for my program is:
package einausgabe;
import java.io.IOException;
import static java.lang.System.*;
public class ConsoleEcho {
public static void main( String args[] ) throws IOException {
while( true ) {
int code = in.read();
out.write(code);
}
}
}
The commands I used are:
javac ConsoleEcho
java ConsoleEcho
I run this from the directory ..\Java\ProgrammierenMitJava2\src\einausgabe
I don't get any errors by compiling. Tried java einausgabe.ConsoleEcho and then I get this error message:
Error: Could not find or load main class einausgabe.ConsoleEcho
You need to include the package name in the command line, something like:
java einausgabe.ConsoleEcho
you appear to just have the class name in your command.
It seems that you forgot the package name
Use
java einausgabe.ConsoleEcho

Error in creating owl file

I want to create ontology whit this code:
public static void main(String[] args) throws FileNotFoundException {
// TODO code application logic here
OntModel my_model= ModelFactory.createOntologyModel();
ObjectProperty op = my_model.createObjectProperty("b");
OntClass my_class = my_model.createClass("student");
DatatypeProperty dtp = my_model.createDatatypeProperty("name");
dtp.addDomain(my_class);
FileOutputStream univer= new FileOutputStream("c:/uni.owl");
my_model.write(univer,"RDF/XML-ABBREV","ns");
}
But it gives this error:
java.lang.NoClassDefFoundError: com/hp/hpl/jena/rdf/model/Resource
Caused by: java.lang.ClassNotFoundException: com.hp.hpl.jena.rdf.model.Resource
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: javaapplication6.Main. Program will exit.
Exception in thread "main" Java Result: 1
I cannot understand what is the problem.Thanks for any Help.
NoClassDefFoundError means the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
Try to download Apache Jena library from here and put it in your classpath

Categories