I am assuming that HelloWorld.java file is wrapped in my\package (package structure).
And you would be wrong. Package structure is for classes -- not java files. When you specify the java file for the compiler, you still need to specify the directory structure.
Henry
sony vijay
Ranch Hand
Joined: Jun 27, 2010
Posts: 32
posted
0
Ok.
Now, I shifted two levels above in the directory structure...meaning I am running from the C:\ directory
But, am still getting the file not found error. I saved the file using Notepad in Windows 7. So, I guess it attaches a .txt to the filename. Is this the problem ? If am using Windows7 how do I save file with .java extension ?
sony vijay wrote:
But, am still getting the file not found error. I saved the file using Notepad in Windows 7. So, I guess it attaches a .txt to the filename. Is this the problem ? If am using Windows7 how do I save file with .java extension ?
If the file is not saved with a java extension, then absolutely, this is the problem. You need to save it with the correct name, or the compiler will not be able to find it.
The easiest solution is to bring up a command window, and rename the file using the command line (The GUI is not really good in this regard).
Actually, for a beginner, it is probably best to not use a classpath (to start). In fact, the instructions that I gave assumes that a classpath environment has not been set.
Regardless, you should fix the ".java" extension first, as that is likely the main cause to this issue.
The classpath is only used by Java to find compiled class files. Java does not use it to find Java source files; so including the directory that contains your source code files in the classpath, and then expecting Java to find your source file HelloWorld.java there, is not going to work.
If your class HelloWorld is in a package named my.package, then you need to compile it like this:
Here, it doesn't matter what your current directory is. You must add the base directory of your package structure (C:\myclasses\here) to the classpath. Specify the fully-qualified name of the class (my.package.HelloWorld).
Exception in thread "main" java.lang.NoClassDefFoundError: my/package/HelloWorld
<wrong name: HelloWorld>
-------
Could not find the main class: my.package.HelloWorld. Program will exit
This is the code in my HelloWorld.java file. There is a main method in it. Then, why is it giving the above exception ?
I noticed that HelloWorld.class file is created in C:\myclasses\here>javac my\package\ after I compiled the file.
I got this error :
my\package\HelloWorld.java:1: <identifier> expected
package my.package;
1 error
compilation approach is fine . the error indicate that *package my.package;* package is not a valid identifier. i.e, you cant create the package name as *package* , since it is a keyword.
hth
sony vijay
Ranch Hand
Joined: Jun 27, 2010
Posts: 32
posted
0
Thanks a lot everyone for your time and help. This worked.