File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Getting started problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting started problems" Watch "Getting started problems" New topic
Author

Getting started problems

James Eaton
Greenhorn

Joined: Oct 02, 2007
Posts: 5
Hello,

I'm trying to teach myself java from a book. I'm having problems, and I've seen others with this problem but I can't seem to get the solutions offered to work for me. So here goes:

First, I'm running a basic installation of Windows XP with SP2. I have installed the jdk1.6.0_02 stuff from sun. When I write the programs, I put them in a simple directory called c:\files. Pretty basic stuff, but ..

I can't get them to run. Here's the error I see on other posts:

Exception in thread "main" java.lang.NoClassDefFoundError: c:\files\Doobee

I am assuming this is a class path error. But when i try to specify the class path with -cp . , i still get the same error:

C:\Program Files\Java\jdk1.6.0_02\bin>java -cp .files C:\files\DooBee.class
Exception in thread "main" java.lang.NoClassDefFoundError:C:\files\DooBee/class

Some further info: In my frustration to fix this, I uninstalled and reinstalled jdk1.6.0. That did not fix my problem with running the class files, but it did add another problem. Now I can't even get the javac or java executables to run unless I'm in the \bin directory, which of course means that they are not in my path. I have added a . to the classpath environment variables and that does not work. And now I can't get the jdk1.6.0 added to my path either. So I'm doubly in trouble now. Am I missing something in XP? I haven't been running it for that long.


This is a frustrating problem to say the least. I am not a computer novice. I've worked in tech for nearly 10 years. I know paths and I've programmed in VB fairly extensively. But this is driving me nuts, so if someone can please help me, I'd appreciate it a great deal.
Thanks.
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
You have to add the bin directory to the environment. In XP I think you have to go to My computer, advanced properties and environment variables. Look for the path one and add the route for your bin. Then open a new command window so it has taken the change into effect.

You must check again the �cp option in your docs. Maybe your are not positioned in the right directory when you are calling the java command. If you have a package like com.javaranch.example don�t try to run from the example dir. You have to run at the root directory where the com dir is located.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
smee bond
Ranch Hand

Joined: Sep 29, 2007
Posts: 34
Now that you have re-installed JDK,
1) you need to set the PATH variable pointing to bin directory. Go to "PATH" variable (not CLASSPATH) and append "C:\....\Java\jdk1.6.0_02\bin;" at the end (dont forget to add ";"). save the new configuration. With this setting you should be able to compile and run from any directory that contains source files.

2) You are not supposed to use the ".class" extension while running java programs.
Try at the command prompt ">java -cp .files C:\files\DooBee" instead of
"C:\Program Files\Java\jdk1.6.0_02\bin>java -cp .files C:\files\DooBee.class"

Hope it helps. I am also a beginner. Dont get frustrated, after all JavaRanch is always there to help us
James Eaton
Greenhorn

Joined: Oct 02, 2007
Posts: 5
Thanks for responding you guys. I'm stuck. Here's another question?

From where does XP set the path? when I type path at the commond prompt. I see two references this: "c:\Program Files\Java\jdk1.6.0_02\bin"

Yet I still can't compile from the command line. Does the registry populate the path? I see a reference in the java key to that directory. With no statement in environment varaibles, I still see that directory in the path, but it doesn't work either. I know everybody probably gets tired of answering this question, but this is driving me nuts. thanks.
James Eaton
Greenhorn

Joined: Oct 02, 2007
Posts: 5
C:\Program Files\Java\jdk1.6.0_02\bin>java c:\files\MyFirstApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: c:\files\MyFirstApp/class

This is what I get when I'm in the bin directory and try run the class file in the c:\files folder. The good news is: The code is compiling. But I still can't get the path straight or the classpath straight.

How do you guys do this java stuff?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

when you run your java program, don't include ".class".

How are you setting the path? are you changing it within a command window, or are you changing it via the My Computer-Properties->Advanced->Environment variables?

