制作java包

发布于 2024-08-21 09:59:31 字数 622 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

窗影残 2024-08-28 09:59:31

java编译器将遍历类路径的子目录寻找它需要的包。

因此,您的命令行应如下所示:

javac -cp "." com/david/Greet.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:

javac -cp "." com/david/Greet.java

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.

倒数 2024-08-28 09:59:31

首先,如设置类路径中所述,您当前设置类路径的方式是错误的。类路径条目应该是文件名或目录。因此使用 com.david.greeter.* 没有任何意义。其次,当前目录默认在类路径中:

默认类路径是当前目录。设置 CLASSPATH 变量或使用 -classpath 命令行选项会覆盖该默认值,因此如果要在搜索路径中包含当前目录,则必须包含“.”。在新设置中。

因此,如果您执行 javac (这里是 手册页 顺便说一句)从包含 com 的文件夹中,您不必调整任何内容,只需键入:

javac com/david/Greet.java

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:

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

So if you execute javac (here is the man page by the way) from the folder containing com, you don't have to tweak anything, just type:

javac com/david/Greet.java

And javac will go through the directory tree to find references (e.g. SayHello if you're using it from Greet) 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.

若水微香 2024-08-28 09:59:31

如果您位于包含 com 的文件夹中,请尝试以下操作:

javac -cp . com\david\Greet.java

If you are in the folder containing com, then try this:

javac -cp . com\david\Greet.java
喵星人汪星人 2024-08-28 09:59:31

这是不正确的(正如编译器已经告诉你的那样):

javac -cp“com.david.greeter.*

打开命令 shell 并导航到包含“com”目录的目录。

我认为您确实希望它编译 SayHello.java 和 SayGoodbye.java:

javac -cp 。 com/david/greeter/*.java

这是编译 Greet.java 的方法:

javac -cp 。 com/david/*.java

运行这个:

java -cp 。 com.david.Greet

This is incorrect (as the compiler has already told you):

javac -cp "com.david.greeter.*

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:

javac -cp . com/david/greeter/*.java

This to compile Greet.java:

javac -cp . com/david/*.java

And this to run:

java -cp . com.david.Greet

梦开始←不甜 2024-08-28 09:59:31

“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文