File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes trouble with jar and main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble with jar and main class" Watch "trouble with jar and main class" New topic
Author

trouble with jar and main class

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm changing this post to here because now i've trouble too trying to create a jar manually

i know how to create one - and it works; the problem is that when i try to create one for my swing/hibernate proj i cant, and get the msg:
could not find the main class.


i tried to comment all code but main() in my entry point Gui class and i got to make a perfect jar; but from the moment i use the whole Gui code i get that nasty msg everytime

does anyone experienced such a behaviour?

can someone give me any hint, pls?

TiA


java amateur
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

What does your .jar's manifest look like? It needs to have a Main-Class entry (if you're trying to create an executable .jar) And check outthis thread for more information on Executable JARs


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
as i said before, everything runs great if my class Gui only has its main; from the moment i uncomment the rest of the code, i get that nasty msg

my Manifest.mf:
Manifest-Version: 1.0
Main-Class: gui.Gui

i'll give a closer look at your link

thanks


EDIT:
after i make the jar, i open it with winzip and check that my Gui class is indeed there; more, my manifest is rewriten like:
Manifest-Version: 1.0
Created-By: 1.5.0_03 (Sun Microsystems Inc.)
Main-Class: gui.Gui
[ August 17, 2005: Message edited by: miguel lisboa ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
Could you open a command prompt window, execute your jar file:
java.exe -jar yourJar.jar
And copy the contents of the screen and post it here?

For another problem I am trying to get the exact error message you report and I am unable to do it.
Anyone else that can show me a screen copy with that error?
Thanks.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks Norm
i guess its like a bingo

as i said previously, i use hibernate, and the problem now looks like hibernate jars cant be found:

Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/hibernate/HibernateException

looks like that somehow i've to tell the jar to add them (all hibernate related jars) to classpath, since from eclipse everything runs very well

is there a way to set classpath with jar?
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Yeah you can specify a classpath in the manifest, check out this:
http://java.sun.com/docs/books/tutorial/jar/manifest/downman.html
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks a lot, James; that really helped!

just look at my manifest:
Manifest-Version: 1.0
Class-Path: lib/hibernate2.jar lib/commons-logging-1.0.4.jar lib/dom4j-1.4.jar lib/commons-collections-2.1.1.jar lib/cglib-full-2.0.2.jar lib/odmg-3.0.jar lib/hsqldb.jar lib/jta.jar
Main-Class: gui.Gui

my command:
C:\eclipse\ProjectosEclipse\projectoC\bin>jar -cmf Manifest.mf 1xpto.jar bd dominio excepcoes gui hbm io negocio config util lib\*.jar

one last thing: i think its quite unbelievable that one has to do this, all by hand; doesnt eclipse should do it by itself?

once again thanks a lot
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Cool you got it working.

Eclipse can jar things up for you, forget what they call it, creating a packager or something similar, and I think there is a tutorial for doing it in the provided help.

But I usually use an ant script for doing that sort of thing, and tell eclipse to run my script for building/packaging instead of it's internal tools.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Eclipse can jar things up for you, forget what they call it, creating a packager or something similar, and I think there is a tutorial for doing it in the provided help.

it was precisely from eclipse help i learned about export/jar and so on, but tutorial was of no help in respect of my concrete problem: how to add hibernate relative jars to classpath (well, in classpath eclipse's doc they do appear there, but that's of no help)

But I usually use an ant script for doing that sort of thing, and tell eclipse to run my script for building/packaging instead of it's internal tools
i wish i had that toy too, but my ant+eclipse education is very rudimentary
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281

how is it possible that my jar only works from inside the folder where i created it?

elsewhere.. there comes the %###& grgrgrgr nasty msg again

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
If you are on a Windows system,
Could you make a batch file to execute your jar file and then copy and post the contents of the screen here?
Put this line in the batch file:
java.exe -jar Jarfile.jar

There is more info on the screen from above than you seen if you run the javaw.exe command when you double-click on the jar file.

To copy the Batchfile screen, click on the Mark button on the toolbar and drag the white box over the whole contents of the screen. Then click on the Copy button.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Norm: thanks for helping!

my main folder is like a game cat and mouse, because i'm often BACKING UP, changing, moving, deleting and adding jars, files and so on.

i can explain: just right now i finally managed to build the entire jar using eclipse wizard; tested it outside eclipse and worked; then i started checking what was really needed and what was superfluous. i hope you get the idea.

i guess my main prob is the associated hibernate relative jars: i added them to my proj as jars, but eclipse complaints so i added them too as external jars. But, to create a manual jar, this isnt enough, because i must tell the path to every one in my manifest, so i added a lib folder to my proj with all needed jars. this works manually but, and that's why i complaint, only from inside eclipse:
C:\eclipse\ProjectosEclipse\projectoC\bin>java.exe -jar gatinho.jar
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.1.7
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Configuration configure
INFO: configuring from resource: /config/hibernate.cfg.xml
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /config/hibernate.cfg.xml
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: hbm/Convencao.hbm.xml
18/Ago/2005 16:35:49 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: dominio.Convencao -> convencao
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: hbm/Entidade.hbm.xml
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: dominio.Entidade -> entidade
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: hbm/Pessoa.hbm.xml
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: dominio.Pessoa -> pessoa
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindSubclass
INFO: Mapping subclass: dominio.Utente -> pessoa
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindSubclass
INFO: Mapping subclass: dominio.Tecnica -> pessoa
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: hbm/ActoMedico.hbm.xml
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: dominio.ActoMedico -> actoMedico
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollection
INFO: Mapping collection: dominio.ActoMedico.tecnicas -> tecnicas
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: hbm/Contrato.hbm.xml
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: dominio.Contrato -> contrato
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindSubclass
INFO: Mapping subclass: dominio.ContratoParticular -> contrato
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindSubclass
INFO: Mapping subclass: dominio.ContratoTecnica -> contrato
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindSubclass
INFO: Mapping subclass: dominio.ContratoConvencao -> contrato
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: hbm/Intervencao.hbm.xml
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: dominio.Intervencao -> intervencao
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: dominio.Convencao.contratos -> contrato
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: dominio.Entidade.actosMedicos -> actoMedico
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: dominio.Entidade.contratos -> contrato
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: dominio.Utente.actosMedicos -> actoMedico
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: dominio.Tecnica.contratos -> contrato
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: dominio.Tecnica.intervencoes -> intervencao
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: dominio.ActoMedico.intervencoes -> intervencao
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-one association property references
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
18/Ago/2005 16:35:50 net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.HSQLDialect
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use outer join fetching: true
18/Ago/2005 16:35:50 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
18/Ago/2005 16:35:50 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
18/Ago/2005 16:35:50 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost/projC
18/Ago/2005 16:35:50 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=sa, password=}
18/Ago/2005 16:35:50 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not
recommended)
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use scrollable result sets: true
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use JDBC3 getGeneratedKeys(): false
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: false
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: cache provider: net.sf.hibernate.cache.EhCacheProvider
18/Ago/2005 16:35:50 net.sf.hibernate.cfg.Configuration configureCaches
INFO: instantiating and configuring caches
18/Ago/2005 16:35:50 net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
18/Ago/2005 16:35:50 net.sf.hibernate.util.ReflectHelper getBulkBean
INFO: reflection optimizer disabled for: dominio.Convencao, BulkBeanException: Property is private (property s
etNome)
18/Ago/2005 16:35:50 net.sf.hibernate.util.ReflectHelper getBulkBean
INFO: reflection optimizer disabled for: dominio.Entidade, BulkBeanException: Property is private (property se
tPessoaSingular)
18/Ago/2005 16:35:50 net.sf.hibernate.util.ReflectHelper getBulkBean
INFO: reflection optimizer disabled for: dominio.ContratoParticular, IllegalArgumentException: Cannot find mat
ching method/constructor
18/Ago/2005 16:35:50 net.sf.hibernate.util.ReflectHelper getBulkBean
INFO: reflection optimizer disabled for: dominio.ContratoTecnica, IllegalArgumentException: Cannot find matchi
ng method/constructor
18/Ago/2005 16:35:50 net.sf.hibernate.util.ReflectHelper getBulkBean
INFO: reflection optimizer disabled for: dominio.ContratoConvencao, IllegalArgumentException: Cannot find matc
hing method/constructor
18/Ago/2005 16:35:50 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured

