开发者

PATH and CLASSPATH in Windows7 7 / Eclipse

开发者 https://www.devze.com 2022-12-31 09:59 出处:网络
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 abl

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.

This is where I have Java installed:

开发者_如何学编程
C:\Program Files (x86)\Java
    jdk1.6.0_20
    jre6

And this is where eclipse stores my Java projects:

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

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

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.


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

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

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


You need to set the classpath to

d:\java-projects\bin

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

Note that java HelloWorld.class will never work - it's after the class name, not the file name.


Your CLASSPATH doesn't point to the directory where the class file is located.


Your classpath should point to 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.


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

It's not advisable to set a permanent CLASSPATH environment 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.

Instead of setting CLASSPATH, use the -cp or -classpath option on the java command, for example:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号