CLASSPATH 101...(在 Windows 上)
我是从命令行使用 Java 的新手,但我不明白。我阅读了之前的 CLASSPATH 问题,但仍然没有解决我的问题。
我在 C:\Temp\a\b\c 中有以下类
package a.b.c;
public class Hello
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
包名称是故意的。 我编译得很好,并将 Hello.class 文件放入 C:\Temp\a\target 中
现在在命令行中,我转到 C:\Temp\ 并执行以下命令:
java -cp .\a\target a.b.c.Hello
它抱怨它找不到类 abcHello
谢谢进步!!
I'm new to working with Java from the command line and I don't get it. I read previous CLASSPATH questions but still didn't get my problem to work.
I have the following class in C:\Temp\a\b\c
package a.b.c;
public class Hello
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
The package name is intentional.
I compiled it fine and I put the Hello.class file inside C:\Temp\a\target
Now in the command line I go to C:\Temp\ and execute the following:
java -cp .\a\target a.b.c.Hello
It complains that it cannot find the class a.b.c.Hello
Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是错误的。它应该放置在与
.java
文件相同的文件夹中。源代码本身被声明在package abc;
中,因此,.class
文件实际上应该保存在\a\b\c
中> 文件夹。然后,要执行它,只需执行以下操作:
This is wrong. It should be placed in the same folder as the
.java
file. The source code itself is declared to be in thepackage a.b.c;
so, the.class
file should really be kept in\a\b\c
folder.Then, to execute it just do:
避免将类文件“放置”在任何地方。以下应该有效:
Avoid "putting" the classfiles anywhere. The following should work:
扩展 BalusC 的观点:类路径定义了一个“根”。当 java 寻找您的类时,它将从类路径中的每个根(或 jar)开始,并向下钻取目录以匹配包结构。您仍然需要在与其包名称匹配的目录结构中创建类。在您的情况下,要执行
java -cp .\a\target abcHello
您会将文件移动到
.\a\target\a\b\c\Hello.class
几年前,我也发现这令人困惑。
To expand on BalusC's point: the classpath defines a "root". When java is looking for your classes, it will start at each root (or jar) in your class path and drill down through the directories to match the package strucutre. You still need to have you class in a directory structure that matches its package name. In your case, to execute
java -cp .\a\target a.b.c.Hello
you would move the file to
.\a\target\a\b\c\Hello.class
Years ago, I too found this baffling.
Java 将尝试从
target
开始搜索目录结构a\b\c
,正如您所注意到的,它不会工作。将整个目录移至
target
中,就可以了,它应该如下所示:您可以使用 -d 选项来编译它,该选项会提高编译器放置类文件的位置。
很多项目结构都是这样的。
这样您就可以随时进入项目目录并输入:
这将编译 src 目录中的所有源代码并将它们放置在classes 目录中。
然后执行您的程序:
您可以选择将所需的 jar 放在
lib
中,这对于小型程序(<10 个 src 文件和 2 - 3 个 jar 库)来说效果很好,如果它超出了这个范围,您可能可以使用IDE 或 ant
遵循此项目结构的好处是,某些 IDES(如 IntelllJ idea)在您创建新项目时可以轻松选择它们。您选择“从现有源创建项目”,然后可以从那里继续。
我非常喜欢在命令行进行编译和编辑!
Java will try to search for a directory structure
a\b\c
from starting intarget
and as you notice, it wont work.Move the whole directory into
target
and you'll be fine, it should look like:You may compile it with the -d option which tall the compiler where to put the class file.
Many project structures are like this.
That way you can always step on your project directory and type:
Which will compile all the sources in the
src
directory and will place them in theclasses
directory.Then execute your program:
You may optionally place required jars in
lib
This works pretty fine for small programs ( < 10 src files and 2 - 3 jar libraries ) If it grows beyond that, you could probably use an IDE or ant
The good thing about following this project structure is that some IDES ( as IntellJ idea ) just pick them very easily when you create a new project. You select "Create project from existing sources" and then you can continue from there.
I like compiling and editing at the command line a lot!!