Search code examples
javabashunixclasspathjavac

Bash compiling Java classes and JARs


I am attempting to write a script that will automate a few tasks that I do regularly. The folder structure of my files is like so:

./
sampleScript.sh
src/SampleProgram.java

In addition to this, my Java program relies on the ojdbc6.jar file, which resides in ~/sampleFolder/lib.

I have attempted to compile and run the Java program in my script like so:

javac -cp "$CLASSPATH:~/sampleFolder/lib/ojdbc6.jar:./src" src/SampleProgram.java
java -cp "$CLASSPATH:~/sampleFolder/lib/ojdbc6.jar:./src" SampleProgram

Unfortunately, this does not work. It seems to fail at the step in my program that checks for the ojdbc6.jar dependency. What's the proper way to assign my -cp based on this folder structure?


Solution

  • You must keep ~ outside quote so that shell can expand it:

    javac -cp "$CLASSPATH":~/sampleFolder/lib/ojdbc6.jar:./src src/SampleProgram.java
    java  -cp "$CLASSPATH":~/sampleFolder/lib/ojdbc6.jar:./src SampleProgram