Running Jars with other Jars
Hello people, let me tell you a little stroy
I'm using netbeans IDE to wrtie a program that uses the mysql connector package to interface with a database. Every time I compile in the IDE, it gives me a nice little command line executable to try. (Something along the lines of
"To run this application from the command line without Ant, try:
java -jar "C:\...path to my jar"
... so I go ahead and try it ...
I'm getting a class not found error. the class its not finding is the connector:
com.mysql.jdbc.Driver
thus, im wondering what the syntax would be to include that in the command line call .... or if there's a better way to run my application with all of the extensions it needs without the IDE.
Thanks in advance ..
Sorry, but we have heard that story before...
To run an Java application use:
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-clientto select the "client" VM
-serverto select the "server" VM
-hotspotis a synonym for the "client" VM [deprecated]
The default VM is client.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-versionprint product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -helpprint this help message
-Xprint help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
Lajma at 2007-7-14 22:24:46 >

maybe java -cp ./any_connector.jar -jar "C:\...path to my jar" ?
> -cp <class search path of directories and zip/jar files>Which would be exactly what you need. You're using a library that's not in the classpath.
Hah :) awesome I knew it wasn't an original ... but nevertheless i'm sure it'll have a happy ending .. i'll get busy with that ..thanks
As I just posted in another thread, you can also add the other JAR to the class-path entry in your JAR's manifest file.
Certainly with NB 5.0 (which is what I'm using) if you add the mysql library to the projects libraries list, then NetBeans will create a lib directory in you dist directory and copy the jar into there, plus place the appropriate Class-path: directive into you manifest file.
Providing you keep the jar and the lib directory together it should run as recomended.
Its the following line that causes a ClassNotFoundException
Class.forName("com.mysql.jdbc.Driver");
.. im still getting the original error message? ... could it be that im still not specifying the classpath correctly?
.. ps Lajm .. could you post the link to where you got that tutorial?
> > .. ps Lajm .. could you post the link to where you> got that tutorial?- Open a command prompt.- Type 'java'Done...
Lajma at 2007-7-14 22:24:46 >

hey malcolmmcwhy is the MANIFEST.MF file read/only? .. i think i want to add it there
If you use the java -jar option any classpath, either on the command or in the CLASSPATH environmental variable is ignored. There are two options:
1) Don't use the -jar option. Put of the jar files on the classpath and give the Java command the name of the main class.
2) Put the location of the connector jar in the manifest file as a Class-path: entry. As I said, Netbeans will do this for you if you tell it the library is needed.
hey malcolmmc ... a little more info
I go under the files tab in NB 5.0 then my project foldr -> dist -> lib -> myproject.jar -> META-INF -> MANIFEST.MF
this is the file that has anentry like "Class-Path: lin/swing-layout-1.0.jar"
.. and this is the one thats read-only.. should I be looking to edit some other manifest file?
ok .. so ill try option 1 ... as for option 2 ... I should be able to run my app without NB then? which is where i thought i was in the beginning ...i'll try the first suggestion
ok wait .. problem with option 1 ... I've got all of them as Main.main for the initial calls .. would putting the jar in the environment %CLASSPATH% variable, then calling java Main execute them all? .. or just the first or what?
You need to put both of the jar files in the class path. Using the -cp option is safer than the environment variable (I don't think it works too well on WIndows).
Then just give the fully qualified name of your main class.
I don't understand wat you mean by "all of them " as Main.main. You have only one main class per project, and you can't have more than one class with the same fully qualified name.
To get NB to put the right entry into your jar, you need to tell NB about the connector library. Go to the libraries node under the project in the project view window, right click and select "Add jar/directory".
Then, when NB builds the jar it will copy the connector library into the dist/lib directory and put the appropriate entry in the manifest.
ok sweet .. ive seen that there .. the "Add jar/library" ... ok let me try that..
also ive got to go afk for like an hour or two ... perhaps I could try a few things and get back to you later on, after my obligations ... I'll repost somthing around 1ish (EST) to see if that works ..
thank you very much
Hey peopleabout setting the classpath environment variable .. I'd need to add jars to it correct? thats still not fixing the problem... which is the ClassNotFoundException from the call:Class.forName("com.mysql.jdbc.Driver");any ideas?