Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

java - Could not find main class HelloWorld

I installed Java 1.7.0 in the following folder C:Program FilesJava. My operating system is Windows XP(Version 2002) with Service pack 3.

The environment variables which I set are:

  • CLASSPATH : C:Program FilesJavajdk1.7.0jrelib t.jar;

  • Path : C:Program FilesJavajdk1.7.0in;

  • JAVA_HOME : C:Program FilesJava;

I have presented here the class names which are in my system.

Next I wrote a program, HelloWorld.java:

import java.io.*;

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

When I am compiling using javac HelloWorld.java it is compiling fine.

But after I issue java HelloWorld I am encountering the below error:

Error: Could not find main class HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:198)

Caused by: java.lang.ClassNotFoundException: HelloWorld
        at java.net.URLClassLoader$1.run(URLClassLoader.java:299)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:288)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:287)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:422)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:355)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:195)

After a bit of searching around, I found that may be something wrong in the environment variable. I tried to play with that but no luck.

I even RESTARTED the machine and then again I tried to run but with same fate.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Tell it where to look for you class: it's in ".", which is the current directory:

java -classpath . HelloWorld

No need to set JAVA_HOME or CLASSPATH in this case


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...