Error in adding external jars - java

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.

Related

Java Exception in thread

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.

In java runtime, class not found exception

I am building the jar and I'm using this jar in one the my .war. When I run the program I am getting the below exception. But in that jar file, that particular class is there.
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.itc.zeas.custominputformat.CustomTextInputFormat not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195)
at org.apache.hadoop.mapreduce.task.JobContextImpl.getInputFormatClass(JobContextImpl.java:174)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:749)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at
A quick search turned this up.
Your classpath is broken (which is a very common problem in the Java world).
Depending on how you start your application, you need to revise the argument to -cp, your Class-Path entry in MANIFEST.MF or your disk layout.
Maybe you should post more information? Which tools are you using to develop the program, which parameters when compiling, etc..

Executable jar file error

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.

Issues linking external jar file, despite many trials

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.)

When executing JAR, get ExceptionInInitializerError: version.properties not found

I've been writing a small project in Eclipse which runs perfectly within the IDE. Then I've build a runnable .jar file through Eclipse (which should include every dependency library inside the jar itself).
I use 3 library in my project:
derby.jar
qtjambi-4.7.1.jar
qtjambi-win32-msvc2008-4.7.1.jar
Then I use this command (in windows):
java -jar prova.jar
And I get this:
Connected to database
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.ExceptionInInitializerError: version.properties not found!
at com.trolltech.qt.Utilities.<clinit>(Unknown Source)
at com.trolltech.qt.QtJambi_LibraryInitializer.<clinit>(Unknown Source)
at com.trolltech.qt.QtJambiObject.<clinit>(Unknown Source)
at WAAAGH.main(WAAAGH.java:52)
... 5 more
As you can see the derby.jar is working as expected ("Connected to database"), but there's an error with Qt-Jambi that I can't understand. Any idea?
EDIT: WAAAGH is the class containing the main method, line 52 consists in:
QApplication.initialize(args);
How is QtJambiObject getting loaded? Have you packeged it inside your prova.jar? The missing file version.properties should be part of the same jar at top level (not in any subdir). It seems you have not packaged it inside prova.jar at top level. See this for explanation of how it is loaded.
You might be better off specifying all jars and main class on command line:
java -classpath prova.jar;derby.jar;qtjambi-4.7.1.jar;qtjambi-win32-msvc2008-4.7.1.jar <your main class>
replace ; with : if you are running on *nix
FWIW the location of version.properties has recently been changed to be inside the package namespace of the bundles com/trolltech/qt/version.properties. The old location was a poor design choice and that has now been corrected. The issue is that if you have another JAR in your classpath that also has a toplevel file then the ClassLoader is entitled to think that the JAR with that file is authorative for the package and it does not need to search another JAR for the file. A package is a minimum deployable unit in Java, only specialist ClassLoaders (such as those use in OSGi) have features to work around this part of the Java design.
Usually your toplevel (application JAR) will be first in the list and I bet in that JAR you have one or more files like /log4j.properties /commons-logging.properties etc... it is because one or more file exists it then masks (hides) the file in the qtjambi-X.Y.Z.jar from being seen at runtime. Which is why the problem might not exist when you test a certain scenario but then appear when you try another (when you changed ClassPath in some way).
My commit to the project at http://qt.gitorious.org/qt-jambi/qtjambi-community/commit/f18ce5da3e30b43424bf94e49adf8c4cac0d9862 better explains in code the very recent change to make life better.
It should never have been the case that you have to copy the version.properties file from the QtJambi redistributable JARs into some other part of the Class Path (like the toplevel project prova.jar in your case) this is a bug that has been corrected for the next release. It is the long term intention to remove the need for the file completely and I am 80% there with that goal, as part of that work making multiple native JARs co-exist in the same Class Path will greatly simplify deployment and getting started guides; as well as making them play with OSGi and Eclipse nicely out-the-box.
However no releases have been made yet to include this change but I am very close (within 30 days of doing so for Qt 4.7.4).
Open Source plug alert: Please consider joining the mailing list at http://lists.qt.nokia.com/pipermail/qt-jambi-interest/ from http://lists.qt.nokia.com/mailman/listinfo for announcements.

Categories