Run a JAR file from the command line and specify classpath

后端 未结 5 1194
耶瑟儿~
耶瑟儿~ 2020-11-22 10:44

I\'ve compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled li

5条回答
  •  半阙折子戏
    2020-11-22 11:02

    Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:

    Main-Class: com.test.App
    Class-Path: lib/one.jar lib/two.jar
    

    Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.

    Then execute it with

    java -jar MyJar.jar
    

提交回复
热议问题