Compiling and Running with the JDK

Discussion in 'Java' started by Alex, May 10, 2006.

  1. Alex

    Alex Guest

    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
     
    Alex, May 10, 2006
    #1
    1. Advertising

  2. Alex

    chris brat Guest

    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
     
    chris brat, May 10, 2006
    #2
    1. Advertising

  3. Alex wrote:
    > 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
    > 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?
    >

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

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, May 10, 2006
    #3
  4. Alex

    Alex Guest

    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, May 10, 2006
    #4
  5. Alex wrote:
    > 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
    >


    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.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, May 10, 2006
    #5
  6. Alex

    Alex Guest

    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...
     
    Alex, May 10, 2006
    #6
  7. Alex

    Rhino Guest

    "Alex" <> wrote in message
    news:...
    > 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
     
    Rhino, May 10, 2006
    #7
  8. Alex

    Oliver Wong Guest

    "Alex" <> wrote in message
    news:...
    > 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
     
    Oliver Wong, May 10, 2006
    #8
  9. Alex

    Oliver Wong Guest

    "Knute Johnson" <> wrote in message
    news:j3o8g.1$...
    > Alex wrote:
    >>
    >> /**
    >> * 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
     
    Oliver Wong, May 10, 2006
    #9
  10. Oliver Wong wrote:
    >
    > "Knute Johnson" <> wrote in message
    > news:j3o8g.1$...
    >> Alex wrote:
    >>>
    >>> /**
    >>> * 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


    Oh silly me :)

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, May 10, 2006
    #10
  11. Alex

    jmcgill Guest

    Rhino wrote:
    > "Alex" <> wrote in message
    > news:...


    > 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?
     
    jmcgill, May 10, 2006
    #11
  12. Alex

    Alex Guest

    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!
     
    Alex, May 11, 2006
    #12
  13. Alex wrote:
    > 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?

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, May 11, 2006
    #13
  14. Alex

    Alex Guest

    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?
     
    Alex, May 12, 2006
    #14
  15. Alex wrote:
    > 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?
    >


    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.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, May 12, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ulf Meinhardt
    Replies:
    0
    Views:
    6,476
    Ulf Meinhardt
    Aug 10, 2006
  2. Jaggu
    Replies:
    3
    Views:
    1,019
    Nigel Wade
    Jan 8, 2007
  3. Roedy Green
    Replies:
    3
    Views:
    2,483
    Arne Vajhøj
    Aug 6, 2009
  4. Mike Schilling
    Replies:
    2
    Views:
    2,121
    Mike Schilling
    Sep 27, 2009
  5. Jochen Brenzlinger
    Replies:
    5
    Views:
    976
    Roedy Green
    Sep 2, 2011
Loading...

Share This Page