简体   繁体   中英

Compile java file with two jars from command line?

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?

try this

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

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

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

java -cp name1.jar:name2.jar:name3.jar code.java arg1 arg2 arg3

此代码运行带有jars(name1,name2,name3)的code.java类-cp用于忽略任何可运行的jar或其他main()并专注于带有参数arg1 arg2 arg3的运行code.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM