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.
Related
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.
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.
I have a jar file without its main class specified in manifest.
So i followed an answer given here:
How to run a class from Jar which is not the Main-Class in its Manifest file
It seems to try to run main from this class. However it looks like importing some other class from this jar file is broken for some reason.
Here is the minimized version of my problem:
jar tf test.jar
gives:
META-INF/
META-INF/MANIFEST.MF
ClassIWantToRun.class
something/
something/something/
something/something/something/ClassA.class
Sources of ClassIWantToRun.class viewed with jd-gui seems to be:
import something.something.something.ClassA;
public class ClassIWantToRun
{
public static void main(String[] args)
{
int x = ClassA.comeMethod();
}
}
Running this with:
java -cp test.jar ClassIWantToRun
gives me the exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/OS4690/FlexosException
at ClassIWantToRun.main(ClassIWantToRun.java:7)
Caused by: java.lang.ClassNotFoundException: com.ibm.OS4690.FlexosException
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:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 1 more
I know only basics of Java but it seems that ClassA can not be found even with the line: import something.something.something.ClassA
How can i make this run?
The exception indicates that you need to add some other JARs into the classpath. Classes in your test.jar depend on other classes. e.g. on com.ibm.OS4690.FlexosException.
You can try searching for another JAR file (in the same place you took your test.jar) so that it will contain the FlexosException.class file. Once you find it, run your test.jar as
java -cp test.jar;<path_to_another_jar_here> ClassIWantToRun
You won't be able to run your program outside an OS4690 environment because you're depending on internal OS4690 libraries. You might find the jar you need if you have access to an OS4690 installation, but at the end those jars use platform dependant libraries.
Try to avoid using those dependencies if you're not developing for that specific platform.
java -cp test.jar ClassIWantToRun
Is importing the JAR containing the class you want to run. You should as well import the JAR that includes ClassA on your classpath.
In your case, I guess it is the JAR that contains com/ibm/OS4690/FlexosException that needs to be on your classpath
So I'm pretty sure I looked as well as I could before asking this question, so here goes.
I am writing a Java class for RC4 encryption, I'd like to make use of the Files class in Google Guava. I am compiling using javac on the Ubuntu command line. I am trying to link the jar file, guava-10.0.1.jar" to my class to no success.
This is a snippet from the top:
import java.util.*;
import java.io.*;
import com.google.common.io.*;
class rc4 {
private static int S[] = new int[256];
private static int kLen;
public static void main(String[] args) {
...
The line specific to what I am calling is as follows, fully in a try block:
byte plain[] = Files.toByteArray(file);
I then compile using this:
$ javac -cp FULL/FILE/PATH/guava-10.0.1.jar rc4.java
It compiles, but when I try to run java rc4 with the options I defined to access the function, I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/Files
at rc4.main(rc4.java:58)
Caused by: java.lang.ClassNotFoundException: com.google.common.io.Files
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)
... 1 more
I've tried a few different calls, and have checked the jar to make sure its not a jar, in a jar, in a jar etc. I then tried using a manifest file:
Manifest-Version: 1.0
Created-By: me
Main-Class: rc4
Class-path: /FULL/FILE/PATH/guava-10.0.1.jar
And then create a jar as such:
jar cvf0m rc4.jar manifest.txt rc4.class
and then run it with:
java -jar rc4.jar
to which I get the same error as above, but it's saying "rc4" can not be found instead.
So, I'm a little lost to what I'm missing. I am still a lil fresh to Java so if I'm missing some huge obviously basic necessity I sincerely apologize for the oversight, but if anyone can offer me some help in this, I would really appreciate it.
Compiling does not package referenced libraries into your jar. You must either extract the libraries into your jar file or pass in the guava classpath at runtime.
To test that your code actually works you can try manually specifying the classpath:
java -cp yourProject.jar:/path/to/guava-10.0.1.jar rc4
Note that you need to specify both project and lib on the classpath like that or rc4 which is from your project JAR will not be found! (As an aside: consider using packages in your project.)
Then if that works it is time to check your JAR manifest:
Is it located in a directory inside the JAR file called META-INF
Is it called MANIFEST.MF
Does the Class-Path contain your classpath, with proper case and so on? Note your attribute is called Class-path instead.)