Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Compiling and Running with the JDK

Thread view: 
Alex - 10 May 2006 15:09 GMT
Hi,

I am just starting out and have what is probably a silly question but
any help would be appreciated.

I am trying to run the first example in the Java In a Nutshell 5th
edition book. Here is the code:

/**
* This program computes the factorial of a number
*/
public class Factorial {                         // Define a class
  public static void main(String[] args) { // The program starts here
     int input = Integer.parseInt(args[0]); // Get the user's input
     double result = factorial(input);        // Compute the factorial
     System.out.println(result);              // Print out the result
  }

  public static double factorial(int x) {    // This method computes
x!
     if (x < 0)                                       // Check for bad
input
        return 0.0;                                 // If bad, return
0
     double fact = 1.0;                         // Begin with an
initial value
     while(x > 1) {                               // Loop until x
equals 1
        fact = fact * x;                          // Multiply by x
each time
        x = x - 1;                                 // And then
decrement by x
     }                                                // Jump back to
start of loop
     return fact;                                  // Return the
result
  }                                                   // factorial()
ends here
}                                                      // The class
ends here

Using the JDK I type "javac Factorial.java" and it seems to compile ok
and creates Factorial.class. However when I try to run that with "java
Factorial 4" I get the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Factorial.

I am not sure what I have done wrong. The code is exactly as it is in
the book and I even downloaded it from their website and tried it and
the same thing happened. What am I doing wrong?

Thanks,

Alex
chris brat - 10 May 2006 15:24 GMT
Sounds like your classpath is not set correctly - its an environment
variable that is used to find class files.

If you are in the directory of the class file you want to execute you
can also use the classpath argument - Try the command below (the .
means the directory you are in) :

java -classpath . Factorial 4

Chris
Thomas Fritsch - 10 May 2006 15:57 GMT
> I am just starting out and have what is probably a silly question but
> any help would be appreciated.
>
> I am trying to run the first example in the Java In a Nutshell 5th
> edition book. Here is the code:

<code snipped>

> Using the JDK I type "javac Factorial.java" and it seems to compile ok
> and creates Factorial.class. However when I try to run that with "java
[quoted text clipped - 4 lines]
> the book and I even downloaded it from their website and tried it and
> the same thing happened. What am I doing wrong?

See <http://mindprod.com/jgloss/helloworld.html>. The hints given there
should apply to your example as well.

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Alex - 10 May 2006 16:05 GMT
Ok, I will try that exact example as well but the .java and .class
files are in the same directory as the javac.exe and java.exe programs
so do I still need a classpath argument?
Alex - 10 May 2006 16:58 GMT
Ok I have tried that example to and I get the same error message. The
files are in the same folder as the javac.exe and java.exe programs.
I'm not sure what I'm doing wrong...
Oliver Wong - 10 May 2006 17:34 GMT
> Ok I have tried that example to and I get the same error message. The
> files are in the same folder as the javac.exe and java.exe programs.
> I'm not sure what I'm doing wrong...

   Did you follow Chris's advice with the classpath?

<quote>
Sounds like your classpath is not set correctly - its an environment
variable that is used to find class files.

If you are in the directory of the class file you want to execute you
can also use the classpath argument - Try the command below (the .
means the directory you are in) :

java -classpath . Factorial 4
</quote>

   - Oliver
Rhino - 10 May 2006 17:06 GMT
> Ok, I will try that exact example as well but the .java and .class
> files are in the same directory as the javac.exe and java.exe programs
> so do I still need a classpath argument?

That's probably a bad idea: your java programs should probably not be in the
same directory as the javac and java programs! I hope this is just a
temporary expedient until you get your first program compiled and running.

Normal practice is to either:
- put the .java files and .class files in the same directory with nothing
else in that directory
- have separate directories for .java and .class files with nothing else in
either directory

I think most people use the latter approach, one directory exclusively for
.java and a separate directory exclusively for .class files.

Don't worry about this until you get your first program running but then you
should set up your new directory or directories before you start cluttering
up the java/bin directory with your own code.

