How to use org.apache.comons.io in eclipse - java

Exclaimer: I am pretty much a complete noob when it comes to eclipse and java, but I am learning! :D Thanks for your help
I am using the apache commons io to create an external server log that is saved in a file along with showing in the server console itself. I added the right file to the external jar library and it all works inside eclipse, but when I export it and try to run it on it's own, I get these errors
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/output/TeeOutputStream
at com.josh.chat.server.ServerMain.<init>(ServerMain.java:10)
at com.josh.chat.server.ServerMain.main(ServerMain.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.output.TeeOutputStream
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
What is going on? How do I fix this, or at least is there another way for me to use the console AND save a log of that console to a text file?
EDIT: I didn't change anything (at least I don't think I did...) and tried to run it again and then got this different error, what did I do?
no main manifest attribute, in InstantChatServer.jar

Probably your issue might have been fixed by now but on broader scope if you need further dependencies/libraries to be there then you will have to edit the class path and also the client(the one who is doing the java -jar ... stuff) needs to have those dependencies be present in his box.What if there are hundred such dependencies. Ideally you should prefer a build tool like maven,gradle etc. and bundle the jars/dependencies with your code. So that the client need not have to bother about having the libraries in local box.

Related

IntelliJ Jar Error : Invalid signature file digest for Manifest main attributes

I'm using IntelliJ IDE on an armx64 linux-based system, where i'm working on a non-maven java project that have a lot of modules (dependencies) linked to it. When running my project from the IDE everything works fine, however when building it into a runnable jar file and trying to run the jar from the terminal, using java -jar myjar.jar, i got the following error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:330)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:263)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:318)
at java.util.jar.JarVerifier.update(JarVerifier.java:230)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
at java.util.jar.JarFile.getInputStream(JarFile.java:450)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:977)
at sun.misc.Resource.cachedInputStream(Resource.java:77)
at sun.misc.Resource.getByteBuffer(Resource.java:160)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
I'm currently using OpenJdk-1.8, i tried to use Oracle JDK v1.8 instead to build and then rerun the jar file, but still facing the same error. I'd also tried to create a new simple "Hello world" application and export a corresponding jar for it where it runs successfully without errors.
Here is my jar internal structure and the corresponding manifist file:
jar structure screenshot
Thanks for #y.bedrov who linked to this issue that helped my solving the error, where it includes the following:
"sqljdbc4.jar" was the signed JAR in OP's external libraries. So,
following above approach to systematically exclude the signature
related files like .SF, .RSA or .DES or other algorithms files is the
right way to move forward.
And yes I'm also using "sqljdbc4.jar", so after reading the answer i decided to solve the problem by deleting the signature files from my Meta-inf folder inside my jar file.

ClassNotFoundException in IntelliJ IDEA

I have received an IntelliJ project from someone else, but it won't run even the simplest classes for me.
As a quick example, the following class will throw a ClassNotFoundException without compiler errors:
package myPackage;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Soon our GUI will start from here!");
System.exit(0);
}
}
This will throw the following error:
Exception in thread "main" java.lang.ClassNotFoundException: myPackage.Main
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Process finished with exit code 1
The file structure is as follows:
<git root>/src/myPackage/Main.java
I have set the 'src' folder as the 'source folder'. My application has no compiler errors.
I have invalidated the caches several times, but with no effect whatsoever.
In my module settings my module dependencies contain <Module source> and jdk 1.8.
I can't seem to find the answer around here (google) anywhere, and to me it seems like this error has to do with the way IntelliJ handles the project. When I first pulled the project, the src folder had NOT been set to be the source folder, which probably means I may be missing out on other settings as well.
Zack Newsham's suggestion helped me. Here is what I did:
Deleted .iml file
File > New > Project, specified the existing folder with code
completed project setup (Manifest, artifacts).
Now the project runs.
Of course there may be other reasons for ClassNotFound. However when everything seems ok or if the error comes up after it worked before, try these simple steps.

java.lang.NoClassDefFoundError when using a third party .jar file

