将 .java 文件导入 Netbeans 中的 Java 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26577202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
import .java file to Java project in Netbeans
提问by Turo
I'm fresh to Java and cannot get this right.
我对 Java 很陌生,无法做到这一点。
I've got only a .java file from a Java study book, that represents a Java project. There seems to be no option to input such file from Netbeans interface, that would automatically compile corresponding class and enable project to run.
我只有一个来自 Java 学习书籍的 .java 文件,它代表一个 Java 项目。似乎没有选项可以从 Netbeans 接口输入这样的文件,它会自动编译相应的类并使项目能够运行。
I've spend a long time to figure this out, copying files manually, changing the automatically created class names and packages, moving this around - but I don't find a solution.
我花了很长时间来解决这个问题,手动复制文件,更改自动创建的类名和包,移动它 - 但我没有找到解决方案。
If someone can help - this will be great.
如果有人可以提供帮助 - 这会很棒。
Pawel
帕维尔
回答by Turo
It seems the issue was following. .java file should be placed in "src" directory within the Project folder, rather than straight in the Project folder.
似乎问题正在发生。.java 文件应该放在 Project 文件夹内的“src”目录中,而不是直接放在 Project 文件夹中。
So if say "Project" is one's destination folder, then one can:
因此,如果说“项目”是一个人的目标文件夹,那么您可以:
- choose from menu: New Project-Java-Java project from existing resources - indicate path to the ...\Project folder
- the next step is to choose "Source Packages Folders" and here the path should lead to ...\Project\src .If the file .java is correctly placed there, the creator will indicate below that .java file is included to the project
- 从菜单中选择:从现有资源中新建 Project-Java-Java 项目 - 指明 ...\Project 文件夹的路径
- 下一步是选择“Source Packages Folders”,这里的路径应该指向 ...\Project\src 。如果文件 .java 正确放置在那里,创建者将在下面指出 .java 文件包含在项目中
If the code is right after finishing the creator and compiling it we get a compiled class Project.class and we can run it :)
如果代码在完成创建者并编译之后是正确的,我们会得到一个已编译的类 Project.class,我们可以运行它:)
Not sure if there is some work around to this in NetBeans, as it's not super intuitive, but I might not have found a better option.
不确定 NetBeans 中是否有解决此问题的方法,因为它不是非常直观,但我可能找不到更好的选择。
回答by Mark Orimoloye
I too was looking for File > "Import File", because there is an "Import Project" option. It appears dragging and dropping the java file into Source Package is just as easy. :)
我也在寻找文件>“导入文件”,因为有一个“导入项目”选项。似乎将 java 文件拖放到 Source Package 中同样容易。:)