制作java包
我的 Java 类组织变得有点混乱,所以我要回顾一下我在 Java 学习中跳过的东西:类路径。我无法安静地将心爱的类编译到我为它们创建的包中。这是我的文件夹层次结构:
.
com/
david/
Greet.java
greeter/
SayHello.java
SayGoodbye.java
其中 SayHello 的构造函数仅打印“hello”,SayGoodbye 的构造函数打印“Goodbye”,而 Greet 的 main 方法仅创建这两个对象。 SayHello 的顶部是 com.david.greeter 包;同样,SayGoodbye 和 Greet 的包是 com.david;
在greeter文件夹中,我能够编译两个java文件,但是如果我转到当前目录(包含com的目录)并执行javac -cp“com.david.greeter.*”com/david/Greet.java,它会说它找不到类并且说包 com.david.greeter 不存在。我还尝试过手动设置 $CLASSPATH 。
我在这里已经束手无策了,Stackoverflow(就像我在这里发帖时通常的情况一样)。你们有人知道我做错了什么吗?
My Java classes organization has gotten a little messy so I'm going over something I've skipped in my Java learning: the classpath. I can't quiet get beloved classes to compile in the packages I have created for them. Here's my folder hierarchy:
.
com/
david/
Greet.java
greeter/
SayHello.java
SayGoodbye.java
Where SayHello's constructor just prints "hello" and SayGoodbye's prints "Goodbye" and Greet's main method just creates those two objects. At the top of SayHello is package com.david.greeter; and likewise with SayGoodbye and Greet's is package com.david;
In the greeter folder I was able to compile both java files but if I go to the current directory (the directory that holds com) and do javac -cp "com.david.greeter.*" com/david/Greet.java it says it can't find the classes as well as saying package com.david.greeter doesn't exist. I've also tried setting the $CLASSPATH manually.
I'm at my wit's end here, Stackoverflow (as I normally am when I post here). Do any of you know what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
java编译器将遍历类路径的子目录寻找它需要的包。
因此,您的命令行应如下所示:
当编译器在编译 Greet.java 时看到对 com.david.greeter.SayHello 的引用时,它将从类路径中的目录开始,并遍历层次结构查找所需的包。
The java compiler will traverse the sub-directories of the classpath looking for the packages it needs.
So, your command line should be as follows:
When the compiler sees a reference to com.david.greeter.SayHello while compiling Greet.java it will start with the directory in the classpath and traverse the hierarchy looking for the package it needs.
首先,如设置类路径中所述,您当前设置类路径的方式是错误的。类路径条目应该是文件名或目录。因此使用
com.david.greeter.*
没有任何意义。其次,当前目录默认在类路径中:因此,如果您执行
javac
(这里是 手册页 顺便说一句)从包含com
的文件夹中,您不必调整任何内容,只需键入:javac
就会出现通过目录树查找引用(例如,如果您在Greet
中使用它,则使用SayHello
)并编译它们。顺便说一句,如果您必须设置类路径,请不要使用
$CLASSPATH
环境变量,这在大多数情况下只是一个不好的做法,更喜欢使用-cp
代码>选项。First, as documented in Setting the Classpath, the way you're currently setting your class path is wrong. Class path entries should be filename or directory. So using
com.david.greeter.*
doesn't make any sense. Second, the current directory is in the class path by default:So if you execute
javac
(here is the man page by the way) from the folder containingcom
, you don't have to tweak anything, just type:And
javac
will go through the directory tree to find references (e.g.SayHello
if you're using it fromGreet
) and compile them too.And by the way, if you have to set the class path, don't use the
$CLASSPATH
environment variable, this is just a bad practice in most case, prefer the-cp
option.如果您位于包含 com 的文件夹中,请尝试以下操作:
If you are in the folder containing com, then try this:
这是不正确的(正如编译器已经告诉你的那样):
打开命令 shell 并导航到包含“com”目录的目录。
我认为您确实希望它编译 SayHello.java 和 SayGoodbye.java:
这是编译 Greet.java 的方法:
运行这个:
This is incorrect (as the compiler has already told you):
Open a command shell and navigate to the directory that contains the "com" directory.
I think you really want this to compile SayHello.java and SayGoodbye.java:
This to compile Greet.java:
And this to run:
“com”目录不应该是当前目录,它应该是当前目录的子目录。您需要上一级并再次启动。此时不需要额外关心类路径。
The "com" directory should not be current, it should be a child directory to current. You need step one level upper and launch again. No extra care about classpath should be needed at this point.