Hi Everyone

I want to make my code run from command prompt

I am using window 7 and JDK 6
Following are steps I did:
1) Created a Text file Example.txt. Changed the file extension to Example.java and placed it under C:\myWork
2) Open command prompt type 'dir' to see all the file in 'myWork'.
3) created a class file as Example.class from exmaple.java by javac Example.java
4) Finally on running command 'java Example'. it does work as desired as it does not print "hello world" and shows an exception "Exception in thread "main" NoClassDefFoundError: Example
Caused by: java.lang.ClassNotFoundException: Example

I dont undertsand this exception is occuring ..:(. How should i Run it successfully??? what is that i am doing wrong or missing something
Please Help!
Thanks in advance!!!!!!!!!!

Dani AI

Generated

Most likely causes of the NoClassDefFoundError when launching from the Windows command prompt are a package/name mismatch, running from the wrong working directory, or a broken environment variable (PATH/CLASSPATH). was right to ask about package statements and working directory, was right to check the JDK installation, and ’s follow-up shows the error is often resolved by fixing system environment variables.

Quick checklist to diagnose and fix (run these from the project root):

  • Confirm the compiled class file exists:

    dir /s /b *.class
  • If the Java source declares a package (for example package com.example;), run the JVM from the directory above the com folder and use the fully qualified class name. Example runtime form:

    java -cp . com.example.Main
  • If there is no package, ensure the current directory is on the classpath (see environment checks below) or run with -cp ..

Environment checks and commands (Windows):

where java
where javac
java -version
javac -version
echo %CLASSPATH%

Compare where/version outputs to ensure the same JDK/JRE is used for compile and run. A persistent CLASSPATH that omits . or points to wrong libraries will cause this error; unset or correct it rather than relying on a global CLASSPATH. Windows-specific gotchas: files renamed in Explorer may still have a hidden .txt extension, and PATH may point to a JRE-only install. Best practice: avoid a global CLASSPATH, compile with javac -d . when using packages, and run using an explicit -cp so the runtime finds your classes.

Recommended Answers

All 7 Replies

check list for your problem
1. did you put any package statements in your java file?
2. are compiling and running on the same directory ?

please check those two conditions (these two are the basic cause of your problem)

Example.java contains:

class Example {
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}

yes I am compiling and running in same directory

Are you sure your commnad prompt says like this:

c:\>myWork>javac Example.java
c:\>myWork>java Example

?

yes ..:(

that's strange... I simulated your scenario to see if i was missing something... but it worked perfectly in my case as it should!

Try running any other java code in your system... btw, are you sure your jdk is valid (was downloaded from the official Oracle site)?

Thanks NP....
There was some issue in setting System Environment variables. Now this resolved

What was the exact problem? Kindly share the scenario and the solution as might help others visitng this post...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.