如何导入包中的jar文件? [英] How to import a jar file which is in a package?
问题描述
我有一个名为 stdlib-package 的jar文件, .class 此jar文件中的文件都在 edu.princeton.cs.introcs 包中
例如,我想使用一个我自己的代码中来自该jar文件的名为 StdDraw 的类
说 E:\ code 是我当前的工作目录。和其他条件一样,我创建了一个子目录 bin \edu \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b 这是我的代码: 然后我把 DrawTest.java 在 src\com\david\test 要编译 DrawTest.java ,我在命令行中输入命令: 但它失败了:包 edu.princeton.cs.introcs.stdlib_package 不存在... 我搜索了很多时间,但没有找到答案 编辑=== 这应该是代码中所需的全部内容: 然后你只需要确保罐子已打开你的班级路径;无论罐子的名称是什么,或者它在哪里都无关紧要。如果它在你的类路径上,Java将加载它,并使用其中文件的位置。 将jar移动到 I have a jar file named stdlib-package, the .class files in this jar file are all in a edu.princeton.cs.introcs package For example, I want to use one class named StdDraw from that jar file in my own codes Say E:\code is my current working directory. like other conditions, I create a subdirectory bin\edu\princeton\cs\introcs to put stdlib-package in Here is my codes: Then I put DrawTest.java in the src\com\david\test To compile the DrawTest.java, I type the commands in command line: But it failed : package edu.princeton.cs.introcs.stdlib_package doesn't exist... I searched for a lot of time, but haven't found the answer EDIT=== This should be all you need in code: Then you only need to make sure the jar is on your class path; it doesn't matter what the name of the jar is, or where it is. If it is on your classpath, Java will load it, and use the location of the files within it. Move the jar into a
这篇关于如何导入包中的jar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
package com.david.test;
import edu.princeton.cs.introcs.stdlib-package。*; //这里有什么问题吗?
公共类DrawTest
{
public static void main(String [] args)
{
StdDraw.line(0.5,0.5,1,0.5) );
}
}
javac -d bin -cp bin \edu \princeton \cs\ introcs\stdlib-package.jar src\com\david\test\DrawTest.java
感谢您的帮助
@ EJP的评论也解决了我的问题。
import edu.princeton.cs.introcs。*
lib
当前目录中的目录,并执行类似
javac -cp lib\stdlib-package的操作。 jar src\com\david\test\DrawTest.javac
package com.david.test;
import edu.princeton.cs.introcs.stdlib-package.*; // any problems here ?
public class DrawTest
{
public static void main(String[] args)
{
StdDraw.line(0.5, 0.5, 1, 0.5);
}
}
javac -d bin -cp bin\edu\princeton\cs\introcs\stdlib-package.jar src\com\david\test\DrawTest.java
Thanks for your help
@EJP's comment solves my question too.import edu.princeton.cs.introcs.*
lib
directory in your current directory, and do something likejavac -cp lib\stdlib-package.jar src\com\david\test\DrawTest.javac