Java——如何访问(导入)与 src 文件夹位于同一目录的文件夹中的类文件?
在 eclipse 或 netbeans 或 emacs pico 或 microsoft word 或记事本或或或其他什么。谢谢。
在 Eclipse 中,我试图与另一个项目共享一个项目中的包。我做了整个构建路径->链接源-> add source ... 它会创建一个链接目录,但不是作为 src/ 目录中的包,而是作为 src/ 的单独目录同级。所以我陷入困境,想知道,等待解决方案。
In eclipse or netbeans or emacs pico or microsoft word or notepad or or or whatever. Thanks.
In eclipse, I'm trying to share a package in one project w/ another project. I do the whole buildpath->link source -> add source ... and it creates a linked directory but not as package within the src/ dir, instead as a separate dir sibling to the src/. so I'm left stranded, wondering, waiting for a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您指的是没有包的类,则只能从它们向外引用,而不能从包中的类引用没有包声明的类(这是一件非常好的事情)。
即使您想引用默认包中的类,它也会位于您的“src”文件夹中,而不是与 src 文件夹位于同一目录中,所以也许您的意思有所不同?
通常,您的“src”目录将包含一个“com”目录——包树的顶部。因此,如果您在默认包中有一个类,则可以将其放置在与“com”相同的级别,但正如我所说,您不能从其他代码中引用它——您只能从命令行执行未打包的类。
如果您说您希望所有类都位于同一目录或其他目录中,那么我建议将它们全部打包为 .jar 文件。
If you mean classes without a package, you can only refer from them outward, you can't refer from a class that is in a package to a class with no package declaration (A very good thing).
Even if you wanted to reference a class in the default package it would be in your "src" folder, not in the same directory as your src folder, so perhaps you mean something different?
Generally your "src" directory would contain a "com" directory--the top of the package tree. So if you had a class in the default package you would place it at the same level as "com", but as I said you can't refer to it from other code--you can only execute the unpackaged class from the command line.
If you are saying that you want all your classes in the same directory or something--then I recommend packaging them all together as a .jar file.
通常,您会将项目(包含要共享的类)添加到使用项目的构建路径(右键单击>构建路径>配置构建路径)。这允许您在 eclipse 中编译和运行。
如果要创建从一个项目到另一个项目代码所在位置的链接,则必须以以下方式创建链接文件夹:1) 它已经位于 /src/ 文件夹中,或者 2) 可以转换为源文件夹,并可以看到编译类所需的完整包结构。但这并不是大多数 java 项目使用另一个 java 项目输出的方式。
Normally you would add the project (with classes you want to share) to the Build Path (right-click>Build Path>Configure Build Path) of the consuming project. That allows you to compile and run within eclipse.
If you want to create a link from one project to the place where the other project code lives, you have to create your linked folder in such a way that 1) it's already in the /src/ folder or 2) can be turned into a source folder and can see the full package structure necessary to compile your classes. But that is not how most java projects consume the output of another java project.