1

I'm a little lost here. We were given a jar file which contained 3 different .class files in it. I successfully built the path to the jar file, but I don't know how to use them. The class files obviously contain methods that can be used, but I've never worked with a jar file before so I don't know how to use them. I'm not sure if I have to import them somehow, how to instantiate them or anything. I have searched for tutorials to no avail.

All I'm looking for is some guidance so I can get moving on this project. If it is a case where I have to import them somehow how would I do it? For example, I have WikiEdits.class contained in WikiEdits.jar. The name of my main class is P2. Can someone show me a brief example of how this works?

4

3 回答 3

1

如果您使用的是 IDE,请将 jar 添加到您的类路径中。

然后,使用它的 java 类看起来像这样:

package p2;
import blah.WikiEdits;  //references a class in the jar

public final class P2 {  //(this is a strange name for a class, by the way)
  public static void main(String... args){
     //builds a new object of the given class
     WikiEdits thing = new WikiEdits();
  }
}

如果您使用命令行,这些示例可能会有所帮助: http ://www.javapractices.com/topic/TopicAction.do?Id=243

于 2013-09-28T16:06:50.080 回答
1

您需要将 WikiEdits.jat 添加到您的路径项目中,然后导入并实例化该类。

import WikiEdits

P2 p = new P2();
p.somemethod();

静态类:

WikiEdit.someMethod();
于 2013-09-28T16:01:59.103 回答
0

在您的 java 类中添加importsjar 中的相关内容。然后从命令行,您可以通过定义正确的类路径,使用 jar 中的类来编译和运行您的类:

汇编

在窗户上:

javac -cp .;pathtoyourjar YourClass.java

在 Linux 上:

javac -cp .:pathtoyourjar YourClass.java

执行

在窗户上:

java -cp .;pathtoyourjar YourClass

在 Linux 上:

java -cp .:pathtoyourjar YourClass

如果您使用的是 Eclipse,请按照此链接了解将 jar 添加到项目的步骤:

http://www.cs.duke.edu/courses/cps004g/fall05/assign/final/addlibrary.html

于 2013-09-28T16:03:48.780 回答