1

I've many jar files to add to my classpath, so everytime I compile my java file I end up with a command like this:

javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. CollectionIndexer.java

I've tried to use:

set CLASSPATH=commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:.

and then:

javac CollectionIndexer.java

but the jar are not added at all: I get error due to the missing jars... thanks

jww
  • 97,681
  • 90
  • 411
  • 885
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
  • Can you post the exception stack please? – Will Mar 02 '11 at 19:44
  • @Will: Given that it's a *compile-time* error, there won't be any exceptions. – Jon Skeet Mar 02 '11 at 19:46
  • You can try setting the class path when you run the java program. java -cp . Are you using eclipse? netbeans? text editor? – MBU Mar 02 '11 at 19:46
  • @Will sure: http://pastie.org/1625807 (Queryperformer.java = CollectionIndexer.java.. it just my java file) – aneuryzm Mar 02 '11 at 19:47
  • 1
    Sounds like you're getting to the point when sane folks start using ant to build the class path for them… – Donal Fellows Mar 02 '11 at 19:48
  • @Donal Fellows yeah but if I use ant, I still need to add the jars to the classpath to run my apps, right ? java -cp ... myApp (or I generate myApp.jar with ant, but this is another thing) – aneuryzm Mar 02 '11 at 21:33
  • Well, actually I go the next step beyond that and use Maven, which can (with the right prompting) assemble all the pieces into an executable jar. But it's a massive step from what you're doing to Maven! Ant is easier to start with (in my experience). – Donal Fellows Mar 03 '11 at 07:14

6 Answers6

3

Try using export CLASSPATH=... instead of set CLASSPATH=...

(I assume you're using a Unix box of some description, given the colons in the classpath.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

The most pain-free way, in my opinion, is to create batch files that contain all your project related jars... one to compile and another to run:-

compile.bat

javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1

run.bat

java -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1

With that, you can do this:-

compile.bat CollectionIndexer.java

run.bat CollectionIndexer

Even better, you can combine them together:-

compilerun.bat

Make sure you append ".java" to javac's %1

javac -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1.java
java -cp commons-digester-2.1/commons-digester-2.1.jar:lucene-core-3.0.3.jar:commons-logging-1.1.1/commons-logging-1.1.1.jar:commons-beanutils-core-1.7.0.jar:. %1

With that, you can do this:-

compilerun.bat CollectionIndexer

I like this approach much better than setting classpath because I don't need to retype the classpath each time I open the terminal. :)

limc
  • 39,366
  • 20
  • 100
  • 145
1

By the way, it is not very nice to modify the CLASSPATH environment variable specifically for compiling a project, given that after that, all other projects will inherit this change. This of course stands only if you are globally changing it. If you aren't and instead you are planning to write a little script to build you project, why don't you consider using ant? Good luck!

JoeSlav
  • 4,479
  • 4
  • 31
  • 50
  • yeah but if I use ant, I still need to add the jars to the classpath to run my apps, right ? java -cp ... myApp (or I generate myApp.jar with ant, but this is another thing) – aneuryzm Mar 02 '11 at 21:29
1

You have set the CLASSPATH, but you didn't put it into the environment. So it's a variable, but not quite an environmental variable.

To promote an in-script / in-session variable to an environmental variable, use the command export like so

export CLASSPATH

This promotes the variable to an environmental variable (which will be accessible to any shell that inherits the environment).

Some systems allow the combining of the set and the export. In such systems, you can combine your set command with the export command like so:

export CLASSPATH=<your value here>

The java command only reads the environmental variable CLASSPATH. It can't look into non-environmental variables as those are not inherited from process to process.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

Besides exporting your UNIX environment, use absolute paths. For example, the class path entry: commons-digester-2.1/commons-digester-2.1.jar only works if you are in the parent directory of the commons-digester-2.1 installation directory.

On unix, there should be a common location into which you install your packages. Something like /usr/local, /usr/lib, or /usr/local/lib.

DwB
  • 37,124
  • 11
  • 56
  • 82
0

maybe you want to try to use maven for build you application? It's really easy to setup and it annihilate all problems with dependency management.

also from java 6 you can use wildcards in classpath: set CLASSPATH=my_libs\*;

Dmytro
  • 195
  • 5