When I tried to compile my HelloWorldApp.java file using the following command:
javac HelloWorldApp.java
it worked (for being able to do this simple thing I had to change a lot of variables).

But then when I tried to Run the program using:
java HelloWorldApp
it gave me the error:
Exception in thread "main" java.lang.noclassdef


To solve this error I tried various commands like:
set classpath=%classpath%;.;
java -classpath . HelloWorldApp
java -classpath . HelloWorldApp.class

but to no avail. Can anyone help me out with this problem?

Regards,

-sgsawant

While compiling you need to specify the .java file, you are right on that part, but while running the file through the java command you cannot specify the .java file, such a file won't simply run, the JVM would need a .class file. So you would have to execute the command in this way :
javac HelloWorldApp the class loader would look for a file with that name and a .class extension.

While compiling you need to specify the .java file, you are right on that part, but while running the file through the java command you cannot specify the .java file, such a file won't simply run, the JVM would need a .class file. So you would have to execute the command in this way :
javac HelloWorldApp the class loader would look for a file with that name and a .class extension.

My sincerest apologies. As you have rightly pointed out it will not run if I command "java HelloWorldApp.java" but I also tried "java HelloWorldApp". I have also corrected the entry in the original post.

The error still persists. In fact if I had typed "java HelloWorldApp.java" it would have given me a different error.


Can you find the mistake I am making?

Regards,

-sgsawant

> So you would have to execute the command in this way :
> javac HelloWorldApp the class loader would look for a file with that
> name and a .class extension

A typo maybe? It should be java HelloWorldApp

> Exception in thread "main" java.lang.noclassdef

This question has been asked a lot of times on almost all java related message boards. Have you tired a web search with the given error message? Have you tried the solutions suggested in the existing threads?

BTW, the `java' command expects a "package qualified" class name and not just a class name. So if you are using a package, make sure you use the fully qualified class name when invoking `java'. If not, then executing the `java' command from the same directory in which the class file lies shouldn't be a problem as such.

Also, is the NoClassDefFoundError error thrown for the main class or some other class which the main class is using? In this case, you might have unresolved runtime dependencies which you will have to resolve by setting the appropriate classpath entries.

Thanks for your reply. I have searched using google the error I have posted above. I thoroughly exhausted the first 10 results given by google; after which the solutions have become repetitive.

I believe I have set the paths correctly. Yet as has been suggested on many threads I also asked java to look into the same folder for the class file. One of the ways in which I tried doing that was:
java -classpath . HelloWorldApp
(this i did right after javac command successfully created the .class file)
Yet I am facing the same errors.

To see if the problem is universal I tried a java program which I knew worked. It came with a camera I have installed on 1 of my robot.
After copying it to an arbitrary folder (C:\jtemp) I tried
java CMUcamGUI
Which gave errors. Thinking that it may be because of the path I tried the following:
java -classpath . CMUcamGUI

This time the program worked and opened up the expected interface. Buoyed by this success I copied the HelloWorldApp.java file to the folder C:\jtemp
I compiled it using the javac command. That was successful as the .class file appeared right besides it.

Now when I tried the same method:
java -classpath . HelloWorldApp
I am still receiving the same errors. Is there a way around this problem? Please help.

Post the code of the class "as it is" without *any* modification, including the package declaration. Also paste the *exact* error message along with a screenshot of the entire session.

This shouldn't be happening if you are well aware of the points I mentioned in my previous post.

I think your classname is different then your .java file. After you compiled a .java file, a .class file will be produced. Determine the name of .class file and try to execute with,

>java classname

NOTE: Lets us know the version of jdk you are using.

To write the program I used Netbeans IDE 6.5.1
The version of my JDK is 1.6.0_13

My program is as follows

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworldapp;

/**
 *
 * @author Shashank Sawant
 */
public class HelloWorldApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

}

The source file is stored at the following address on my hard disk:
C:\Documents and Settings\Shashank Sawant\My Documents\NetBeansProjects\HelloWorldApp\src\helloworldapp\HelloWorldApp.java

