Java command line noclassdeffounderror Solution

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.