I'm using NetBeans. I have a simple project, which prints in a fancy format some data sent as arguments.
This is the code that throws an Exception:
JasperDesign design = JRXmlLoader.load("Certificate.jrxml");
JasperReport report = JasperCompileManager.compileReport(design);
JasperPrint print = JasperFillManager.fillReport(report, new HashMap(), new ClientList(args).getClients());
JPanel panel= new JRViewer(print);
These Exceptions are thrown whenever a Jasper class is created.
I also tried System.getProperty("java.class.path") and it includes the "jasperreports-5.0.1.jar" file..
What am I doing wrong? I know these Exceptions are thrown whenever a class is available at compile time, but not at runtime.. But the jar is there!
Requested Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
at certificado.Certificado.main(Certificado.java:31)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.digester.Digester
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
Java Result: 1
You may be compiling against the Jasper jar ok, but at runtime that jar has its own dependencies, and you should include those too.
According to your stacktrace you need Apache Commons Digester. I would perhaps build using Maven, and let Maven (hopefully) resolve the set of dependencies for you. You can use a Jar search engine to find these dependencies, but they won't determine matching versions of those jars for you.
Your library (Jasper?) seems to have a (run-time) dependency on another library.
Meaning, it requires some other JAR to be present on classpath when being executed.
The missing classname is org/apache/commons/digester/Digester. Thus you can guess the missing library is commons-digester. Make sure the Digester library is available on Classpath too.
I face same problem, I just add commons-digester-2.0.jar and problem get solved.

ClassDefNotFoundError while class in classpath

I'm trying to run ParSeMiS. According to the documentation, it requires ant, prefuse and antlr jars to be available in its lib directory. I've put all the required jars in it. However, when I try to run it, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: antlr/TokenStreamException
at de.parsemis.miner.environment.Settings.parseFileName(Settings.java:198)
at de.parsemis.miner.environment.Settings.parseOption(Settings.java:312)
at de.parsemis.miner.environment.Settings.parse(Settings.java:170)
at de.parsemis.miner.environment.Settings.parse(Settings.java:122)
at de.parsemis.Miner.run(Miner.java:358)
at de.parsemis.Miner.main(Miner.java:61)
Caused by: java.lang.ClassNotFoundException: antlr.TokenStreamException
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)
... 6 more
Now, I've verified that antlr/TokenStreamException.class is present in the antlr jar. I have tried adding the jar manually to the classpath by both exporting the CLASSPATH variable and setting it via the -cp switch. However, none of that works, and I still get this exception. Can anybody help me figure out what's wrong? Thanks.
The problem is that you're using -jar which ignores your CLASSPATH environment variable. You should list your dependencies in the manifest, as shown here, e.g.
Class-Path: lib/ant.jar lib/antlr-3.4-complete.jar lib/prefuse.jar
(It should have still worked with an explicit -cp option, however. My guess is that you got something wrong when specifying that, and assumed it was the same underlying cause as the failure when using the environment variable.)
I realize this question is very old, but I just had exactly the same problem and found this thread. For posterity, I'm posting how I did eventually get it to run:
As mentioned above, when running with -jar, java apparently ignores the class path. So don't run it with -jar. instead include the jars in the path and run the class directly. Poking around, the following should work (paths are on my Ubuntu 12.10 system):
java -cp /usr/share/java/antlr.jar:/full/path/to/parsemis.jar de.parsemis.Miner
You can then pass in options to the above. Maye sure you use full paths, and no shortcuts like ~/foo, as they apparently don't expand.
Of course, if you're using a Dot-formatted graph like I am, it dies very early on complaining of "unexpected char 0xA", but at least it gets further.

Class in buildpath-jar still not found

I'm developing a eclipse plugin rcp and I'm running into a NoClassDefFoundError
Exception in thread "Thread-7" java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
at org.geonames.WebService.search(WebService.java:783)
at geo.GeocoderGeonames$SearchThread.run(GeocoderGeonames.java:119)
Caused by: java.lang.ClassNotFoundException: org.jdom.input.SAXBuilder
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClassInternal(BundleLoader.java:483)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:399)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:387)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:87)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 2 more
The class that supposedly cannot be found is in a jar that I have added to the buildpath. I don't get any compile error etc only this exception when the running application enters the code where this class is needed...
Is there some other place I need to add the jar
After reading this added the jar to the MANIFEST.MF, which solved the problem.
As I understand it, eclipse starts several classloaders which only sees what MANIFEST.MF tells them to see and ingnores the build path...
How are you running your plug-in? You may need to add the JAR to the class path in the VM arguments.
In our experience a NoClassDefFoundError can sometimes mean that more than one version of a Class are found, as there is also a ClassNotFoundException that's normally thrown if a class cannot be found.
Another reason in your case (XML parser) might be something with endorsed classes. Are you directly importing the jdom classes or something like org.w3c...? If so, have a look at the "endorsed classes" system of Java, something that I just recently came across.

Categories

Resources