I renamed the program as Noose.java When I do a java Noose, I get NoClassDef found error - wrong name moose/Noose. javac Noose.java compiles fine.
If the problem persist then revert.
Step 1. Create your program. (In your case it is Noose.java)
Step 2. Now put this file in a folder with name same as your package name. (In your case, the package name is moose). So you need to create a directory or folder with folder name as moose, and then put your noose.java inside this folder.
Step 3. [Main Step] Now set the CLASSPATH. Consider you are working on Windows and your newly created folder moose is present under C:\ directory. If this is the case then you need to set CLASSPATH using command prompt as: set CLASSPATH=.;C:\;
When you set classpath, Java will search our file in C-directory and also in current directory. This is because I have used .(dot) for current directory and C:\ for C directory.
Step 4. Compile the java file
Step 5. Run your program
No, setting the classpath is not at all important. you should almost certainly NOT set a system classpath. A system classpath can cause more trouble than it solves. Note the advice given in that Java™ Tutorials link was to unset the classpath.
Peeyush Bhadola wrote:Hello Sami, I think you forgot to set the CLASSPATH variable, which is the most important activity to be performed while playing with packages. . . .
You should be executing your application from the root of the package structure. so if you have a class called moose.Noose and the moose directory is inside the myJava folder, then you should set your command line to the myJava folder
If there is no system classpath, then the java tool will use "." as its classpath.
c:\ myJava:> javac moose/Noose.java
c:\myJava:> java moose.Noose
. . . and welcome to the Ranch
All right, so I am not using Windows®, but it would work exactly the same way on any operating system. And I have no classpath set
[campbell@campbell1110 ~]$ cd java
[campbell@campbell1110 java]$ mkdir moose
[campbell@campbell1110 java]$ cd moose
[campbell@campbell1110 moose]$ gedit Noose.java
[campbell@campbell1110 moose]$ cd ..
[campbell@campbell1110 java]$ javac moose/Monkey.java
javac: file not found: moose/Monkey.java
Usage: javac <options> <source files>
use -help for a list of possible options
[campbell@campbell1110 java]$ javac moose/Noose.java
[campbell@campbell1110 java]$ java moose.Noose
This is monkey