0

Suppose I have two jar files (with classes inside) and a java file:

name1.jar
name2.jar
code.java

As said at How to use classes from .jar files?, if I wanted to import name1.jar, I could add it to my CLASSPATH, and run

javac -cp '.:name1.jar' code.java

every time I wanted to import name1.jar. However, how would I compile the java code and import both jar files, not just name1.jar?

Community
  • 1
  • 1
covariance
  • 6,833
  • 7
  • 23
  • 24

3 Answers3

1

try this

javac -cp name1.jar:name2.jar code.java

note that if you are in Windows path separator should be ;

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

The java command can also define the classpath using the -cp flag, which is just a shortcut for the -classpath flag.

(1)javac -cp "/yourPath/name1.jar;/yourPath/name2.jar;" code.java

(2)javac -classpath "/yourPath/name1.jar;/yourPath/name2.jar;" code.java

T8Z
  • 691
  • 7
  • 17
0
java -cp name1.jar:name2.jar:name3.jar code.java arg1 arg2 arg3

this code runs code.java class with jars (name1,name2,name3) -cp used to ignore any runnable jar or other main() and focus on run code.java with arguments arg1 arg2 arg3

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
LonIslam
  • 15
  • 4