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
Related
I'm trying to detect my webcam using com.github.sarxos library. This is the download link to the library.
import com.github.sarxos.webcam.Webcam;
public class DetectWebcamExample {
public static void main(String[] args) {
Webcam webcam = Webcam.getDefault();
if (webcam != null) {
System.out.println("Webcam: " + webcam.getName());
} else {
System.out.println("No webcam detected");
}
}
}
But every time I run the program it throws an Exception like this.
Exception in thread "main" java.lang.NoClassDefFoundError:org/slf4j/LoggerFactory
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:97)
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:97)
at webcam.WebCam.main(WebCam.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 2 more
Java Result: 1
Can you please tell me what is the wrong with the Code or Library?
I have added the Library (Which I downloaded in the link given above as a .zip file) to the Library folder in Netbeans. Are there any libraries that I should Add???
You didn't add the third-party dependencies of the library, in the dl link there should be libs file containing (slf4j-api-1.7.2.jar, bridj-0.6.2.jar), add those .jars into your project and it should work. The exception is simply saying it cannot find the classes, that is needed to run the library as they are contained different .jar file
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..
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
I'm trying to use Lucene StandardTokenizer as in the code below:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.util.Version;
public class App {
public static void main(String[] args) throws Exception {
// line 28 goes below
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
// the rest is irrelevant
}
}
It is throwing VerifyError:
Exception in thread "main" java.lang.VerifyError: class org.apache.lucene.analysis.standard.StandardTokenizer overrides final method setReader.(Ljava/io/Reader;)V
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at lt.App.main(App.java:28)
Any ideas how to fix this? Thanks a lot.
Do you have 2 different versions of lucene in your classpath?
The method setReader(...) was added to StandardTokenizer in the 4.0.0-BETA version. But it was later removed from StandardTokenizer and made final in Tokenizer before the 4.0.0 release version. So I you may have the 4.0.0-BETA version of the lucene-analyzers-common jar in your classpath and the 4.0.0 release version of lucene-core jar.
LUCENE-4343 is probably the issues that changed this.
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.