CLASSPATH 101...(在 Windows 上)

发布于 2024-09-16 06:53:56 字数 474 浏览 20 评论 0原文

我是从命令行使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-09-23 06:53:56

我将 Hello.class 文件放入 C:\Temp\a\target

这是错误的。它应该放置在与 .java 文件相同的文件夹中。源代码本身被声明在 package abc; 中,因此,.class 文件实际上应该保存在 \a\b\c 中> 文件夹。

然后,要执行它,只需执行以下操作:

C:\Temp>java -cp . a.b.c.Hello

and I put the Hello.class file inside C:\Temp\a\target

This is wrong. It should be placed in the same folder as the .java file. The source code itself is declared to be in the package a.b.c; so, the .class file should really be kept in \a\b\c folder.

Then, to execute it just do:

C:\Temp>java -cp . a.b.c.Hello
夏花。依旧 2024-09-23 06:53:56

避免将类文件“放置”在任何地方。以下应该有效:

javac -d c:\temp c:\temp\a\b\c\Hello.java
# creates Hello.class in c:\temp\a\b\c
java -cp c:\temp a.b.c.Hello

Avoid "putting" the classfiles anywhere. The following should work:

javac -d c:\temp c:\temp\a\b\c\Hello.java
# creates Hello.class in c:\temp\a\b\c
java -cp c:\temp a.b.c.Hello
离笑几人歌 2024-09-23 06:53:56

扩展 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.

放肆 2024-09-23 06:53:56

Java 将尝试从 target 开始搜索目录结构 a\b\c,正如您所注意到的,它不会工作。

将整个目录移至 target 中,就可以了,它应该如下所示:

 C:\Temp\a\target\a\b\c\Hello.class

您可以使用 -d 选项来编译它,该选项会提高编译器放置类文件的位置。

很多项目结构都是这样的。

C:\whatever\projectname\src
C:\whatever\projectname\classes
C:\whatever\projectname\bin
C:\whatever\projectname\lib
C:\whatever\projectname\doc

这样您就可以随时进入项目目录并输入:

javac -d classes src\*.java 

这将编译 src 目录中的所有源代码并将它们放置在classes 目录中。

然后执行您的程序:

java -cp classes a.b.c.Hello 

您可以选择将所需的 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 in target and as you notice, it wont work.

Move the whole directory into target and you'll be fine, it should look like:

 C:\Temp\a\target\a\b\c\Hello.class

You may compile it with the -d option which tall the compiler where to put the class file.

Many project structures are like this.

C:\whatever\projectname\src
C:\whatever\projectname\classes
C:\whatever\projectname\bin
C:\whatever\projectname\lib
C:\whatever\projectname\doc

That way you can always step on your project directory and type:

javac -d classes src\*.java 

Which will compile all the sources in the src directory and will place them in the classes directory.

Then execute your program:

java -cp classes a.b.c.Hello 

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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文