--
Rhino
jmcgill - 10 May 2006 18:32 GMT
> Normal practice is to either:
> - put the .java files and .class files in the same directory with nothing
> else in that directory
> - have separate directories for .java and .class files with nothing else in
> either directory

Well, my opinion is that you shouldn't even have a system that *allows*
you to write in the directory with the java runtime, even if you are the
sole person involved.

But I also read this and would point out that there are several other
things besides .class files that need to be on the classpath, such as
properties.

What kind of system gives a standard user write access to runtime
library or bin paths, anyway?
Alex - 11 May 2006 11:22 GMT
Well I am just trying to compile my first simple program. It's not
going to be a permanent measure but it still doesn't seem to work! I
have tried adding the classpath argument and I get the same error!
Knute Johnson - 11 May 2006 20:22 GMT
> Well I am just trying to compile my first simple program. It's not
> going to be a permanent measure but it still doesn't seem to work! I
> have tried adding the classpath argument and I get the same error!

You absolutely do not want to put in a classpath.  That will only make
your life more difficult.  Are you sure that you installed the JRE?
What version JDK did you install?  What OS are you running on?  If you
type java at the command prompt does it run?

Signature

Knute Johnson
email s/nospam/knute/

Alex - 12 May 2006 17:36 GMT
Well I have found this tutorial from Sun:
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/
Right at the end is the problem I'm having. If I do EXACTLY as they
say, after I have typed 'set CLASSPATH=' and then 'java HelloWorldApp'
it runs.
So I understand how to change the CLASSPATH variable, but NOT what to
change it to!! They don't seem to say! I have tried the following but
none of them worked:

C:\Program Files\Java\jdk1.5.0_06\bin
C:\Program Files\Java\jdk1.5.0_06\jre\bin
C:\Program Files\Java\jre1.5.0\bin
C:\Program Files\Java\jre1.5.0_06\bin

Any ideas?
Knute Johnson - 12 May 2006 18:29 GMT
> Well I have found this tutorial from Sun:
> http://java.sun.com/docs/books/tutorial/getStarted/cupojava/
[quoted text clipped - 11 lines]
>
> Any ideas?

Alex:

The 'set CLASSPATH=' causes the classpath to be set to nothing.  Your
problem is that you have a classpath.  Follow these steps to get rid of
it on WinXP:

Start
Control Panel
Performance and Maintenance
System
Advanced Tab
Environment Variables
Select CLASSPATH and delete it in both User and System variables if it
is in both

Close any DOS windows (shells) and reopen them to have the new settings
take effect.  Type set<ENTER> to make sure there is no classpath
variable set.

Now you should be able to compile and run your Java programs.  The
classpath environment variable and the -cp or -classpath command line
option only need be used if you have to search for class files in jars
and other directories.

Signature

Knute Johnson
email s/nospam/knute/

Knute Johnson - 10 May 2006 16:57 GMT
> Hi,
>
[quoted text clipped - 49 lines]
>
> Alex

Your program has a class file name of Factorial and it appears that you
are trying to run Factorial 4.  You should expect the error you got if
that is the case.  If your jdk installation is on a Windows machine you
do not need nor should you put in a CLASSPATH.  The only modification
you need to make to the environment is to add the jdk bin directory to
your path.  Everything else is done by the installation.

Signature

Knute Johnson
email s/nospam/knute/

Oliver Wong - 10 May 2006 17:35 GMT
>> /**
>>  * This program computes the factorial of a number
>>  */
>> public class Factorial {                         // Define a class
>>    public static void main(String[] args) { // The program starts here
>>       int input = Integer.parseInt(args[0]); // Get the user's input
[snipped rest of code]

> Your program has a class file name of Factorial and it appears that you
> are trying to run Factorial 4.

   The 4 is one of the command line arguments pass to the Factorial
program.

   - Oliver
Knute Johnson - 10 May 2006 18:17 GMT
>>> /**
>>>  * This program computes the factorial of a number
[quoted text clipped - 11 lines]
>
>    - Oliver

Oh silly me :)

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.