如何在Java中导入JAR和包 [英] How do I import JARs and Packages in Java

查看:173
本文介绍了如何在Java中导入JAR和包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java比较陌生。我一直在使用Eclipse做一些工作,但我想回到基础,只需将文本编辑器与JDK结合使用。我现在面临的问题是Eclipse和其他IDE隐藏了很多基本的东西,这些东西对于了解和完全理解非常重要。这就是我要做的事情:

I'm relatively new to Java. I've been using Eclipse to do some work but I want to get back to basics and just use a text editor in conjunction with the JDK. The problem I'm now having is that Eclipse and other IDEs hide away a lot of fundamental stuff which is very important to know and fully understand. This is what I'm trying to do:


  • 我创建了一个名为C:\ Java Project的目录我在其下创建了3个子文件夹,Project1,Project2和SharedJars。 Project1和Project2都有SubDirs,如类和源。 Poject1源.java文件位于c:\ Java项目\ Project1 \source \ com \ myApp

  • I've created a directory called "C:\Java Projects", under which I have created 3 sub-folders, Project1, Project2 and SharedJars. Project1 and Project2 both have SubDirs like classes and source. The Poject1 source .java files live in "c:\Java Projects\Project1\source\com\myApp"

Project1和Project2是使用Log4J JAR的包,它位于SharedJars文件夹中。在Eclipse世界中,我可以设置一些东西告诉Eclipse我的项目将使用哪些JAR,然后执行像导入org.apache.log4j.Logger这样的工作正常。但是我很难让这个工作。

Both Project1 and Project2 are packages which use the Log4J JAR which lives in the SharedJars folder. In the Eclipse world, I could set something which told Eclipse which JARs my Project will use and then do something like import org.apache.log4j.Logger which worked fine. But I'm struggling to get this working.

我已经设置了CLASSPATH环境变量,如C:\ Java Projects \SharedJars \ log4j-1.2.15.jar

I've set the CLASSPATH environment variable like "C:\Java Projects\SharedJars\log4j-1.2.15.jar"

然后我执行以下操作:

cd Project1 \source

javac -d .. \classes com \ myApp \ *

这会产生一大堆相关错误,例如

This produces a whole bunch of related errors like

拿起_JAVA_OPTIONS:-Duser.home =C:\ Java项目


com \ myApp \ Monitor.java:11:找不到符号

符号:class记录器

位置:类com.myApp.Monitor

私有静态Logger LOG;

一些问题:
1)我还需要导入org.apache.log4j.Logger吗?
2)如果是这样,是什么决定了包的FQDN?
3)我是否需要在特定目录中才能运行javac?目前我要进入Project1的源目录(java / bin已经在我的PATH中)

Some questions: 1) Do i still need to import org.apache.log4j.Logger? 2) If so, what determines the FQDN of the package? 3) Do I need to be in a specific directory in order to run javac? Currently i'm going into the source directory of Project1 (the java/bin is in my PATH already)

对于这些愚蠢的问题感到抱歉。我已经浏览了很多网站,但很多都没有涵盖这些基础知识。我希望这是有道理的。

Sorry for these silly questions. I've trawled through so many websites but many do not cover the very basics. I hope this makes sense.

Rgds
John

Rgds John

推荐答案

欢迎来到SO。

回答这个问题:


我是还需要导入org.apache.log4j.Logger吗?

Do i still need to import org.apache.log4j.Logger?

是的,你总是需要使用import指令你要使用的任何类。 Java除非你告诉它,否则即使它在类路径上也不会加载任何东西。

Yes you always need to use import directives any class you want to use. Java won't load anything even if it is on the classpath unless you tell it to.


如果是,那么什么决定了它的FQDN package?

If so, what determines the FQDN of the package?

在jar中,.class文件位于子目录中,如下所示: org / apache / log4j ... 等。这就是确定FQDN的方式,它基本上用作命名空间构造,因此你可以拥有两个或多个同名的类 - 这也是Java不能的原因加载类路径上的所有内容。如果确实如此,它将无法消除歧义并找出你想要的类。

Inside the jar, the .class files are located in subdirectories like this: org/apache/log4j ... etc. This is how the FQDN is determined and it is used basically as a namespace construct so you can have two or more classes of the same name - which is also why Java can't load everything on the classpath. If it did, it would have no way to disambiguate and work out which class you wanted.


我是否需要在特定目录中为了运行 javac

否。你可以从任何地方运行它,只要你有类路径,就可以访问你的依赖项。如果在命令行中指定,则它可以是相对类路径,如果在环境变量中,则可以是绝对路径。同样,您不需要在特定目录中使用 java ,但您确实需要它才能到达正确的位置。

No. You can run this from anywhere provided the classpath, from whereever you are, can get to your dependencies. It could be a relative classpath if specified on the command line or absolute if in an environment variable. Likewise you don't need to be in a specific directory to use java but you do need it to be able to get to the correct location.

这篇关于如何在Java中导入JAR和包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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