When studying ant today, errors are always reported when ant is used to run Java.
Exception in thread "main" java.lang.NoClassDefFoundError: HelloworldCaused by: java.lang.ClassNotFoundException: Helloworld at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)Could not find the main class: Helloworld. Program will exit.
Path and classpath are configured as follows:
Path configuration: % java_home % \ binclasspath configuration: % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ tools. Jar
In addition, in DOS, the Java and javac commands are displayed normally.
When Java-version is used, javac-version displays the same version, but an error is still reported.
Later, I saw an article on the Internet and modified the classpath configuration as prompted. The changes are as follows:
.;%JAVA_HOME%\lib\dt.jar.;%JAVA_HOME%\lib\tools.jar
Run the Java command and the error disappears.
Originally, "." indicates the current path, which is the default value. Once the environment variable classpath path is set, the original default value is overwritten. If Java-classpath is used, it overwrites the classpath value set by the environment variable.
Therefore, when setting the environment variable classpath, do not forget to add. To add the default value, it will be overwritten by default.
Note that when running a Java class using a Java command, you must add the package name before the class if there is a package name. Even when ant is used for running.