0

I know this question is asked enough no. of times here before but unluckily I'm still not able to find solution. I'm trying to run swt-java project from command prompt (without eclipse) & getting the following exception :

Exception in thread "main" java.lang.NoClassDefFoundError: com/beans/MyBean
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    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:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70)
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276)
    at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
    at org.hibernate.cfg.Configuration.add(Configuration.java:669)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
    at com.facadeimplementation.hibernate.util.HibernateFactory.configureSessionFactory(HibernateFactory.java:83)
    at com.facadeimplementation.model.dao.MyDao.<init>(MyDao.java:23)
    at com.facadeimplementation.model.dao.MappingDao.<init>(MappingDao.java:23)
    at com.ui.MainForm.<init>(MainForm.java:45)
    at com.ui.MainForm$1.run(MainForm.java:77)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at com.ui.MainForm.main(MainForm.java:74)
Caused by: java.lang.ClassNotFoundException: com.beans.MyBean
    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)
    ... 37 more

I'm able to run this project successfully in Eclipse.

MyBean.java is contained in a Jar. I have so many POJOs used in the project so it's not possible to use them directly instead of using them from within a jar.

I have followed so many links to solve this. Some of them are :

What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?

http://javaeesupportpatterns.blogspot.in/2012/06/javalangnoclassdeffounderror-how-to.html

http://javarevisited.blogspot.in/2011/06/noclassdeffounderror-exception-in.html

Can anyone help me?

EDIT

I'm trying to run my Java app using the following command:

java -classpath .:swt.jar:mybeans.jar:(other necessary jars separated by :) com.ui.MainForm.java
Community
  • 1
  • 1
RAS
  • 8,100
  • 16
  • 64
  • 86
  • Did you check if the jar containing your `MyBean` class is correctly defined within the classpath in the manifest? – Baz Oct 15 '12 at 11:48
  • Surely from reading those threads (and supposedly more) the 'run-time class path' would have been mentioned at least once. What exact command is used to launch the app.? Does the main Jar have a manifest? What is the content of the manifest? – Andrew Thompson Oct 15 '12 at 11:49
  • @Baz and Andrew, Which manifest? I have created a manifest.txt as per this link: http://wiki.eclipse.org/FAQ_How_do_I_create_an_executable_JAR_file_for_a_stand-alone_SWT_program%3F – RAS Oct 15 '12 at 12:05
  • @RAS You should read the Oracle tutorial about manifest files: [Working with Manifest Files: The Basics](http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html) – Baz Oct 15 '12 at 12:07
  • @RAS was all your required jars set to the classpath? – thar45 Oct 15 '12 at 12:24

2 Answers2

0

Make sure your bean can be found as com/beans/MyBean.class in the JAR file. I.e. when you list the contents of the JAR file with jar tf, there must be a line which reads

com/beans/MyBean.class

It must be exactly like that. Also make sure that you did add the JAR to the classpath. You can use this code to check:

URL url = getClass().getClassLoader().getResource("com/beans/MyBean.class"),

If url is null, then the class is not on the runtime class path.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

The starting point of the error is at

com.facadeimplementation.hibernate.util.HibernateFactory.configureSessionFactory(HibernateFactory.java:83)

and then you are getting.

Exception in thread "main" java.lang.NoClassDefFoundError: com/beans/MyBean

There are chances that you are using Third party Dependency in your POJO and that API is referencing some other API. If Its also missing from CLASSPATH. You may get this error.

Just check whatever Third Party Dependency you are using in your POJO com/beans/MyBean is in the CLASSPATH.

As you mentioned that,It is running find from eclipse.

Is all .jar files which are in eclipse CLASSPATH are also in the CLASSPATH when you run it from CMD.

May be you list down API's being used in that POJO.That might be more helpful.

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96