I have encountered an error that seems pretty common. Tried pretty much ALL the solutions recommended on here and some from other sites. Still no luck. I am using Eclipse Oxygen. I get this error on running. I am using a JAR which I have added to the project's lib folder (hence the import statement - which resolves fine). Any help would be greatly appreciated.
code:
package com.ed;
import microsoft.exchange.webservices.data.core.ExchangeService;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ExchangeService service = new ExchangeService();
}
}
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager
at com.ed.Test.main(Test.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.HttpClientConnectionManager
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Not sure which jar file you are using, maybe something similar to: https://mvnrepository.com/artifact/com.microsoft.ews-java-api/ews-java-api/2.0
If you are, then it has dependencies on other jars that must be included. The are listed in the link above. One of those is is httpclient: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.4.1.
Eclipse will not automatically download dependent jars. Build tools like maven or gradle will do that for you.
This is due to Class not found, you can print all the JARs used by the application by something like:
for (String entry : System.getProperty("java.class.path").split(File.pathSeparator)) {
System.out.println(entry);
}
You must have a JAR which contains the class in question, i.e. httpclient, as hinted here.
Sometimes, you can have conflicting versions as well.
Sometimes, the JAR is not fully downloaded, which can be checked by manually opening the JAR (as .zip), and see its contents.
Related
I want to use the PoissonDistribution from Apache Commons but always get the error a NoClassDefFoundError.
The code:
import org.apache.commons.math3.distribution.PoissonDistribution;
public class Test {
public static void main(String[] args) {
PoissonDistribution p = new PoissonDistribution(5.0);
}
}
The error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/distribution/PoissonDistribution
at Test.main(Test.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.distribution.PoissonDistribution
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
What I've done so far:
The commons-math3-3.6.1.jar file is located in a folder inside my project folder and via Project->Properties->Java Build Path->Libraries I added the JAR file and then in the "Order and Export" tab next to it I selected the jar file.
Thanks for your help!
Ok so by coincidence I've found the solution:
My problem was that I added the JAR to the Modulepath and not the Classpath in the project properties.
In my case, I'd forgot just to add part of Apache library is the name: commons-math3-3.6.1.jar
Make sure your PoissonDistribution class available in Java Classpath.
And i can guess your jar will not in add jar properly.
Add following step to add jar :
Eclipse by right-clicking on the Project
Build Path
Add External JARs.
And give the Jar.
Ok
Firstly, I created an xtext project using default values (with the simple Greetings grammar which comes as an example).
I created then a .java file with some code (that can be seen below) and added it to the validation folder.
I have loaded some jars to the build path as I would do in a normal Java Project. If I run my specific java file as a standalone it will load the libraries and run the code. The java file is quite simple:
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.xtext.example.mydsl.myDsl.Greeting;
public static void main(String[] args){
Greeting hey = null;
try {
test(hey);
} catch (OWLOntologyCreationException e) {
e.printStackTrace();
}
}
public static void test(Greeting imp) throws OWLOntologyCreationException {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
System.out.println("success!");
}
However, if a call the "test" function from the .xtend validator file, it will generate this error:
java.lang.NoClassDefFoundError: org/semanticweb/owlapi/model/OWLOntologyCreationException
at org.xtext.example.mydsl.validation.MyDslValidator.checkGreetingStartsWithCapital(MyDslValidator.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
/* (has some more "at" in the middle) */
Caused by: java.lang.ClassNotFoundException: org.semanticweb.owlapi.model.OWLOntologyCreationException cannot be found by org.xtext.example.mydsl_1.0.0.qualifier
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:461)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:372)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:364)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:161)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 32 more
When it mentioned "cannot be found by org.xtext.example.mydsl_1.0.0.qualifier" I assumed the problem was in the manifest, but I've tried adding the libraries to the manifest in every way possible but to no avail.
if you add libs to the buildpath
copy it to a new lib folder inside the plugin
adapt the manifest and add the jars as entries (runtime tab, classpath section)
add the jars to be included in build.properties (there should be a warning and a quickfix)
Im a begginer in java and im using eclipse (if it is relevant).
I recently tried to work with an external Jar (Std), and i keep having the following error in each attempt to run my program no matter what i do.
The error:
`Exception in thread "main" java.lang.NoClassDefFoundError: StdOut
at LocalMaxFinder.main(LocalMaxFinder.java:11)
Caused by: java.lang.ClassNotFoundException: StdOut
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
`
The most minimal code that i tried to run that show me that error was:
import libraries.*;
public class Test {
public static void main (String[]args) {
StdOut.print("Enter Integer");
int x= StdIn.readInt ();
}
}
"libraries" is the name of the Jar file that includes StdIn, StdOut, StdDraw and StdAudio
Before i turned to here i viewed recent questions about that issue and tried every tutorial i found about adding external jars, but im still receiving that same error.
I also tried every method in that guide:
http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
I uploaded an image of the project's current path.
As i said before, Im new to java so i hope i gave you all the necessary details for you to help me with solving that problem.
Thanks alot for any kind of help.
The current project's path
This is because you are not running the project you are currently looking at.
According g to your stack trace the problem originates in a method max() which is not present in your current project
Right click on the file test.java and select run as java application.
Your run menu most likely Hassan old entry of a previous project.
The error is complaining about the class StdOut that is referenced in LocalMaxFinder, in the main() method on line 11 >.>
I don't see the source code for the actual library classes, but my guess is the package name is incorrect. Java (by convention) uses a naming scheme that mirrors the directory structure of the class files, not the name of the actual jar file that contains them. That part is quite irrelevant, assuming the jar file is on the classpath.
To understand what I am describing in detail it is probably best to download some open-source code, for example from Apache, and look at the folder structure and the line at the top of the source code in the .java file that declares the package. If it is 'package com.foo.bar.baz' for example, it would be in the source folder, in com/foo/bar/baz. If the package name is incorrect it will give you this misleading error that the class can not be found.
Do you have the source code for those classes?
If not, there is a command line utility included in the jdk called javap that should be able to help.
Hey there i am developing java application.
I completed coding part but i am recieving exception in thread main java.lang.noclassdeffounderror when i ran it. I have searched this through the internet, and could not find suitable answer.
Let me explain what i have done before exporting java program to the executable jar file from eclipse:
I added some jar files to my library so my program could connect a 3rd party program. It runs from a custom Jre which is named SSC and
got location in 3rd party program's(SunSystems) location folder.
I wrote the required java code to run the program.
Then i ran the project, which works perfectly when i run from eclipse.
I clicked on project's name then clicked Export. I selected Runnable jar file and Copy required libraries into sub-folder next
to generated jar file. I picked correct class from launch
configuration. Then clicked finish.
When i ran the jar file from cmd by typing java -jar ssc.jar It gives me this error:
exception in thread main java.lang.noclassdeffounderror
and some other lines with tag.
What should i do to fix this?
Some documentations say that i need add classpath to program, or edit manifest file etc. I could not figure out how.
I would appreciate your help. Thanks
(Edit)SSC.java class under the demo package. It works when i run from the eclipse:
package demo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import com.systemsunion.ssc.client.*;
public class SSC {
static String HOST="localhost";
static int Port=8080;
public static void main(String[] args) {
try
{
SecurityProvider secman= new SecurityProvider(HOST,true);
String voucher= secman.Authenticate("PKP","").toString();
String sInputPayload="";
String path="C:/SSC temp/temp.txt";
BufferedReader reader= new BufferedReader(new FileReader(path));
BufferedWriter writer= new BufferedWriter(new FileWriter(new File("C:/SSC temp/temp-result.txt")));
String line="";
while((line = reader.readLine()) != null)
{
sInputPayload = sInputPayload + line;
}
try
{
SoapComponent ssc= new SoapComponent(HOST, Port);
ssc.authenticate(voucher);
String result= ssc.execute("Journal", "Import", sInputPayload);
writer.write(result);
writer.newLine();
writer.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Error when i ran the jar file from cmd:
Exception in thread "main" java.lang.NoClassDefFoundError: com/systemsunion/ssc/
client/SoapComponent
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.systemsunion.ssc.client.SoapCom
ponent
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
You need to tell what your main class is before you run the jar. For easy cases you can do it like this:
jar -uvfe ssc.jar your.main.class.Name
Note that you must specify the full name of your main class (the one that contains the main method).
This will add a standard manifest to your jar and you can henceforth run it.
Another alternative would be to run it thus:
java -cp ssc.jar your.main.class.Name
Edit:
The problem seems to be that you a) have no clue what the name of your main class is b) your development environment is lacking.
For a)
Look up the definition of your main class, this is the class that has:
public static void main(String[] args) { ... }
Note the class name (maybe SSC from your comments). Browse to the beginning of the file and look for the package statement. Prepend the package name to youir classname like
package org.brave.programmers
public class SSC { ... }
Here the class name would be org.brave.programmers.SSC.
If there is no package, it is just SSC
For b) Make sure you have java.exe and jar.exe in your path. If you don't have jar.exe, something is wrong with your installation.
Please do a
jar -tvf ssc.jar
to see what is in your jar file.
The problem is with "Copy required libraries into a sub-folder next to the generated JAR". When you do this, the libraries are copied to a sub-folder in the same folder as the created jar file. When you attempt to run the jar file, you must add all the jars this sub-folder to the classpath (I'm not sure if wildcards work - I think they will with Java 7):
java -cp sub\jar1.jar;sub\jar2.jar -jar ssc.jar
To avoid this issue (having to add all the jar files is cumbersome), you can use either of these:
Extract required libraries into generated JAR
Package required libraries into generated JAR
The advantage is: your application JAR will now travel with all dependencies built-in so it will always work. The disadvantage is: a JAR bloated with the dependencies inside (potentially resulting in a very large JAR file, out of which maybe only a small part is your actual application).
Edit: You may also need to add a reference to your project in the command line above. See comments on this answer for more details.
Use a META-INF/MANIFEST.MF with a Class-Path: lib/... .jar.
It might be a moment to consider using a maven plugin. Maven provides a build infrastructure for miscellaneous things. It also heavily uses conventions; other default paths unfortunately, src/main/java, but it's worth it. So with maven first start a new project. But then library version dependencies, packaging with libraries and such come free.
I have: ubuntu-9.10-desktop-amd64 + NetBeans6.7.1
I just downloaded "Commons HttpClient 3.1 (legacy)" from http://hc.apache.org/downloads.cgi .
I created new application with IDE NetBeans.
I focused on Projects -> Libraries -> Add JAR/Folder and here added commons-httpclient-3.1.jar
By now, I can write "import org.apache.commons.httpclient.*;" This is OK.
But, if I try to add any statement included something like this:
public static void main(String[] args) {
HttpClient client = new HttpClient();
}
It is compiled well.
But when I run this project, I receive the following errors:
***Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66)
at SimplestAppl.Main.main(Main.java:22)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
... 2 more
Java Result: 1***
What is wrong?
I tried to download "HttpClient 4.0 (GA)". But the result was same.
What you are experiencing is just a missing dependency. Java developers are used to gather their dependencies themselves, esp. when it comes to logging frameworks.
Download commons-logging and add it to your classpath. Do as well with all other dependencies required by httpclient.
You need the dependencies. If you used maven, they would fetch themselves. Since you aren't, you've got to go download commons-logging (and perhaps others) and toss them in.
I'd like to update anyone coming here to the fact that all answers, and especially RocketSurgeon's, helped me solve my issue in the same sense. I had to download commons httpclient (legacy 3.1), logging and codec before my issues were resolved.
Also of note; using Eclipse I had to extract the .jar-files and import them into Buildpath using "add library". Did not work with only adding .zip-file.
If you dont want to use Maven, then
- build and launch, look at missing dependencies
- identify vendor, fetch the jar to resolve dependencies
- rinse/repeat until you have all dependencies satisfied
- done
anyway you will be never sure if all dependencies are 100% supplied. Its usually around 150% or 99%, but never in between