Windows7 7 / Eclipse 中的 PATH 和 CLASSPATH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2858463/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:45:12  来源:igfitidea点击:

PATH and CLASSPATH in Windows7 7 / Eclipse

javaeclipse

提问by Richard Knop

So I would like to set PATH and CLASSPATH system variables so I can use javac and java commands in the command line. I can just compile and run java programs in eclipse but I would also like to be able to run them through command line.

所以我想设置 PATH 和 CLASSPATH 系统变量,以便我可以在命令行中使用 javac 和 java 命令。我只能在 eclipse 中编译和运行 java 程序,但我也希望能够通过命令行运行它们。

This is where I have Java installed:

这是我安装 Java 的地方:

C:\Program Files (x86)\Java
    jdk1.6.0_20
    jre6

And this is where eclipse stores my Java projects:

这是 Eclipse 存储我的 Java 项目的地方:

D:\java-projects
    HelloWorld
        bin
            HelloWorld.class
        src
            HelloWorld.java

I have set up the PATH and CLASSPATH variables like this:

我已经像这样设置了 PATH 和 CLASSPATH 变量:

PATH: C:\Program Files (x86)\Java\jdk1.6.0_20\bin
CLASSPATH: D:\java-projects

But it doesn't work. When I write:

但它不起作用。当我写:

java HelloWorld

Or:

或者:

java HelloWorld.class

I get error like this:

我得到这样的错误:

Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorld

The error is longer, that's just the first line.

错误更长,那只是第一行。

How can I fix this? I'm mainly interested to be able to run compiled .class programs from the command line, I can do compiling in the eclipse.

我怎样才能解决这个问题?我主要对能够从命令行运行编译的 .class 程序感兴趣,我可以在 eclipse 中进行编译。

回答by Michael

Your classpath should point to "D:\java-projects\HelloWorld\bin".

您的类路径应该指向“D:\java-projects\HelloWorld\bin”。

Alternatively, you can specify your classpath with the "-cp" parameter instead of using an environment variable:

或者,您可以使用“-cp”参数而不是使用环境变量来指定类路径:

java -cp D:\java-projects\HelloWorld\bin HelloWorld

java -cp D:\java-projects\HelloWorld\bin HelloWorld

回答by Jon Skeet

You need to set the classpath to

您需要将类路径设置为

d:\java-projects\bin

Currently you haven't got the "bin" part.

目前你还没有“bin”部分。

Note that java HelloWorld.classwill never work - it's after the classname, not the filename.

请注意,java HelloWorld.class这永远不会起作用 - 它在名之后,而不是文件名之后。

回答by Petar Minchev

Your CLASSPATHdoesn't point to the directory where the classfile is located.

CLASSPATH没有指向class文件所在的目录。

回答by Matthieu BROUILLARD

Your classpath should point to D:/java-projects/bin

你的类路径应该指向 D:/java-projects/bin

But instead I would invite you to use some build tools in order to manage your projects. Have a look at ANT, Maven, Gradle.

但相反,我会邀请您使用一些构建工具来管理您的项目。看看 ANT、Maven、Gradle。

回答by Jesper

Your PATHenvironment variable should include the bindirectory of your JDK installation directory. So you should add C:\Program Files (x86)\Java\jdk1.6.0_20\binto your PATH.

您的PATH环境变量应包含binJDK 安装目录的目录。所以你应该添加C:\Program Files (x86)\Java\jdk1.6.0_20\bin到你的PATH.

It's not advisable to set a permanent CLASSPATHenvironment variable, because it is a global setting that affects all Java programs that you run on your machine. If you don't set CLASSPATH, Java will by default only look in the current directory.

不建议设置永久CLASSPATH环境变量,因为它是一个全局设置,会影响您在机器上运行的所有 Java 程序。如果没有设置CLASSPATH,Java 将默认只在当前目录中查找。

Instead of setting CLASSPATH, use the -cpor -classpathoption on the javacommand, for example:

在命令上CLASSPATH使用-cpor-classpath选项而不是设置,java例如:

java -cp D:\java-projects\HelloWorld\bin HelloWorld

If you don't want to type that every time you want to run your program, put the command in a batch file (for example hello.bat), which you can then run by simply typing hello.

如果不想在每次运行程序时都键入该命令,请将命令放入批处理文件(例如hello.bat)中,然后只需键入hello.