• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

Netbeans 4.1 adding .jar to project

So netbeans has done away with the mount thing(which I'm not familar with anyway.) So, how do I add a .jar file to my project.  Here is what I did and it did not work

I have a project called Server

I have a package

net.cardgamesnetwork.games.server ------------> this is where the main class is

and another package

net.cardgamesnetwork.database.drivers ---------> this is where the database .jar is

I right  - clicked on the project and added that jar file to the run and complie time classpath.  But, when my program try to acces it as follows:

String driverName = ("org.gjt.mm.mysql.Driver"); // MySQL MM JDBC driver
Class.forName(driverName);

I get

java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
java.lang.NullPointerException

at runtime.  But it does compile fine.

What is the proper way to include database drivers with your project when you build it as a .jar?
0
turtletimer
Asked:
turtletimer
  • 8
  • 7
1 Solution
 
aozarovCommented:
You need to add it to your Run settings.
Right click on the project go to Libraries. pick the run option and add your jars there as well.
0
 
turtletimerAuthor Commented:
" I right  - clicked on the project and added that jar file to the run and complie time classpath. "

I did that...
0
 
aozarovCommented:
I assume you are running from the IDE? right?
If you right click on your Main class and look at Properties/Runtime Classpath you should see that libraries you added (mm.mysql-XXXX-bin.jar for MySQL driver)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
turtletimerAuthor Commented:
I build the jar with the IDE then upload it to a linux server and execute it remotely through SSH.  I did what you said and the jar file is in the run-time classpath.  I was wondering is that classpath relative or absolute?  Becasue it is different on the linux server.  How do I set a relative classpath for just inside of the .jar file.

0
 
turtletimerAuthor Commented:
It does find the class when I run it on the IDE.  I just tried.  When I build my .jar and upload to the linux server it does not work.
0
 
aozarovCommented:
>>  I was wondering is that classpath relative or absolute?
it can be either. (relative will be translated to absolute based on where you are running from).


>>When I build my .jar and upload to the linux server it does not work.
You need to setup correctly the classpath on your linux server (make sure mm.mysql-XXXX-bin.jar is can be found localy on that machine).
0
 
aozarovCommented:
if you need help with the classpath setup on your linux machine then provide me your run command, your current directory and the place you have the mySQL jdbc driver)
0
 
turtletimerAuthor Commented:
the mysql driver is packaged in the .jar file of the project.  Is that the right way to do it?
0
 
aozarovCommented:
>> Is that the right way to do it?
No put it outside the jar and reference it in your classpath.
How are you running your program (what is the command line you are using)?  
Is it java -classpath .... your_main_class
or is it java -jar your_main_class
If it is the latter (executable jar) then you will need to change your manifest file (Class-Path: ...) to point to the library (after you extracted it).
0
 
turtletimerAuthor Commented:
I put it outside of the jar, still not getting it though.  Could you tell me exactly what I need in my manifest file and how to execute the jar


here is what i did :

okay here is my classpath environment variable:

[admin115@s3 Cards_Server]$ echo $CLASSPATH
/home/virtual/site115/fst/home/cardgamesnetwo/tomcat4/common/lib/servlet.jar:/home/virtual/site115/fst/home/cardgamesnetwo/Cards_Server/:/home/virtual/site115/fst/home/cardgamesnetwo/Cards_Server/mysql_driver/mysql-connector-java-3.0.16-ga-bin.jar

the last entry refers to the database driver.

i execute the .jar by:

java -jar <the jar file>


the main class is set inside of netbeans so I assume it sets the manifest correctly.  The jar execute, but it still does not find the database drivers
0
 
turtletimerAuthor Commented:
ahhhh this is so frustrating

my manifest file:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.4.2_08-b03 (Sun Microsystems Inc.)
Main-Class: server.Server
X-COMMENT: Main-Class will be added automatically by build
Class-Path: home/virtual/site115/fst/home/cardgamesnetwo/Cards_Server/
 mysql_driver/mysql-connector-java-3.0.16-ga-bin.jar

run command:

[admin115@s3 Cards_Server]$ java -jar Cards_Server.jar                                                                                                      
 Listening on: 4555
java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
java.lang.NullPointerException
0
 
aozarovCommented:
java -jar <the jar file> do not use the classpath settings.
You should exteract all the libraries outside the jar and have them in the same directory as the executable jar.
Then make sure that your jar META-INF/MANIFEST.MF file contains a Class-Path entry like that (Class-Path: servlet.jar connector-java-3.0.16-ga-bin.jar) [space is the delimiter]
See this link for more info about it:
http://java.sun.com/docs/books/tutorial/jar/manifest/downman.html
http://mindprod.com/jgloss/jar.html (search for Class-Path).
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20894045.html?query=jar&topics=86
http://forum.java.sun.com/thread.jspa?threadID=471397&messageID=2178634

As an alternative you can run your program this way:
java -classpath $CLASSPATH:<the jar file> full_name_of_your_main_class
0
 
aozarovCommented:
>>Class-Path: home/virtual/site115/fst/home/cardgamesnetwo/Cards_Server/ mysql_driver/mysql-connector-java-3.0.16-ga-bin.jar
Is the driver located under $PWD/mysql_driver/mysql-connector-java-3.0.16-ga-bin.jar ?

0
 
turtletimerAuthor Commented:
okay i got it.  Thanks for your help.  what is that $PWD variable? I'm more ignorant in linux than java :)
0
 
aozarovCommented:
just do: echo $PWD
Its your current directory :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now