5

I'm running Ubuntu and want to execute a Java file from terminal by including multiple jar files.

All my jars are included in tha jar folder.

I tried

javac -cp jar/A.jar: jar/B.jar: jar/C.jar: jar/D.jar MyFile.java

I get below error.

javac: invalid flag: jar/B.jar:
Usage: javac <options> <source files>
use -help for a list of possible option

Can anyone guide how to use multiple jars in classpath ?

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
user3044327
  • 193
  • 1
  • 3
  • 11

2 Answers2

14

Remove the spaces from the classpath and add the current path

javac -cp jar/A.jar:jar/B.jar:jar/C.jar:jar/D.jar:. MyFile.java

Since Java 6 you can use classpath wilcards

javac -cp jar/*:. MyFile.java
Reimeus
  • 158,255
  • 15
  • 216
  • 276
-1

ClassPath set through command prompt will work only for current cmd window. Once you close it and open a new cmd window it will not work. Rather than setting classpath from command prompt, keep related paths to system properties:

For windows:

go to My Computer--> Properties--> Advance System Settings--> Environment Variables--> CLASSPATH--> put your path like this--> path1;path2;path3;. Don't forget to keep . (DOT) at the end.

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28
  • Specifying application specific jars on system level is not a way to go. This is why startup scripts exist which are often a mess, but more flexible. – Peter Sep 21 '18 at 12:09