I got trouble in using my external jar file even if I put it inside class path correctly.
My external jar file is in the D: drive and my class path is "C:\Program Files\Java \jdk1.7.0_51\bin; D:\webcam-capture-0.3.10-RC6.jar; C:\Program Files\Java\jdk1.7.0_51\lib;" so please help me solve the problem. Below is my code:
import com.github.sarxos.webcam.Webcam;
import javax.swing.JOptionPane;
public class DetectWebcamExample {
public static void main(String[] args) {
try {
Webcam webcam = Webcam.getDefault();
if (webcam != null) {
System.out.println("Webcam: " + webcam.getName());
} else {
System.out.println("No webcam detected");
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
}
the exeption detail is
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:40)
at test.Test.main(Test.java:20)
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
i amend the path with "C:\Program Files\Java\jdk1.7.0_51\bin;" and classpath "D:\webcam-capture-0.3.10-RC6.jar;." but the exception is still there
How can I solve this problem?
Java has found com.github.sarxos.webcam.Webcam so you have successfully added that JAR to the classpath.
What's happening now is that Webcam is trying to use a log4j class, and that's not on the classpath.
This is common - package A needs package B; package B needs package C; and you can spend a while finding JARs to fulfil chains of dependencies.
In this case you're going to need two log4j jars - log4j-api and one of a choice of log4j implementation jars. See the log4j web page for details.
If the hunt for dependency jars starts getting silly, consider using Maven or Ivy to handle your dependencies for you.
Related
I need to use jackson to handle some JSON in my code. I wrote a simple test class to get it working.
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
}
}
I placed jackson-databind-2.9.9.1.jar in /dirwherejarresides/jdk/jre/lib/ext.
I am able to compile the class with no issues, and produce Test.class.
javac Test.java
However, I can't seem to get java find the jar & execute the class.
java -cp "/dirwhereclassresides/java" Test Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonView
at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:37)
at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:291)
at Test.main(Test.java:5)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more
I tried adding the path where the jar resides, but I get the same error.
java -cp "/dirwhereclassresides/java:/dirwherejarresides/jdk/jre/lib/ext/jackson-databind-2.9.9.1.jar" Test
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonView
at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:37)
at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:291)
at Test.main(Test.java:5)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more
What do I need to do to get java to see the jar?
Jackson library is split into several jar-modules that declare dependencies between each other. For example: jackson-databind depends on jackson-annotations and jackson-core.
The jackson-databind-*.jar does not actually contain a class for JsonView annotation that you get the exception about. It is placed in the jackson-annotations-*.jar file. But to be able to parse a JSON with Jackson you will definitely need jackson-core-*.jar as well.
Btw, your second way to pass a class path is more correct, you need to list all jar-files your class depends on in the -cp parameter split by colon on the Unix/Linux systems. E.g.
$ java -cp ".:/pathtojackson/jackson-databind-2.9.9.1.jar:/pathtojackson/jackson-core-2.9.9.jar:/pathtojackson/jackson-annotations-2.9.9.jar" Test
You can also use star-expressions in -cp parameter, see https://stackoverflow.com/a/219801/2288384
It is complaining about annotation/JsonView and seems like it found jackson data-bind. Since you are not using maven, I suspect jackson-core which is a transitive dependency of data-bind, is not injected.
Try adding jackson-core into your classpath.
Often ClassNotFoundException are a side effect of you having the two of the same packages with different versions. Print out a dependency tree and see that there is not two versions of the same dependency loaded.
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 7 years ago.
I have the following code:
import java.io.IOException;
public class DbTest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
I downloaded the Platform Independent .zip version of the MySQL Connector from the following location:
http://dev.mysql.com/downloads/connector/j/
Then, after unzipping the file downloaded, I placed the resulting folder at the following location:
/java/mysql-connector-java-5.1.36
I am compiling my code from within the directory where it resides with the following command:
javac DbTest.java -cp /java/mysql-connector-java-5.1.36/mysql-connector-java-5.1.36-bin.jar
Finally, I am running my code with the following command:
java DbTest
The full stack trace from the event is as follows:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at DbTest.main(DbTest.java:6)
Thanks in advance for any help you can offer!
You need the JAR file on the classpath when you run your application; e.g.
java -cp /longpath/mysql-connector-java-5.1.36-bin.jar:. pkg.pkg.DbTest
(For a database driver, you don't need the JAR at compile time if you load the class using Class.forName(...). However, it does no harm including it on the compile-time classpath anyway.)
I am trying to run a tutorial from here Office Open XML. I have downloaded the Docx4j library and added it to the netbeans as a library.
the code snippet is given below
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class OfficeOpen {
public static void main(String[] args) throws Docx4JException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
wordMLPackage.save(new java.io.File("src/main/files/HelloWord1.docx"));
}
}
when I run the above example I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.docx4j.openpackaging.Base.<clinit>(Base.java:43)
at officeopen.OfficeOpen.main(OfficeOpen.java:24)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
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)
... 2 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
looks like the JVM cannot find a class at run time, any suggestion. I am using Netbeans 8.0
You are missing some libraries that docx4j depends on. See http://htmlpreview.github.io/?https://github.com/plutext/docx4j/blob/master/docs/Docx4j_GettingStarted.html at docx4j dependencies.
Indeed you are missing slf4j that docx4j uses for for logging: http://www.docx4java.org/docx4j/docx4j-3_2_0/dependencies/slf4j-api-1.7.5.jar
Possibly you'll need many others.
I think this are all the dependencies, althought you may not need all of them: http://www.docx4java.org/docx4j/docx4j-3_2_0/dependencies/
I am using jackcess for the conncetivity to my access database. But I am following exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/builder/CompareToBuilder
at com.healthmarketscience.jackcess.impl.RowIdImpl.compareTo(RowIdImpl.java:113)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:1838)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:1646)
at java.util.Collections.indexedBinarySearch(Collections.java:273)
at java.util.Collections.binarySearch(Collections.java:259)
at com.healthmarketscience.jackcess.impl.IndexData$DataPage.findEntry(IndexData.java:2368)
at com.healthmarketscience.jackcess.impl.IndexData.findEntryPosition(IndexData.java:722)
at com.healthmarketscience.jackcess.impl.IndexData.access$3300(IndexData.java:56)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.updatePosition(IndexData.java:2133)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2072)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2055)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.beforeEntry(IndexData.java:2017)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findPotentialRow(IndexCursorImpl.java:368)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntryImpl(IndexCursorImpl.java:262)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntry(IndexCursorImpl.java:135)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$DefaultTableFinder.findRow(DatabaseImpl.java:1890)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$TableFinder.findObjectId(DatabaseImpl.java:1799)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.readSystemCatalog(DatabaseImpl.java:804)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.<init>(DatabaseImpl.java:513)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:386)
at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:170)
at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:193)
at ass.Access.main(Access.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.builder.CompareToBuilder
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 23 more
I have used jdbc but it is not working either. How can solve my problem?
Here's how I got Jackcess working, starting with a fresh install of NetBeans 7.4 on Windows 8:
I downloaded the latest Jackcess JAR file via the "Looking for the latest version?" link on files page. I saved it in the folder
C:\Users\Public\Java\
As listed on the Project Dependencies page for Jackcess, I downloaded the ZIPped binaries for the two required dependencies: commons-lang v2.x, and commons-logging v1.x. I unpacked the ZIP files into the above folder, so it now contained two sub-folders
C:\Users\Public\Java\commons-lang-2.6\
C:\Users\Public\Java\commons-logging-1.1.3\
I launched NetBeans and created a new Project (for a Java Application) named "myJackcessTest". I expanded the Project in the tree view, right-clicked "Libraries", chose "Add JAR/Folder...", and added the three JAR files:
Once that was done, I created my little test app...
package myjackcesstest;
import com.healthmarketscience.jackcess.*;
import java.io.File;
import java.io.IOException;
public class MyJackcessTest {
public static void main(String[] args) {
try {
Table table = DatabaseBuilder.open(new File("C:\\Users\\Public\\Database1.accdb")).getTable("Clients");
System.out.println(String.format("table contains %d row(s)", table.getRowCount()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
...and when I hit F6 it ran fine:
run:
table contains 1 row(s)
BUILD SUCCESSFUL (total time: 0 seconds)
Jackcess has a dependency on Jakarta Commons Lang. You need to make sure that the commons lang and the other dependencies are on your classpath.
I am trying to run a java program and I am getting the following run time error.The error is shown below.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/params/SyncBasicHttpParams
at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:157)
at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448)
at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309)
at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466)
at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at net.floodlightcontroller.core.internal.PacketStreamerClient.registerForPackets(PacketStreamerClient.java:90)
at net.floodlightcontroller.core.internal.PacketStreamerClient.main(PacketStreamerClient.java:51)
Caused by: java.lang.ClassNotFoundException: org.apache.http.params.SyncBasicHttpParams
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
The error is java.lang.NoClassDefFoundError
Now the Obvious reason of NoClassDefFoundError is that a particular class is not available in Classpath, so we need to add that into Classpath or we need to check why it’s not available in Classpath if we are expecting it to be.
Now the files that I have added to the classpath are the following.
export CLASSPATH=$(JARS=(./lib/*.jar); IFS=:; echo "${JARS[*]}")
export CLASSPATH=$CLASSPATH:~/.m2/repository/org/apache/httpcomponents/httpclient/4.0.1/httpclient-4.0.1.jar
export CLASSPATH=$CLASSPATH:~/.m2/repository/org/apache/httpcomponents/httpcore/4.0.1/httpcore-4.0.1.jar
export CLASSPATH=$CLASSPATH:~/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
export CLASSPAHT=$CLASSPATH:~/ms_thesis/ONOS/httpcore-4.1.jar
#export CLASSPATH=$CLASSPATH:~/ms_thesis/ONOS/lib/httpclient-4.2.jar
export CLASSPATH=$CLASSPATH:~/google-gson-2.2.4/gson-2.2.4.jar
What jar file should I add to find "org/apache/http/params/SyncBasicHttpParams" I am very new to java and don't know how to debug this issue.
According to the official documentation, the class that you seek is only in httpcore since 4.1. You'll want to pull a new JAR.
org/apache/http/params/SyncBasicHttpParams class is under httpcore-4.1-alpha1.jar , which can be downloaded from
http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/httpcomponents/httpcore/4.1-alpha1/httpcore-4.1-alpha1.jar
In future if you need to find a jar for a class, then you can use this link, this is generally helpful:
http://www.findjar.com/
In your case it seems to br you missing out some necessory jar files to include in your project
otherwise you any one of your linked is giving an exception mainly because of static method invocation or intialization
Here's the link to get the jar file httpcore-4.1