if you are doing the former, it is only valid with that one command window. if the later, you have to open a new command window to pick up the changes - an existing window won't have them.
[ October 02, 2007: Message edited by: Fred Rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hang in there, James. Smee has it close, but no cigar -- don't be discouraged by misinformation.

The CLASSPATH tells Java where to search for class files. For the beginner (and frankly, for virtually everybody) the best setting is NO SETTING AT ALL. Then Java will look in the current directory only. This is a great default. My recommendation to you is to remove any existing definition, completely.

Now, as far as Java still not finding your class definitions: the argument to java.exe is the name of a class, not the name of a class file, and definitely not the path to a class file. Instead, the -cp flag can be used to tell Java where to look for classes. It works exactly the same as the CLASSPATH environment variable.

So please try

java -cp c:\files MyFirstApp

The "-cp c:\files" tells Java where to look for class files; the "MyFirstApp" tells it what class to look for.

If CLASSPATH is undefined, and your current working directory is c:\files, then this can be simplified to

java MyFirstApp


[Jess in Action][AskingGoodQuestions]
James Eaton
Greenhorn

Joined: Oct 02, 2007
Posts: 5
Ernest thank you very much. I'm not sure why that seemed so difficult but I used your instructions and got my java apps to run from the command line. Thanks for your help.

I'm still baffled by one last thing though. My working directory was c:\files where the .java files reside. I had already compiled a class file for the app you described above so I just used your instructions and it ran, viola.

So, I had another .java file in there and I tried to run javac against it from the same directory. I got the "not recognized as an internal command error," a path problem.

I changed to the jdk bin directory, compiled the file into a class file and ran it from the command line the way you described above and it worked as well.

If I can run java from c:\files, why can't I run javac?

Thanks again for your help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by James Eaton:

If I can run java from c:\files, why can't I run javac?


For reasons that have never been entirely clear to me, the JDK installer will usually (although not always!) install two copies of java.exe on your system. One is in %JAVA_HOME%\bin, as you expect; but the other goes someplace like C:\WINDOWS or C:\WINDOWS\SYSTEM . I believe the purpose of the extra java.exe is to support the Java web-browser plugin, although why a second copy is needed for this, I don't know.

In any case, it's likely this is the one that's being picked up on your PATH, while the one in %JAVA_HOME%\bin isn't. That would explain why javac.exe isn't found. From all you've said about setting your path, it sounds like things should be working, but obviously there's some little detail that's still wrong.
Makwana Deepak
Greenhorn

Joined: Aug 14, 2007
Posts: 6
Ernest Hello, make yourself clear now, because the thing is that when you install jdk it is true that it installs java.exe into program or system directory too, but that is for the purpose of web browser capability, so when you run the ready made example of swings or applet you want get problem in executing the same. So the moral of the story is "for technical people more clear things are really great but half knowledge is too dangerous" bez. if the same thing you say in interview then you are definitely gone buddy.

Now for the problem of James it is quite normal thing for beginner to face.

But listen James its programming world dude if you get frustrated so early then how you will manage the whole project or even a module dude?, so be patience and its just a matter of class path thats it.

So where ever you install jdk first thing you need to do is set the bin dir. into you path env. variable list then you can compile your code. But if you face problem while executing simple java file then you need to do is only one thing that is "set classpath=." thats it then see you code executes like a sexy lady's cat walk.

Enjoy de moments, any further query can ping me at [address removed, please read this for more information. ]
Makwana Deepak.
[ October 03, 2007: Message edited by: Bear Bibeault ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Makwana Deepak,

My heart bursts with gratitude for the iridescent pearls of wisdom you have allowed to tumble from your sainted keyboard. You have truly altered the course of my life, and I will never be the same henceforth.

.. "dude." :roll:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Originally posted by Makwana Deepak:
Enjoy de moments
Please use real words when posting to the forums. Abbreviations such as "de" in place of "the" only serve to make your posts more difficult to read.

Please read this for more information.

thanks,
bear
JavaRanch sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

James, you indicated above that your PATH includes...

c:\Program Files\Java\jdk1.6.0_02\bin

This looks correct, so I'm not sure why javac isn't recognized. Verify that there are no extra spaces before or after this entry, and that semicolons are used to separate this from other entries.

If it's still not working, then referring back to Fred's post above, did you set this in the command prompt or as an environment variable?

(For reference, the PATH is addressed in step 4 of Sun's installation instructions for Windows.)
[ October 03, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
GeeCON Prague 2014
 
subject: Getting started problems