JD87
JD87

Reputation: 123

Setting the classpath for JAR files

I have recently just created Java project using Eclipse that requires 2 JAR files (phiget21.jar and the mysql.jar)

Everything works fine when running the programme in Eclipse, and I have noticed the the jar files are saved in a 'lib' folder.

I soon going to me moving the programme off my computer to be used on other machines, so I decided to create a batch file to compile all of the classes and then run.

However, I am having trouble with the locating of the jar files. In the batch file do I require a command something like: set classpath=.:..;mysql.jar:../phidget21.jar, before the compilation of the Java classes?

I have read that the dots (...) have something to do with directories but not entirely sure how to implement them.

My programme is currently saved in these locations:

Project/src/.java files (I have also put the .jar files in here as well as i thought this may make thing s easier)

Project/lib/ .jar files

Any help would be greatly appreciated!

Upvotes: 6

Views: 43423

Answers (2)

krystan honour
krystan honour

Reputation: 6793

You need something like

java -classpath lib/foo.jar:. com.company.Program

you can also use wildcards since java 6. see here

so the above becomes

java -classpath lib/*:. com.company.Program

Upvotes: 1

Chandra Sekhar
Chandra Sekhar

Reputation: 19492

while setting the classpath a single dot (.) means current directory. As you jar files are in current directory, you just need to go to your current directory using cd command in DOS prompt, then use

set classpath = .;filename.jar;another filename.jar

Here . represents current directory and semicolon separates each classpaths.

You can even set classpath of more than one jar files using wild card character * which can be read as all.

Upvotes: 9

Related Questions