I am currently trying to make a Jar with all my libraries included. What I have done, I have created folders like this :
eancopy (which contain all my classes)
lib (containing all my libraries : mongo, jedis...)
META-INF/MANIFEST.MF
My main class is named process (within the package eancopy)
My manifest is like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Class-Path: lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar
Created-By: 1.6.0_24-b07 (Sun Microsystems Inc.)
Main-Class: eancopy.process
I have generated the JAR with this command :
jar cvmf META-INF/MANIFEST.MF EANcopy.jar eancopy/*.class lib/*.jar
My problem : When executing the JAR by java -jar EANcopy.jar, it works when it is executed in the place where I have generated the JAR but in another place I have the message :
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject
Caused by: java.lang.ClassNotFoundException: com.mongodb.DBObject
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)
Could not find the main class: eancopy.process. Program will exit.
Any ideas ? Thanks
This exception shows you the cause of the problem:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject
You haven't specified these libs lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar in your CLASSPATH environment variable, hence why it fails when run in other places.
Related
I created a executable jar of my Java project I tested the jar and it is working as expected in windows but when i try run the same jar in Unix..
java jar my.jar
..it is throwing me the error below:
Exception in thread "main" java.lang.NoClassDefFoundError: jar
Caused by: java.lang.ClassNotFoundException: jar
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)
How to overcome the above error?
It should be
java -jar my.jar
not the
java jar my.jar
See
java - the Java application launcher
In a runnable jar file , there will be a MANIFEST.MF inside the META-INF folder. Inside MANIFEST.MF there should be an entry Main-Class: com.myapp.MyMainClass . This will be created automatically if you using the jar command to make a runnable jar file.To be sure,unjar the jar and see if this entry is present.
Im trying to execute a .jar file i have like 3 days trying to run it and nothing i tried right clink and run with java and nothing happens, i tried like a million ways in the console but nothing, the programs runs fine on NetBeans but when i compile i get nothing i checked the manifest and its ok and when i run with the console i get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: dist/ModuloControlAcceso
Caused by: java.lang.ClassNotFoundException: dist.ModuloControlAcceso
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)
Could not find the main class: dist.ModuloControlAcceso. Program will exit.
The path of my project is /home/user/NetBeansProjects/ModuloControlAcceso/dist
The manifest is:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.)
Class-Path: /home/victor/NetBeansProjects/ModuloControlAcceso/dist/lib/mysql-connector-java-5.1.18-bin.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: modulocontrolacceso.ModuloControlAcceso
Im sorry im a noob in java this is my first program and my english isn't good either so i would apreciatte a full explanation to solve my problem.
if you are using eclipse then first clean all files using following steps
project->clean->select clean all -> and then click on
after that right click on your project
Export->click on Runnable jar->click next next and then finish
Problem will be solve.
I'm trying to create a simple Java Agent program before I implement the real thing. I cannot get it to run. Clearly I have some sort of configuration or class path problem. No amount of looking and trying has lead to me the problem.
If I run:
java -cp ./demoAgent.jar -javaagent:./demoAgent.jar com.kingtigerbooks.demoMod.Main
I get the following error:
Exception in thread "main" java.lang.ClassNotFoundException: com.kingtigerbooks.demoAgent.Agent
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)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:280)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:338)
FATAL ERROR in native method: processing of -javaagent failed
Abort trap
I'm running this on my Mac. The jar file with the agent is in the current directory. The manifest for the jar file looks like this:
Manifest-Version: 1.0
Premain-class: com.kingtigerbooks.demoAgent.Agent
Can-Redefine-Classes: true
If smells like a class path problem, but as you can see I have included the jar on the class path. Any help would be appreciated. Its a very simple project.
Problem solved. I had a bad jar file. The agent jar file did not contain the agent class. By building the correct jar file and putting the fully qualified path to the jar on in the javaagent parameter it all works.
when you run an agent, you do not add the agent jar to the normal classpath.
as a side note, you can add -verbose:class to the command line to get info about the class loading process.
I am trying to run a jar file I made, I keep getting this error:
java -jar Client.jar
Exception in thread "main" java.lang.NoClassDefFoundError: Client (wrong name: Client/Client)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
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)
I used this command
jar cmf Client.mf Client.jar Client.class Client.java Client
where Client.mf is:
Manifest-Version: 1.0
Main-Class: Client
The last argument is a folder containing all the depency classes:
ButtonListen.class
ButtonListener.class
Client$1.class
Client$2.class
Client$3.class
NewPage.class
NewPage.java
NextPage.class
NextPage.java
OptIPDemo$1.class
OptIPDemo$2.class
OptIPDemo$3.class
OptIPDemo$4.class
OptIPDemo$5.class
OptIPDemo$Doc$1.class
OptIPDemo$Doc.class
OptIPDemo.class
OptIPDemo.java
ServerStats.class
ServerStats.java
blue2.jpeg
blue3.JPG
For whatever reason it is giving me a NoClassDefFoundError, but I dont know why, or how to resolve it.
Client.class seems to be missing.
And I would get into the habit of using packages to organize your code.
You either need to compile with -d
javac -d classes
...or move all your classes into a classes/Client directory
mkdir classes/Client
mv *.class classes/Client
Then, adjust your manifest to use a package-qualified Main-Class:
Main-Class: Client.Client
And finally, build the jar so that the classes are located within the Client directory of the jar:
cd classes
jar cmf ../Client.mf ../Client.jar *
I have created my jar file in the following folder:
/usr/local/bin/niidle.jar
And I have one jarfile which is in the following folder
/Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar
And this file 'hector-0.6.0-17.jar' I have to include in MANIFEST.MF in jar.
And when I mention class path in MANIFEST.MF as follows:
Manifest-Version: 1.0
Main-Class: com.ensarm.niidle.web.scraper.NiidleScrapeManager
Class-Path: /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar
When I run this using command:
java -jar /usr/local/bin/niidle.jar
It works properly..
But I dont want to give full Class-Path name, I have to give Class-Path as follows:
Manifest-Version: 1.0
Main-Class: com.ensarm.niidle.web.scraper.NiidleScrapeManager
Class-Path: lib/hector-0.6.0-17.jar
And when I run this using command:
java -jar /usr/local/bin/niidle.jar
It is showing error message:
Exception in thread "main" java.lang.NoClassDefFoundError: me/prettyprint/hector/api/Serializer
at com.ensarm.niidle.web.scraper.NiidleScrapeManager.main(NiidleScrapeManager.java:21)
Caused by: java.lang.ClassNotFoundException: me.prettyprint.hector.api.Serializer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more
Please tell me solution for that...
I see at least two solutions:
store hector-0.6.0-17.jar inside niidle.jar and use a relative path in Class-Path. E.g. Class-Path: lib/hector-0.6.0-17.jar (for niidle.jar/lib/hector-0.6.0-17.jar)
java -cp /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar -jar /usr/local/bin/niidle.jar
Create a lib folder in the folder where you are running the command, put hector-0.6.0-17.jar in it and try running the command again.