Setting Java Classpath to Load a Class File

后端 未结 2 769
离开以前
离开以前 2021-02-04 17:50

I\'m new to Java, and I\'m unsure how to access a class file located in a specific directory from a separate program jar.

For example, I have an third party jar file loc

2条回答
  •  庸人自扰
    2021-02-04 18:02

    When you use "-jar" then only the Class-Path attribute defined in the META-INF/MANIFEST.MF file inside the jar file will influence the classpath.

    It will also ignore the MyClass argument (or more specifically: interpret it as an argument to the main-class defined in the MANIFEST.MF).

    If you simply want to call a class from that jar call it like this:

    java -cp mainprog.jar:/mylib MyClass
    // or using this one on windows:
    java -cp mainprog.jar;/mylib MyClass
    

提交回复
热议问题