• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using classpath when executing a jar

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did a lot of when trying to figure out how to set the classpath when executing a jar so I thought I would share what I found.

I searched through the beginner, intermediate, and advanced forums (and used google) and found the same question had been asked many times but did not find any single answer that completely worked for me.

So here goes (if I screw anything up hopefully someone more knowledgable then I will set me straight):

Suppose I have a class file, JdbcTest.class in this case, that makes use of the Microsoft SQL Server JDBC driver. To use the driver (via Class.forName) I will need to ensure that the java runtime environment knows how to reach the driver by either setting an environment variable called classpath that contains the full path or by passing the classpath to the runtime using the -cp flag (preferred method from what I understand).

ex:

c:\Projects\java\JDBCtest2>java -cp "C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.0\enu\sqljdbc.jar;." JdbcTest

I believe that the character used to separate filepaths in the classpath is platform dependent. In my case I am using Windows XP so it is ';' .
Note that I've included two paths: the path to the driver (which must include the name of the jar itself) and the current path '.'. If you specify a class path using the environment variable or the cp flag you must include the current path also. Here's the error message I get if I don't:

Exception in thread "main" java.lang.NoClassDefFoundError: JdbcTest

Okay, but what if I want to place JdbcTest.class into a jar (test.jar)? Can I just do something like this?

C:\Projects\java\JDBCtest2>java -cp "C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.0\enu\sqljdbc.jar;." -jar test.jar

No! When you try to execute a jar the classpath is ignored. So you will get a ClassNotFoundException . You need to place the classpath in the jar's manifest file instead. Here is mine:

Main-Class: JdbcTest
Class-Path: \Progra~1\MIAA2F~1\sqljdbc_1.0\enu\sqljdbc.jar

Now there are several things to note here:
First, in the manifest file it is "Class-Path" and not "classpath".

Next, the current directory does not need to be included in the classpath. But if I were to include another path it would need to be separated by a space and not a semicolon.

That of course creates a problem on Windows when you have directories with spaces in their names. So what you have do is use the DOS short-name for any directory which has a space in its name. So in this example Program Files becomes Progra~1. Now the next directory in the path is Microsoft SQL Server 2005 JDBC Driver -- how in the world did that become MIAA2F? Well, it just so happens that I have 9 directories in my Program Files directory which start with Microsoft. The first by date created would be Micros~1, followed by Micros~2, Micros~3, and Micros~4. After that, however, the system changes--see this link:
(http://www.microsoft.com/resources/documentation/windowsnt/4/workstation/reskit/en-us/diskdesc.mspx)

But you can use "dir /x" at the command prompt to discover the short-name for any file or directory which is how I figured out this one.

Also, the path to the external jar is relative to the path of my jar. Using drive names (c:\) will not work. I am not sure what you would need to do to list a path on another drive.

Lastly, remember that the manifest file must end with a new line or carriage return.

Hopefully, this helps someone.

Disclaimer: I am VERY new to java. I'm not sure that this method is the best way to access an external jar. If anyone out there has any more information to add I would LOVE to hear it!
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the nice summary, Delbert. You have collected a lot of useful information.

I usually copy all the jars I need to a single location in my project directory (e.g. /com/javaranch/project/lib) and avoid multiple classpaths (and usually spaces) in my manifest.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a great tutorial! You should consider adding it to our FAQ. I have a few suggestions/comments/questions to add to the above, if you don't mind:

1) Can you put double-quotes around directory or file names with spaces? More improtantly will this allow you to use the full name in the manifest file? I don't know since I don't use Windows for Java development. I know a lot of programs allow you to put double-quotes around file names to be able to use special characters, like space, in the name. This might be interesting to try.

2) Another alternative is to put your JAR file in the path and then execute your program as normal. For example, you can do something like this:

C:\Projects\java\JDBCtest2>java -cp "C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.0\enu\sqljdbc.jar;test.jar;." JdbcTest

Of course, this assumes that you know which class in the jar file contains main().

Well, that's my comments. Over all, I think you have summarized a lot of good information about JAR files. I will bookmark this and refer other Ranchers to it when related questions arise.

Layne
 
Delbert Bell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) Can you put double-quotes around directory or file names with spaces? More improtantly will this allow you to use the full name in the manifest file? I don't know since I don't use Windows for Java development. I know a lot of programs allow you to put double-quotes around file names to be able to use special characters, like space, in the name. This might be interesting to try.



This does not seem to work. I tried both the relative and absolute paths(with drive letter). It looks like only the short-name will work in the manifest file.

2) Another alternative is to put your JAR file in the path and then execute your program as normal. For example, you can do something like this:

C:\Projects\java\JDBCtest2>java -cp "C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.0\enu\sqljdbc.jar;test.jar;." JdbcTest



I didn't know that was an option. I tried it and it worked for me. Thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic