1

I've been looking for an answer, but still a little confused...

In eclipse I just configure the build path of the project and add the jar files that I need, and everything works just fine. When I try to run my programs via Bash, it's not finding my imports.

How do I run my Java programs in bash or on another server with the required jar files?

Thanks

Dylan
  • 499
  • 1
  • 10
  • 21

1 Answers1

3

You specify the CLASSPATH. Either through the environment like

export CLASSPATH="a.jar:b.jar"
java com.stackoverflow.Main

or explicitly via the -cp command line option like,

java -cp a.jar:b.jar com.stackoverflow.Main
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I just call export CLASSPATH="a.jar:b.jar" in the same directory my java programs are? – Dylan Nov 15 '15 at 02:29
  • You have to put the path to `a.jar` and `b.jar` if they're in different directories from your java programs. – Elliott Frisch Nov 15 '15 at 02:32
  • I cant just add an entire folder of jar files all at once? It seems very unintuitive to add them all individually. – Dylan Nov 15 '15 at 02:38
  • 1
    @Dylan See [this question](http://stackoverflow.com/questions/4729863/using-bash-how-do-you-make-a-classpath-out-of-all-files-in-a-directory). – Elliott Frisch Nov 15 '15 at 02:41
  • Thanks! That's exactly what I was looking for. – Dylan Nov 15 '15 at 02:43
  • Interesting, I added the jar files to the class path and it worked. A few hours later I tried it again and I'm getting an error that the packages I'm trying to import are not found. Did the exact same thing and made no changes... – Dylan Nov 17 '15 at 01:17
  • Make sure your CLASSPATH is set as you expect (perhaps `echo` it) and check the jars are actually in the folder you're specifying. – Elliott Frisch Nov 17 '15 at 01:23
  • I did echo ${CLASSPATH} and it looks correct. one of the 6 files in the path are ./required_jar_files/gs-algo-1.3-javadoc.jar: and I'm using this command: CLASSPATH=$(find "." -name '*.jar' | xargs echo | tr ' ' ':'):. – Dylan Nov 17 '15 at 01:43
  • 1
    @Dylan Why? From that linked question, you want `java -cp "./required_jar_files/*"` – Elliott Frisch Nov 17 '15 at 01:44
  • Still no luck. echo cp is correct. Would it be possible for us to chat? – Dylan Nov 17 '15 at 01:48
  • I don't really have time for chat right now, but make sure you're in the correct folder (since you're using relative paths). – Elliott Frisch Nov 17 '15 at 01:51