Hello, I am new at this java thing... like really, 1 day new.

I have this book called Java 2: A Beginner's Guide (Second Edition).
I got stuck on the second step which tells me to..

use javac to compile Example.java.. I read old posts that were posted here through google but.. it still failed.

Here is what I did:

i changed the cd to C:\java\bin..

javac Example.exe ENTER

java Example ENTER

then an error pops out:

Exception in thread "main" java.lang.NoClassDefFoundError: Example

How do I fix this? Can anyone help me?
Thanks.

Do you have a main method in your program? All java programs must have the following in order to compile and execute on their own:

public static void main(String[] args) 
	{

Javac Example.exe ENTER This command must have failed. Did you not get any error message? Java source code files have the extension .java not exe. Check your book's example closely.

Javac Example.exe ENTER This command must have failed. Did you not get any error message? Java source code files have the extension .java not exe. Check your book's example closely.

oops sorry, that was a typo.. it was really Example.java

Javac Example.exe ENTER This command must have failed. Did you not get any error message? Java source code files have the extension .java not exe. Check your book's example closely.

oops sorry, that was a typo.. it was really Example.java

Do you have a main method in your program? All java programs must have the following in order to compile and execute on their own

I have no idea what you're talking about. :[
I am just following what the book was telling me to do.

The first step told me to make a wordpad document and name it:
Example.java
Second step:
Execute javac and compile Example.java to turn it into Example.class or something like that.

snakai415 it is nice for once that somebody tries to learn Java through command line. However, I hope this was just one of few examples which you had to copy and paste from somewhere else. So if the book come also with electronic version of source code for examples, try to avoid it and type it . You will make typos which will triger plenty of error message, but that way you will learn to handle them. With copy and paste you will never learn. So if you get in trouble just post here and we will be happy to help you...

I didn't copy and paste, I just typed down the step-by-step guide that was written in the book.

Copy and paste the java code here. Then someone can easily point out the errors. Your error message states that it did not find a compiled file called Example.class. Did you see a file called Example.class appear in the same folder after running the javac command?

> The first step told me to make a wordpad document and name it:
Wordpad is not a text editor, its a word processor. It inserts its own headers in the saved file. You need to use notepad. In short you need something which saves the files in plain text format and not add its own headers.

Type the code from the book in a notepad, save it at a place you want, go to that folder, run the javac command properly (I assume that you must have set the class path).

After having completed the following steps successfully, you must get a class file in the same folder (provided you have not kept a package declaration). Use the java command to run the program.

If you want names of few good text editors, just let us know.

Copy and paste the java code here. Then someone can easily point out the errors. Your error message states that it did not find a compiled file called Example.class. Did you see a file called Example.class appear in the same folder after running the javac command?

-Copy and paste the java code?
-I did not see a Example.class in my folder after I typed:
javac Example.java


> The first step told me to make a wordpad document and name it:
Wordpad is not a text editor, its a word processor. It inserts its own headers in the saved file. You need to use notepad. In short you need something which saves the files in plain text format and not add its own headers.

Type the code from the book in a notepad, save it at a place you want, go to that folder, run the javac command properly (I assume that you must have set the class path).

After having completed the following steps successfully, you must get a class file in the same folder (provided you have not kept a package declaration). Use the java command to run the program.

If you want names of few good text editors, just let us know.

-After I used notepad to create Example.java, I have the same problem.
-"(I assume that you must have set the class path)." What does that part mean?


Sorry guys, I'm really bad at this. :'(


I followed this guy's guide. (http://www.daniweb.com/forums/thread8505-2.html)

ok THIS WILL WORK FOR SURE.
delete everything and go to this download page and download the j2sdk1.4.2_12 version for windows.
http://java.sun.com/j2se/1.4.2/download.html
Then download this version.
J2SE v 1.4.2_12 SDK includes the JVM technology[IMG]http://java.sun.com/images/pixel.gif[/IMG]The J2SE Software Development Kit (SDK) supports creating J2SE applications. More info...[IMG]http://java.sun.com/images/pixel.gif[/IMG]Download J2SE SDK[IMG]http://java.sun.com/images/pixel.gif[/IMG]Installation Instructions ReadMe ReleaseNotes
Sun License Third Party Licenses

i am assuming you know how to install anyways after installing you need to know where your j2sdk1.4.2_12 folder is. just for instance lets assume you have it in c drive. go to start and then run command prompt window. enter cmd. you should have a path something like this
c:\documents and settings\chris
well do this first
cd..
after that you will have c:\\documents and settings
well do the same thing one last time so then you are in your c:\
cd..
now you will have the screen something like this
C:\
i am assuming your j2sdk folder is in c drive so you will do this next
cd j2sdk1.4.2_12 (then press enter)
now you will see your command prompt like this
c:\j2sdk1.4.2_12
then just type one last command.
cd bin
now your command prompt looks like this
c:\j2sdk1.4.2_12\bin
Thats it BOYyYyyyYyy You got it. The next thing you need to do is type your code in a notepad and make sure MAKE SURE YOU SAVE IT IN THE BIN FOLDER WHICH IS IN J2SDK1.4.2_12 and save it as a .java file e.g :) (HelloWorldApp.java) in the bin folder.
Now go to your command prompt i hope it is still in bin directory
type this
javac HelloWorldApp.java
no errors HURRAY !!!!
then type java HelloWorldApp
Have fun BoYyYyyYy

Here is tutorial how to setup PATH for Java and there is also tutorial how to setup CLASSPATH which is used to tell system where are the extra JAR files which you downloaded and want to use. For example customized graphic user interface(look&feel)

sir what about if my program is saved in another directory must I bring it in the bin folder

Member Avatar for iamthwee

sir what about if my program is saved in another directory must I bring it in the bin folder

If you set the classpath properly no.

Never do any changes to instalation of java! No copy&paste any files into bin folder or any other place.
Once you set your PATH, in command prompt you navigate to place where is your file saved ( for example you know your program is saved on C hard drive in folder JavaDevelopment/Chapter7, you do cd C:\JavaDevelopment\Chapter7 ).
Once there you compile your code javac MyProgram.java, if any error in code they will be displayed and you need to deal with them. However, if compile succesfull you will run your program as java MyProgram

If you not familiar with MS-DOS commands simply type help and press enter to read more about them

Member Avatar for iamthwee

Also, for windows systems (for example) if your folder name is separated by a space you need to wrap the command with quotation marks.

i.e

cd "c:\my documents\work"

holy crap, I'm an idiot.

Thanks guys, I got it to work.
I will not mention what my mistake was since it's too embarassing v^_^v

Can someone help me understand "Blocks of Code" better? That is the only thing I have trouble understanding right now.

In the book:

Consider this if statement:

if(w < h) {
v = w * h;
w = 0;
}

I don't really get that :confused:

Thanks in advance.

if w is smaller then h execute following statement
v = w * h;
w = 0;

Nothing else to say. There can be more if you provide whole code. Otherwise this is what it does...

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.