• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Strange ClassNotFoundException working with ucanaccess

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code that I use to get the connection:

The code works perfectly in Eclipse running as an application or debugging; It has worked for more than a year. The problem I have is that I am getting in the line "Class.forName(driver);" running the code in a jar file. I am mystified; how could the virtual machine know that it is running code compiled for Eclipse or for a jar file compiled by Eclipse?
 
Sheriff
Posts: 24632
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't compile the code for Eclipse, you just compiled the code. Compiled Java code can be run anywhere there's a JVM to run it.

However what the JVM does need to know, at the time you do "Class.forName(class)", is where that class is located. In Eclipse you have the build path which tells the JVM where to look for it. In the jar file you have a manifest for that purpose. No doubt you didn't put a manifest in the jar when you created it because you didn't know about that yet.
 
Alejandro Barrero
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replay Paul. I created the jar file in Eclipse and it does have a MANIFEST file specifying what is the Main_Class.  I cant specify the class with the the package from the jar file. Could you please explain how do I specify the class when I am creating the jar file?
 
Paul Clapham
Sheriff
Posts: 24632
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean how do you put things into the manifest when you ask Eclipse to create a jar file?

I don't know. But that really isn't a JDBC question so I'll attach this thread to the Eclipse forum as well.

And by the way, here's a tutorial which explains how to format the classpath entry in a JAR manifest: Adding Classes to the JAR File's Classpath.
 
Alejandro Barrero
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I create the jar file with Eclipse. I imagined that since Eclipse compiles correctly to run "As a Java Application" it will create the jar file correctly, but surprisingly it does not. Creating the jar file with Eclipse, I don't see any option to specify classes or jar files. It appears I have to create the jar file myself; I an exploring information on the Internet, but any information or links that you can give me will be a great help.
 
Paul Clapham
Sheriff
Posts: 24632
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alejandro Barrero wrote:Yes, I create the jar file with Eclipse. I imagined that since Eclipse compiles correctly to run "As a Java Application" it will create the jar file correctly, but surprisingly it does not.



It does create the jar file correctly. But it has no idea that the contents of the jar file are going to use other classes which aren't in the jar; it's your responsibility to deal with that.

It might be easiest to run the jar using something like "java -cp yourjar;uncanaccess.jar ..." instead of the executable jar version "java -jar yourjar". But I'll leave it at that because I don't try to explain to people how to set up their classpath on this forum.
 
Alejandro Barrero
Ranch Hand
Posts: 447
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an easy solution. Do it with Eclipse -> Export using the option "Runnable JAR file" instead of plain "JAR file".
 
Paul Clapham
Sheriff
Posts: 24632
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works for you? Excellent! And thanks for posting your solution.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!