Compiling and Running with the JDK

A

Alex

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
 
C

chris brat

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
 
T

Thomas Fritsch

Alex said:
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:
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.
 
A

Alex

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?
 
K

Knute Johnson

Alex said:
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.
 
A

Alex

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...
 
R

Rhino

Alex said:
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.
 
O

Oliver Wong

Alex said:
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
 
O

Oliver Wong

Knute Johnson said:
Alex said:
/**
* 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
 
K

Knute Johnson

Oliver said:
Knute Johnson said:
Alex said:
/**
* 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 :)
 
J

jmcgill

Rhino said:
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?
 
A

Alex

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!
 
K

Knute Johnson

Alex said:
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?
 
A

Alex

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?
 
K

Knute Johnson

Alex said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top