从命令行编译.java文件 - 外部库,类路径 [英] Compiling .java files from the command line - external libraries, classpath
问题描述
我在Eclipse做了一个项目,现在试图确保.java文件从命令行编译。我一直在尝试使用 javac * .java
在所有我的.java文件的文件夹中编译。但是,这导致由于引用外部库(Joda-time)中的类引起的错误。我有以下的.classpath文件,Eclipse为该项目,但不知道该怎么做。
< ?xml version =1.0encoding =UTF-8?>
< classpath>
< classpathentry kind =srcpath =src/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER / org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType / JavaSE-1.7/>
< classpathentry kind =libpath =libs / joda-time-2.2.jar/>
< classpathentry kind =outputpath =bin/>
< / classpath>
我试着用编译javac -classpath * .java
但这只会产生更多的错误。我的源文件在src文件夹内的包文件夹中。我在哪里放我的classpath文件和我的joda-time-2.2.jar文件,我怎么得到一切编译?
[edit]我使用Windows 7
根据您的 .classpath
javac -d bin -cp libs / joda-time-2.2.jar src / your / package / *。 $ b
这假设您在项目的目录中运行它。
-d
标志告诉 javac
在哪里放置输出文件。 -cp
标志是 -classpath
的缩写。我得到这些参数基于 .classpath
文件在你的问题。
一般来说,你可以弄清楚什么是需要阅读并理解 javac
输出中的错误。我打赌你有很多无法找到符号
错误,因为符号不在你的类路径,所以 javac
不可能知道如何找到它们。
了解如何构建项目非常重要,它是基础知识和程序员的核心竞争力。 p>
一旦你理解了如何手动构建,我建议调查构建工具,最好是Maven,或者Ant,使得在命令行上构建更容易。大多数Java项目使用这些工具在命令行和自动化构建系统,持续集成等方面构建他们的产品。
I made a project in Eclipse and am now trying to ensure that the .java files compile from the command line. I've been trying to compile using javac *.java
in the folder with all my .java files. However, this results in errors due to a reference to a class from an external library, Joda-time. I have the following .classpath file that Eclipse made for the project but don't know what to do with it.
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="libs/joda-time-2.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
I tried compiling with javac -classpath *.java
but this only generates more errors. My source files are inside a package folder inside a 'src' folder. Where do I put my classpath file and my joda-time-2.2.jar file, and how do i get everything to compile?
[edit] I'm using Windows 7
Judging by your .classpath
file, this probably works:
javac -d bin -cp libs/joda-time-2.2.jar src/your/package/*.java
This assumes you run it in your project's directory.
The -d
flag tells javac
where to put the output files. The -cp
flag is a shorthand for -classpath
. I got these parameters based on the .classpath
file in your question.
In general you can figure out what is needed by reading and understanding the errors in the output of javac
. I bet you got many cannot find symbol
errors at first, because the "symbols" were not on your classpath, so javac
could not possibly know how to find them.
It is very important to understand how to build your projects, it is fundamental knowledge and a core competency of programmers.
Once you understand how to build manually like this, I recommend to look into build tools, preferably Maven, alternatively Ant that make building on the command line much easier. The majority of Java projects use these tools to build their products on the command line and in automated build systems, continuous integration, and so on.
这篇关于从命令行编译.java文件 - 外部库,类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!