Say you have multiple jar files a.jar,b.jar and c.jar. To add them to classpath while compiling you need to do
$javac -cp .:a.jar:b.jar:c.jar HelloWorld.java
To run do
$java -cp .:a.jar:b.jar:c.jar HelloWorld
Answer from noPE on Stack OverflowSay you have multiple jar files a.jar,b.jar and c.jar. To add them to classpath while compiling you need to do
$javac -cp .:a.jar:b.jar:c.jar HelloWorld.java
To run do
$java -cp .:a.jar:b.jar:c.jar HelloWorld
You use the -classpath argument. You can use either a relative or absolute path. What that means is you can use a path relative to your current directory, OR you can use an absolute path that starts at the root /.
Example:
bash$ java -classpath path/to/jar/file MyMainClass
In this example the main function is located in MyMainClass and would be included somewhere in the jar file.
For compiling you need to use javac
Example:
bash$ javac -classpath path/to/jar/file MyMainClass.java
You can also specify the classpath via the environment variable, follow this example:
bash$ export CLASSPATH="path/to/jar/file:path/tojar/file2"
bash$ javac MyMainClass.java
For any normally complex java project you should look for the ant script named build.xml
First thing to note - you're not using "Putty" to set the CLASSPATH, you're using the 'shell' that you login to, using Putty as your SSH client.
Depending on your shell, there are different ways to set your classpath -
Bash (or compatible shells
export CLASSPATH=/full/path/to/xalan.jar:/full/path/to/serializer.jar:/full/path/to/xml-apis.jar:/full/path/to/xercesImpl.jar:$CLASSPATH
C Shell (or compatible shells)
setenv CLASSPATH /full/path/to/xalan.jar:/full/path/to/serializer.jar:/full/path/to/xml-apis.jar:/full/path/to/xercesImpl.jar:$CLASSPATH
Define an environment variable in your putty session for CLASSPATH with the path of required jars. If you are using bash shell in putty then export CLASSPATH=<Jar Path>:$CLASSPATH will help. Use java -cp .:$CLASSPATH ... for including your CLASSPATH.
For JAR files, you have to set the CLASSPATH and not the PATH environment variable.
If you're using BASH, it is: export CLASSPATH="$CLASSPATH:<full_path_to_each_jar_files>"
You better add it in the file .bashrc unless you know what you are doing.
Example:
export CLASSPATH="$CLASSPATH:$HOME/java/lib/foebar.jar:$HOME/extra/lib/another.jar"
But of course, if you are still invoking the jar file with the Main class you have to use the full path for it:
java -jar $HOME/java/lib/main-prog.jar
However, you can set its execution right and run it:
chmod u+x $HOME/java/lib/main-prog.jar
export PATH=$PATH:$HOME/java/lib
main-prog.jar
But you have to take care that your classpath is correct and list all required jar.
If your tools are scripts, which contain commands like
java -jar somejafile.jar
then you should edit them to contain the correct path
java -jar /full/path/to/somefile.jar
Try this
java -classpath ./db2jcc_license_cu.jar:./db2jcc4.jar:. CreateCoffees
when you use -classpath it looses current directory from classpath so it needs . in classpath as well explicitly
How to include the jars of your project into your runnable jar:
I'll walk you through it step by step with Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.
Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class:
RunnerPut this code in there.
Now include the
mysql-connector-java-5.1.28-bin.jarfrom Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to
File->Export->Java->Runnable Jar File. You will see this dialog:
Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an
ant jarlater.Then go to the terminal and look at the ant script:

So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.
Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class
To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.
Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.