编译时无需将项目文件放在类路径下
我希望有人能够编译代码,而不必将 CLASSPATH 设置为项目目录。这可能吗?
I want someone to be able to compile the code without then having to set the CLASSPATH to the project directory. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以设置要用于的 类路径编译作为参数传递给编译器而不是设置环境变量。参数为
-classpath
或-cp
。You can set the classpath to be used for the compilation as a parameter passed to the compiler instead of setting an environment variable. The parameter is
-classpath
or-cp
.类路径需要以某种方式设置,尽管现在不建议使用环境变量。通常,人们应该自己编译的项目是通过 Apache Ant 构建脚本或 Maven POM 描述符。这两个构建自动化系统都可以做的不仅仅是编译,但是如果您以前没有使用过它们,则都需要一些时间来学习。然而,这是值得的,因为它们可以为您在各种重复性任务上节省大量时间,并且两者在 Java 项目中都非常常用。
The classpath needs to be set somehow, though using the environment variable is not recommended these days. Typically, projects that people are supposed to compile for themselves are distributed with either an Apache Ant build script or a Maven POM descriptor. Both of these build automation systems can do much more than just compilation, but both require some time to learn if you haven't used them before. However, it is time well spent, as they can save you a lot of time on all kinds of repetitive tasks, and both are very commonly used in Java projects.
使用构建工具,例如 Ant 或 Maven。他们使用一种或另一种项目描述符来为您管理类路径。
Use a build tool, like Ant or Maven. They use a project descriptor of one sort or another to manage the classpath for you.
听起来您想创建一个 .jar
JDK 中的 jar 工具将您的 .class 文件放入存档中。然后,您可以仅引用类路径上的存档或使用 java 可执行文件的 -jar 标志。
如果您为 jar 创建清单,则可以通过在 Windows 和大多数其他桌面环境中单击来使 .jar 运行。
Sounds like you want to create a .jar
The jar tool in the JDK puts your .class files in an archive. Then you can just reference the archive on the classpath or with the -jar flag for the java executable.
If you create a manifest for the jar, you can make the .jar runnable with a click from Windows and most other desktop environments.