创建我们自己的jar并在另一个项目中使用它 [英] creating our own jar and using it in another project

查看:195
本文介绍了创建我们自己的jar并在另一个项目中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自己的jar,它有一些简单的方法,比如print方法。

I want to create our own jar which has some simple methods like a print method.

我创建了该java项目的jar并将其导入到其他项目中我也在构建路径。

I created the jar of that java project and imported it into an other project I am building path also.

问题是我能够创建驻留在jar中的类的对象但是我无法调用那个方法类。
i我正在使用eclipse 3.4(Eclipse Ganymede(3.4))版本

The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class. i am using eclipse 3.4 (Eclipse Ganymede (3.4))version

推荐答案

听起来好像你是否成功构建了JAR在编译/运行应用程序时,不要将它包含在类路径中。如果使用 -cp -classpath 选项从命令行编译/运行,则可以执行此操作。两者都执行相同的功能。

Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.

编译:

javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java

运行:

java -cp .:PathToMyJar/MyJar.jar MyMainClass

上面的命令将查看当前目录('。')和指定的MyJar.jar文件。在Windows上,您将使用分号分隔类路径中的项目,在Linux / Unix / OS X上,您将使用冒号。

The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.

如果您使用的是IDE,则会需要以某种方式将JAR文件添加到项目的类路径中。如何做到这一点是特定于IDE的。

If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.

这篇关于创建我们自己的jar并在另一个项目中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