I'm creating a JAR file for my project that uses Log4j2. I have saved the Log4j2 jars (for CORE and API) in the 'lib' folder under the 'src' folder. Within Eclipse, I exported my project to create the JAR file and I have checked that the 'lib' folder is included/present in the JAR file.
When I run the project from within eclipse, it runs absolutely fine but when I try to execute the jar via the cmd prompt, I get the following error:
C:\Users\workspace>java -jar ConnectorClient.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at com.client.ConnectionClient.<clinit>(ConnectionClient.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
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)
... 1 more
The line in my code (Line 20) causing the error is:
private static Logger logger = LogManager.getLogger(ConnectionClient.class);
It seems as if the Log4j2 jars are not being read. Can somebody be able to help me here please. I've already added the 2 jars in the project's Java Build Path as shown in the image..
My classpath file (present inside the project folder) includes the 2 jars in questions as shown:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_05"/>
<classpathentry kind="lib" path="lib/log4j-api-2.0-rc1.jar"/>
<classpathentry kind="lib" path="lib/log4j-core-2.0-rc1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Any help please?
Thanks
Maybe you want to export a Runnable Jar file:
And include the required libraries:
There is a common misconception that a jar can be packaged into another jar. It just won't be available to the classloader, so you will end up with class loading exceptions.
So you either extract the dependency jar into your own jar, as described using eclipse by #PaulVargas, OR you do what 90% of people do and add your dependencies to the classpath of your jar execution:
java -classpath libs/*.jar -jar MyJar.jar
This means putting all your dependencies in a separate folder (here, libs) NOT packaged within your jar.
Related
Hi I am currently using Eclipse Kepler for my java project.
I wanted to use the Octave (the open sourced MatLab) for calculation. Therefore I have tried to use the javaoctave (https://kenai.com/projects/javaoctave) to help me do the calculation in java. However, when I want to compile my code I get the following error:
Exception in thread "main" dk.ange.octave.exception.OctaveIOException: java.io.IOException: Cannot run program "octave": error=2, No such file or directory
at dk.ange.octave.exec.OctaveExec.<init>(OctaveExec.java:102)
at dk.ange.octave.OctaveEngine.<init>(OctaveEngine.java:65)
at dk.ange.octave.OctaveEngineFactory.getScriptEngine(OctaveEngineFactory.java:49)
at learn.CapabilityLearner.connectOctave(CapabilityLearner.java:72)
at learn.CapabilityLearner.connect(CapabilityLearner.java:37)
at learn.CapabilityLearner.<init>(CapabilityLearner.java:24)
at learn.capability.CapabilityMeanVarianceEquation.<init>(CapabilityMeanVarianceEquation.java:10)
at parse.basketball.runner.GetPlayerFrequency.learnSynergyGraph(GetPlayerFrequency.java:102)
at parse.basketball.runner.GetPlayerFrequency.main(GetPlayerFrequency.java:94)
Caused by: java.io.IOException: Cannot run program "octave": error=2, No such file or directory
at java.lang.ProcessBuilder.processException(ProcessBuilder.java:478)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:457)
at java.lang.Runtime.exec(Runtime.java:593)
at dk.ange.octave.exec.OctaveExec.<init>(OctaveExec.java:100)
... 8 more
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
at java.lang.ProcessImpl.start(ProcessImpl.java:91)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 10 more
This java project has been successfully run on a Windows machine with Eclipse Kepler so I think it is not the code I have that contains an error. the wiki has mentioned the error and some explanation is given. However, due to my lack of experience with programming I could not have grasped the meaning of the solutions. Can anyone help me to identify the problem and teach me how I shall fix this?
With many thanks ! :)
PS : the .classpath of the project is as followed:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="matlabcontrol-4.0.0.jar"/>
<classpathentry kind="lib" path="javaoctave-0.6.4.jar" sourcepath="javaoctave-0.6.4.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="commons-math-2.2.jar"/>
<classpathentry kind="lib" path="commons-logging-1.1.3.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
You can easily solve your problem setting up the Factory
OctaveEngineFactory factory = new OctaveEngineFactory();
factory.setOctaveProgram(new File("path_to_octave_executable"));
OctaveEngine octave = factory.getScriptEngine();
bgcode
I created a project in Eclipse and executed it succesfuly (It's a normal project, without Maven or Beans, really simple), I created a .jar with the project and tried to execute it but it throws the following error:
C:\Documents\wrapper-windows-x86-64-3.5.19-st\bin>java -cp QuoteHandler.jar stoc
k.view.Main
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hiber
nate/cfg/Configuration
Exception in thread "Quotes" java.lang.ExceptionInInitializerError
at stock.controller.HollidayController.<clinit>(HollidayController.java:
25)
at stock.view.MainThread.run(MainThread.java:57)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at stock.controller.HollidayController.<clinit>(HollidayController.java:
22)
... 2 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
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)
... 3 more
The .classpath file of the project has:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7"/>
<classpathentry kind="lib" path="lib/antlr-2.7.7.jar"/>
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="lib/hibernate-commons-annotations-4.0.1.Final.jar"/>
<classpathentry kind="lib" path="lib/hibernate-core-4.1.10.Final.jar" sourcepath="lib/hibernate-core-4.1.10.Final.jar"/>
<classpathentry kind="lib" path="lib/hibernate-entitymanager-4.1.10.Final.jar"/>
<classpathentry kind="lib" path="lib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
<classpathentry kind="lib" path="lib/javassist-3.15.0-GA.jar"/>
<classpathentry kind="lib" path="lib/jboss-logging-3.1.0.GA.jar"/>
<classpathentry kind="lib" path="lib/jboss-transaction-api_1.1_spec-1.0.0.Final.jar"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.20-bin.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.beans_3.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.web_3.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.context_3.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/joda-time-2.2.jar"/>
<classpathentry kind="lib" path="lib/c3p0-0.9.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Checking on the Internet I modify the %CLASSPATH% variable to:
CLASSPATH = C:\Program Files\Java\jre7\lib;C:\Program Files\Java\jre7\lib\c3p0-0.9.1.jar
All the References are well configured in the Build Path of the project and it works, but when I'm executing the .jar it doesn't work (All the references are included in the jar). What am I missing?
when you specifically specify the class path when you run your program with -cp you override the default CLASSPATH os variable.
So you need to either specify all the needed classes/jars as a list in -cp, or you need to modify your CLASSPATH to have the directory of your jar. This question, Setting multiple jars in java classpath, describes setting multiple classpath elements.
Try following option from Eclipse:
File->Export
From the Wizard select Java->Runnable JAR file
Click Next
Select Launch Configuration: Select the class with main() method, if it is not available here then try to run your program once in eclipse
Export Destination: Select full path for your jar file
Library Handling: Select third one "Copy required lib..."
click on finish
Go to command prompt and change the dir to exported jar file and type
java -jar <exported_jar>.jar
One note:
In java for classpath lib/* does not resolve to all jars in directory, you have to type each and every jar entry manually.
You are trying to launch the program using command-line, the .classpath is file is for the eclipse project to maintain the list of jars pertaining to that project and has nothing to do with running the same code from command line, that's why you get the "java.lang.ClassNotFoundException"
Looking at the .classpath file, I think you project structure is like:
Proj
|
|---src
|
|---lib
So in order to run the code you can do,
For windows:
java -cp lib/*;/path/to/your/jar/QuoteHandler.jar stock.view.Main
For UNIX:
java -cp lib/*:/path/to/your/jar/QuoteHandler.jar stock.view.Main
Few things to note here:
-cp argument is ignored if the -jar option is used.
1. So you have two options, modify the manifest file to include the classpath variables
2. Add this jar too in classpath along with other jars as mentioned above, and run the main class.
Also, using -cp will basically override the values of Environment Variable for that session.
Next time again if you try the program again without -cp switch, default value (set in environment variable) of CLASSPATH will be picked.
UPDATE
If you are using this command:
java -cp lib/*;QuoteHandler.jar stock.view.Main
1. Remove quotes they add no value
2. If I look at the command above it means that, you layout is something like:
somefolder
|
|----QuoteHandler.jar
|
|----lib
| |
| |--- antlr-2.7.7.jar
| |--- dom4j-1.6.1.jar
| |--- hibernate-commons-annotations-4.0.1.Final.jar
| |--- hibernate-core-4.1.10.Final.jar
| |---...and so on (NO SUBDIRECTORIES)
Is it the same structure you are having here?
You mentioned in comments of using wildcards.The wildcards only work on JDK 6 onward . So if you JDK version is lesser than that, it might not work (and the way around it to add all the jars in path).
When starting my Java application, I get exceptions when trying to save images. In Eclipse, however, everything works fine. The application is built using fatjar and the necessary libraries (jar_imageio.jar and ij.jar) have been selected for export as well.
I tried using ImageIO and ImageJ:
a.) ImageIO:
ImageIO.write(image, "jpg", new File(f));
Exception in thread "main" sun.misc.ServiceConfigurationError:
javax.imageio.spi.ImageWriterSpi:
Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi
could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!
at sun.misc.Service.fail(Unknown Source)
at sun.misc.Service.access$200(Unknown Source)
at sun.misc.Service$LazyIterator.next(Unknown Source)
at javax.imageio.spi.IIORegistry.registerApplicationClasspathSpis(Unknown Source)
at javax.imageio.spi.IIORegistry.<init>(Unknown Source)
at javax.imageio.spi.IIORegistry.getDefaultInstance(Unknown Source)
at javax.imageio.ImageIO.<clinit>(Unknown Source)
b.) ImageJ:
IJ.saveAs(image, "jpg", f);
java.lang.NoClassDefFoundError: Could not initialize class javax.imageio.ImageIO
at ij.plugin.JpegWriter.saveAsJpeg(JpegWriter.java:49)
at ij.plugin.JpegWriter.save(JpegWriter.java:28)
at ij.io.FileSaver.saveAsJpeg(FileSaver.java:340)
at ij.io.FileSaver.saveAsJpeg(FileSaver.java:332)
at ij.plugin.filter.Writer.run(Writer.java:24)
at ij.plugin.filter.PlugInFilterRunner.processOneImage(PlugInFilterRunner.java:256)
at ij.plugin.filter.PlugInFilterRunner.<init>(PlugInFilterRunner.java:105)
at ij.IJ.runPlugIn(IJ.java:158)
at ij.Executer.runCommand(Executer.java:127)
at ij.Executer.run(Executer.java:64)
at ij.IJ.run(IJ.java:249)
at ij.IJ.run(IJ.java:296)
at ij.IJ.saveAs(IJ.java:1579)
As #Victor says I think you should look at
Exception in thread "main" sun.misc.ServiceConfigurationError:
javax.imageio.spi.ImageWriterSpi:
Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi
could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!
I had this issue just yesterday and it was tricky. There are similar questions here. I found if I included jai_imageio in the jar and did not modify the manifest file to include the contents of the JAI manifest file or combine the files in the services folder of META-INF in your build then I had a number of errors similar to yours. My application did work though without JAI included since JAI was installed locally I opted to build it with JAI included for the time being.
Opening you jar you will find a directory called META-INF. In there is the file MANIFEST.MF. I use Maven to include the JAI things in the Manifest file so it looks like
Manifest-Version: 1.0
Implementation-Title: com.sun.media.imageio
Implementation-Version: 1.0_01
Built-By: myName
Specification-Vendor: Sun Microsystems, Inc.
Created-By: Apache Maven
Implementation-Vendor: Sun Microsystems, Inc.
Build-Jdk: 1.6.0_43
Specification-Title: Java Advanced Imaging Image I/O Tools
Specification-Version: 1.0-mr
Extension-Name: com.sun.media.imageio
Main-Class: myMain
Archiver-Version: Plexus Archiver
You should have your name and your main class substituted in there. You could just modify this file and jar it up yourself on the command line if you don't use Maven (or Ant) to get it working. I had the extra issue where some of my included jars were overwritting files in the services folder of META-INF. Instead I merged these files using Maven's Shade plugin.
add this lines into build.xml (solved for me)
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Built-Date" value="${TODAY}" />
<attribute name="Implementation-Title" value="MyApp" />
<attribute name="Implementation-Vendor" value="MyCompany" />
<attribute name="Implementation-Version" value="${version.num}-b${build.number}"/>
</manifest>
I created a runnable jar using ant but it is not running. when I run the jar I am getting following error
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/teamdev/jxbrowser/events/NavigationListener
at com.MainClass$2.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.ClassNotFoundException: com.teamdev.jxbrowser.events.NavigationListener
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)
... 15 more
but when I created jar using eclipse that jar worked fine. Menifest of jar I created with ant is as below
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_37-b06-434-11M3909 (Apple Inc.)
Main-Class: com.MainClass
Class-Path: ./ commons-cli.jar commons-codec-1.2.jar commons-httpclien
t-3.0.1.jar commons-logging-1.1.1.jar ffmpeg-java.jar fmj.jar jdom.ja
r logback-classic.jar logback-core.jar lti-civil-no_s_w_t.jar mail.ja
r mp3spi1.9.4.jar slf4j-api.jar tritonus_share.jar vorbisspi1.0.2.jar
xuggle-xuggler.jar comfyj-2.9.jar engine-gecko.jar engine-ie.jar eng
ine-webkit.jar jniwrap-3.8.4 jxbrowser-3.3.jar jxbrowserdemo.jar log4
j-1.2.15.jar MozillaInterfaces.jar runtime.jar slf4j-api-1.5.8.jar sl
f4j-log4j12-1.5.8.jar winpack-3.8.3.jar xulrunner-mac.jar
and menifest of jar I created with eclipse is as below
Manifest-Version: 1.0
Rsrc-Class-Path: ./ jniwrap-3.8.4.jar lti-civil-no_s_w_t.jar commons-h
ttpclient-3.0.1.jar slf4j-log4j12-1.5.8.jar vorbisspi1.0.2.jar slf4j-
api.jar ffmpeg-java.jar fmj.jar commons-codec-1.2.jar engine-ie.jar x
ulrunner-mac.jar commons-logging-1.1.1.jar winpack-3.8.3.jar mp3spi1.
9.4.jar tritonus_share.jar commons-cli.jar jdom.jar MozillaInterfaces
.jar comfyj-2.9.jar jxbrowser-3.3.jar runtime.jar logback-classic.jar
engine-gecko.jar mail.jar engine-webkit.jar log4j-1.2.15.jar jxbrows
erdemo.jar slf4j-api-1.5.8.jar logback-core.jar xuggle-xuggler.jar
Class-Path: .
Rsrc-Main-Class: com.MainClass
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
both are same and all dependencies are also inside the created jar but still the jar create with ant is not working.
I know you would recommend me to create jar with eclipse but I want to obfuscate my code that's why I am using ant to create runnable jar.
I guess that your jar file generated with Ant does not have jar-in-jar-loader, that's why it is not able to find classes inside embedded jars.
When you generate JAR with Eclipse you can Save Ant script, then jar-in-jar-loader.zip file would be added to project. Then use generated Ant file to create your JAR. This approach works for me.
Your Ant script should look like this:
<jar destfile="C:\Users\\workspace\Your.jar">
<manifest>
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
<attribute name="Rsrc-Main-Class" value="org.mypackage.MainClass"/>
<attribute name="Class-Path" value="."/>
<attribute name="Rsrc-Class-Path" value="./ libA.jar "/>
</manifest>
<zipfileset src="jar-in-jar-loader.zip"/>
<fileset dir="${ProjectPath}/bin"/>
<zipfileset dir="${ProjectPath}\lib" includes="libA.jar"/>
</jar>
First zipfileset would include jar-in-jar-loader.zip
fileset would add all your classes
Second zipfileset would add libA.jar as embedded jar and same should be mentioned in Rsrc-Class-Path
With your Ant-generated Manifest, it is looking for the library jar files outside of the main jar file, the Eclipse one uses a special Main-Class and class loader to get to the bundled dependencies.
You probably want to use the OneJar ant task (or something similar) to achieve something comparable to what Eclipse does.
I have created a maven project. After I type: mvn clean package everything works fine and I see my jar file inside target. But if I click the file I get the follwing error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.Applica
tionContext
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)
Could not find the main class: comt.test.Test. Program will exit
I can run the project under Eclipse and works ok, but the jar file doesnt work.
This is the content of Manifest:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Build-Jdk: 1.6.0_23
Main-Class: com.stlpo.App
Class-Path: commons-httpclient-3.1.jar commons-codec-1.2.jar h2-1.2.138.jar
commons-lang-2.5.jar spring-core-3.0.3.RELEASE.jar spring-asm-3.0.3.RELEASE.jar co
mmons-logging-1.1.1.jar spring-context-3.0.3.RELEASE.jar spring-beans
-3.0.3.RELEASE.jar spring-expression-3.0.3.RELEASE.jar spring-orm-3.0
.3.RELEASE.jar spring-tx-3.0.3.RELEASE.jar spring-aop-3.0.3.RELEASE.j
ar aopalliance-1.0.jar spring-jdbc-3.0.3.RELEASE.jar spring-test-3.0.
3.RELEASE.jar cglib-nodep-2.2.jar logback-core-0.9.24.jar logback-cla
ssic-0.9.24.jar slf4j-api-1.6.0.jar hibernate-annotations-3.5.4-Final
.jar hibernate-core-3.5.4-Final.jar antlr-2.7.6.jar commons-collectio
ns-3.1.jar jta-1.1.jar hibernate-commons-annotations-3.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar hibernate-entitymanager-3.5.4-F
inal.jar cglib-2.2.jar asm-3.1.jar javassist-3.9.0.GA.jar opencsv-2.1
.jar jfreechart-1.0.13.jar jcommon-1.0.16.jar commons-beanutils-1.8.3
.jar beansbinding-1.2.1.jar poi-3.6.jar log4j-1.2.13.jar
poi-ooxml-3.6.jar poi-ooxml-schemas-3.6.jar xmlbeans-2.3.0.jar stax-
api-1.0.1.jar geronimo-stax-api_1.0_spec-1.0.jar dom4j-1.6.1.jar xml-
apis-1.0.b2.jar commons-dbcp-1.4.jar commons-pool-1.5.4.jar TableLayo
ut-20050920.jar
Thanks in advance for your help.
When you build a JAR, it only has your classes in it. It doesn't contain all the other libraries that your project depends on. (Doing that is possible, but it's not common and mostly inadvisable.) To run a class in your JAR that depends on Spring, you have to be sure Spring is on the classpath, either by passing a classpath to the java executable (doesn't work if using the -jar argument) or by putting a "Class-Path" attribute in the JAR's manifest file. Of course, it's not just Spring. You'll have to include a classpath entry for every jar your project depends on.
see another post regarding the same problem
you need to create a classPath (the eclipse is dong it automatically for you, and this is why you have no problem ruining the project from the eclipse) for the dependencies (in your case the spring)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
<addClasspath>true</addClasspath>
<!-- use class path prefix if your dependencies are out side the jar)
<classpathPrefix>lib/</classpathPrefix> -->
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>