C:\>

and ther it stops, but jar works ok with database...

if i move jar to my root:
C:\>java.exe -jar gatinho.jar
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/hibernate/HibernateException
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
The items listed on the manifest's classpath are relative on the file system to your jar.
The means that if you move your jar, you also have to move the supporting hibernate jars too.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i understand what you'r saying, James, and thanks for that: i checked inside my jar and those files arent just there: you'r 100% right

as soon as i can i'll try to correct it

thanks again
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
if i have a folder (lib) with all related hibernate stuff next to my jar, my jar works anywhere. What was happening was that lib folder was not inside my original jar...

BTW how did you figured that out?
The items listed on the manifest's classpath are relative on the file system to your jar.


for now, and since i want to keep my proj (in eclipse) as light as possible, i'll go for this solution(in case i need to build my jar manually)

as to building the jar with eclipse, i added related hibernate stuff as archives, and several packages were added to my proj; anyway that wasnt enough to build the jar so i had to add all those jars (and some more too) again as external jars also...

seems to much to add all those jars a third time (lib folder) so that i can build the jar manally - that's the reason for having a folder next to my manually build jar (with eclipse)

if i fall in trouble again i'll be back

note: as you might see, i probably dont have my hibernate stuff properly added to my proj, but i dont know how to do it in a better/proper way...

thanks to everyone who helped me!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with jar and main class