can a class in a jar use a class in a different jar

后端 未结 3 928
失恋的感觉
失恋的感觉 2021-01-24 09:15

I am new to java so not sure I fully understand jar files.

I want to put some common code in a library jar, which I then use from applications that are in different jars

3条回答
  •  故里飘歌
    2021-01-24 10:03

    If the Class-Path attribute of the manifest of the application jar file is

    mylibrary-0.0.1-SNAPSHOT.jar
    

    and the two jar files are in the same directory, then running

    java -jar myapp-0.0.1-SNAPSHOT.jar should work.
    

    You can also set the classpath explicitely, and set the main class explicitely, which is much easier, and doesn't rely on relative paths in the manifest.

    java -cp myapp-0.0.1-SNAPSHOT.jar:mylibrary-0.0.1-SNAPSHOT.jar org.myorg.myapp.Main
    

    You should not use the -classpath and the -jar options in the same command. It's one or the other.

提交回复
热议问题