exception in thread 'main' java.lang.NoCla

2020-01-25 05:45发布

The following program is throwing error:

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello World!"); 
    }
}

CLASSPATH C:\Program Files\Java\jdk1.6.0_18\bin\

Path C:\Program Files\Java\jdk1.6.0_18\bin\

JAVAHOME C:\Program Files\Java\jdk1.6.0_18\bin

Can you please tell me the root cause?

22条回答
走好不送
2楼-- · 2020-01-25 05:47

The CLASSPATH variable needs to include the directory where your Java programs .class file is. You can include '.' in CLASSPATH to indicate that the current directory should be included.

set CLASSPATH=%CLASSPATH%;.
查看更多
家丑人穷心不美
3楼-- · 2020-01-25 05:47

If you want to 'compile and execute' any java file that you have created using any IDE(like eclipse), just run the below commands:

Compile: javac Users\dhiraj01\workspace\Practice\src\PracticeLogic\Logics.java

Execute: java -classpath Users\dhiraj01\workspace\Practice\src\ PracticeLogic.Logics

查看更多
爷的心禁止访问
4楼-- · 2020-01-25 05:48

if your Program.java is in "src/mypkg/subpkg/" directory:

go to "src" dir

Then to compile use "javac mypkg/subpkg/Program.java"

To run use "java mypkg.subpkg.Program.class"

查看更多
够拽才男人
5楼-- · 2020-01-25 05:50

Your CLASSPATH needs to know of the location of your HelloWorld class also.

In simple terms you should append dot . (means current directory) in the CLASSPATH if you are running javac and java commands from DOS prompt.

查看更多
成全新的幸福
6楼-- · 2020-01-25 05:51

Run it like this:

java -jar HelloWorld.jar
查看更多
闹够了就滚
7楼-- · 2020-01-25 05:51

I had the same problem, and stumbled onto a solution with 'Build Main Project F11'. The ide brought up an "option" that I might want to uncheck 'Compile on Save' in the Build > Compiling portion of the Project configuration dialog. Unchecking 'Complile on Save' and then doing the usual (for me) 'Clean and Build' did the trick for me.

查看更多
登录 后发表回答