Class Files Won't Appear after compiling a .java file!!!

I am still kinda new to the Java language. I have used Java off and on for a several months just trying something new that I've seen someone else use in a book or on the web and then trying to develop my own code for that hack; now I have a problem. Many times when I compile a .java file with MS-DOS, the .class file will not appear and when I try to run the file with thejava command, I get a noClassDefFoundError in mymain method. What can I do to make my class files show up in Windows Explorer and get rid of the pesky noClassDefFoundError? Thanks in advance for any and all help.

[609 byte] By [java650a] at [2007-10-3 0:32:35]
# 1

If you are not getting a .class file then you are either

1. Not typing the correct command to compile your .java file.

2. You have errors in your .java file which the compiler complains about.

Either way you should have some message in your command prompt window telling you what you have done wrong.

floundera at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 2
Well, you should try to check if you have set your class path right!
Redxxiva at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 3

Dont forget source file declaration rules:

?There can be only one public class per source code file.

?Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here.

?If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { }

must be in a source code file named Dog.java.

?If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.

?If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.

?import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.

? A file can have more than one nonpublic class.

?Files with no public classes can have a name that does not match any of the classes in the file.

JJ

Java_Jaya at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 4
Some of those rules are compiler specific.
kablaira at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 5
Go to your System environment variables.Make sure CLASSPATH has a .Prepended to it..so that instead ofC:\path\to\dir;C:\path\to\other\dirIt reads:.;C:\path\to\dir;C:\path\to\other\dirThat indicates 'current directory'
xiarcela at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 6
> Some of those rules are compiler specific.Which ones? They came straight from the SCJP study guide. I highly dought these rules are compiler specific, as you state. They will work on any Sun Java compiler.JJ
Java_Jaya at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 7

> ?There can be only one public class per source code

> file.

> ?If there is a public class in a file, the name of

> the file must match the name of the public class. For

> example, a class declared as public class Dog {

> }

> must be in a source code file named

> Dog.java.

These rules are optional and an implementation may or may not choose to enforce them and there is at least one compiler that allows more than one public class in a file. Currently the JDK enforces the rules and I would be surprised if it didn't continue to, but there's nothing preventing them from stopping.

Most of the other rules also only apply to file-based implementations but they are clearly using a file-based implementation so that's irrelevent in this context, just note they don't necessarily apply to all environments.

kablaira at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 8
Also note that a compiler could very well choose to require a non-public class be declared in a file with the same name if it were referred to by code in another compilation unit.
kablaira at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 9

> 1. Not typing the correct command to compile your

> .java file.

I know I am typing the right command because MS-DOS doesn't complain about the command beause it doesn't say anything about the java compiler not being where it is supposed to be.

> 2. You have errors in your .java file which the

> compiler complains about.

The DOS window shows text similar to the following:

D:\Program Files\Java\Java SE 1.5\bin\>javac calc.java

D:\Program Files\Java\Java SE\bin\>java calc

That is where the annoying noClassDefFoundError appears.

> Either way you should have some message in your

> command prompt window telling you what you have done

> wrong.

There is no error appearing until I try to run my .java file after compiling it. Thanks though.

java650a at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 10

java -classpath . calc

Like they said, it's a classpath issue. Java has to be "told" where the class(es) is/are.

You apparantly have a CLASSPATH environment variable set to something which does not include the current directory (where your class is).

Get rid of that environment variable. It is getting in your way. Without it, then you don't need the "-classpath ." switch either, because without it and no CLASSPATH environment variable, the default is to include the current directory.

Message was edited by:

warnerja

warnerjaa at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 11
After getting similar errors (which were also answered on this forum -- http://forum.java.sun.com/thread.jspa?threadID=751260&tstart=90 and in a few others) I have compiled and called all java classes using the -classpath commands warnerja and others have suggested. It helps. Try it.
cdelmioa at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 12

> D:\Program Files\Java\Java SE 1.5\bin\>javac

> calc.java

>

> D:\Program Files\Java\Java SE\bin\>java calc

>

> That is where the annoying noClassDefFoundError

> appears.

Please make use of the copy and paste function.

Click the icon in the corner, the select Edit->Mark [ http://img135.imageshack.us/my.php?image=copypaste2ht.gif ]

Click and drag the text you want to copy [ http://img98.imageshack.us/my.php?image=copypaste8my.gif ]

Click on the icon at the top again, and select copy [ http://img225.imageshack.us/my.php?image=copypaste3hm.gif ].

Next use the dir application to check that the file you think has been created

H:\>dir *.class

Volume in drive H is xxxx

Volume Serial Number is xxx

Directory of H:\

20/04/2006 17:35163 Test.class

11/07/2006 10:291,854 Sudoku$Square.class

11/07/2006 10:291,244 Sudoku$Block.class

11/07/2006 10:292,420 Sudoku$Board.class

11/07/2006 10:29324 Sudoku.class

5 File(s) 3,775 bytes

0 Dir(s) xxxx

H:\>

Finally, I HIGHLY recomend you do NOT use c:\program files\java\ as the root of your personal projects.

Instead create a folder in say the root of C called "javaprojects", and set your path. [ http://forum.java.sun.com/thread.jspa?threadID=713720&start=34&tstart=0 ]

And follow warnerja advice above. run with

java -classpath . Test

Message was edited by:

mlk

mlka at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...
# 13
Thanks Java_Jay. I looked at the book I've been using and I overlooked the statement that said I had to use the first or only class in my .java file as the filename. I tried that and it worked. Thanks for everyones help.
java650a at 2007-7-14 17:26:01 > top of Java-index,Java Essentials,New To Java...