The compiled file is stored at the address:
C:\Documents and Settings\Shashank Sawant\My Documents\NetBeansProjects\HelloWorldApp\src\helloworldapp\HelloWorldApp.class

One seemingly important thing I discovered after viewing the properties of the HelloWorldApp.class file through the Netbeans IDE is that it had the following "Runtime Classpath":
C:\Documents and Settings\Shashank Sawant\My Documents\NetBeansProjects\HelloWorldApp\build\classes

Following link is the screenshot of the error and a few commands that have preceded it:
I have tried to commands after compiling the HelloWorldApp.java file. One is the simple "java HelloWorldApp" {the last but one error} and the second is "java -classpath . HelloWorldApp
Both produce failure.
http://1.
bp.blogspot.com/_NpujkFKQb0w/ShuJI7so4pI/AAAAAAAAMf8/DsT1qyhS6eQ/s1600-h/Java+Error+1.JPG

I have linked the image as I was unable to find a method to upload the image on this post. I guess there is none. Thank You for considering my error and trying to help me out.

Regards,

-sgsawant

Let me again point you to my first post in this thread which clearly mentioned:

BTW, the `java' command expects a "package qualified" class name and not just a class name. So if you are using a package, make sure you use the fully qualified class name when invoking `java'. If not, then executing the `java' command from the same directory in which the class file lies shouldn't be a problem as such.

Does that ring a bell? Think what could have gone wrong, give it a second try and reproduce the entire session again in case you still face problems.

BTW, it's advisable to have the project placed in your personal folder; something like D:\personal\projects to avoid dealing with excessively long paths which IMO is messy.

Because of this statement

package helloworldapp;
..
..

you have to change the your method of compilation and also execution.

If package is mentioned then your .class must be placed under the package named folder.

Now, compile your program
>javac HelloWorldApp.java -d .

>java helloworldapp.HelloWorldApp

Note:
>javac HelloWorldApp.java -d .
where, -d option create a package folder and
. means current folder
>java helloworldapp.HelloWorldApp

If class definition placed inside the package then qualify the class with package name.

Posting complete solutions never has and never will help. How about giving the OP a chance to try out a few things before handing out a solution?

Because of this statement

package helloworldapp;
..
..

you have to change the your method of compilation and also execution.

If package is mentioned then your .class must be placed under the package named folder.

No it doesn't. It is convention to do it that way, but you don't have to. You could place every one of your Java files in the same folder, or all of them in wildly scattered directories around the system, you would have a hard time compiling all of them at the same time, you'd probably have to compile one at a time, in the proper order, using the "-d" option, but you could do it, if you really wanted to.

Now, compile your program
>javac HelloWorldApp.java -d .

>java helloworldapp.HelloWorldApp

Note:
>javac HelloWorldApp.java -d .
where, -d option create a package folder and
. means current folder
>java helloworldapp.HelloWorldApp

"-d" does not mean create a package folder, and using "-d ." is absolutely meaningless, as that is the default anyway. The "-d" option is so that the compiled class files do not appear "in place", but rather somewhere else. I.E. You have a folder containing a src folder and a bin folder, you cd to the source folder and then use "-d /path/to/bin" to have the compiled class files appear (with a full package directory structure) under bin, rather than in place under src. Don't forget, however, to include "-cp /path/to/bin" on the javac commands, however.


If class definition placed inside the package then qualify the class with package name.

Thanks a lot to all!

@sos and @adatapost
Sorry for not being able to heed to your(sos) first comment. The concept of package is yet unclear to me. As you advised I tried to create a new folder<C:\jtemp> and in that folder I created created a new file name j2.java
I removed the package code line and save the rest of the code as it was in the previous example after changing the class name to j2.
Then I used the following commands:
javac j2.java -d . //as recommended by adatapost
java -classpath . j2

After that the output was
Hello World!

This was my first step in learning Java. I will try to study Java further and the concept of package. Will post a new problem if I have any with regards to package.

Thank You once again.

Regards,

-sgsawant

This question has already been answered. Start a new discussion instead.