LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2006, 01:47 PM   #1
chief_officer
Member
 
Registered: Mar 2006
Location: Istanbul, TR
Distribution: Red Hat, CentOS, Ubuntu
Posts: 181

Rep: Reputation: 30
Java: java.lang.NoClassDefFoundError: javaapplication8/Factorial


Friends,

I am going through the "complete reference" code by code and there are so many things that I don't understand and post here. Hope I will be up someday.

Here is my simple factorial code:

Factorial.java
Code:
package javaapplication8;


public class Factorial
{
    
int fact (int n)
{
int result;
            
        if(n==1) return 1;
        result = fact(n-1)*n;
        return result;
}
   
}
main.java

Code:
package javaapplication8;


public class Main
{
   
    public Main()
    {
    }
    
    
    public static void main(String[] args)
    {
        Factorial f = new Factorial();
        
        System.out.println("Factorial of 3 is " + f.fact(3));
        System.out.println("Factorial of 5 is " + f.fact(5));
        System.out.println("Factorial of 9 is " + f.fact(9));
    }
    
}
When I compile main.java, I get the "BUILD SUCCESSFUL" result. But when I run the code, following is what I have:

Quote:
init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.NoClassDefFoundError: javaapplication8/Factorial
at javaapplication8.Main.main(Main.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I have written the code from scratch many times, but got the same error. Don't know why.

I also feel I am bugging the community too much with my newbie questions. I will be grateful if someone can point me through a real newbie java-teaching website. I began to lose control with the book.

Regards,
 
Old 05-29-2006, 02:33 PM   #2
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
The problem is regarding the package statement.
I tried compiling the program by removing the package statement from both the files and it worked fine.
Code:
mann@Manish:~/javaapplication8$ javac Factorial.java
mann@Manish:~/javaapplication8$ javac Main.java
mann@Manish:~/javaapplication8$ java Main
Factorial of 3 is 6
Factorial of 5 is 120
Factorial of 9 is 362880
mann@Manish:~/javaapplication8$
I previously knew about how to make it work with packages, but, it has been years I haven't coded java. Anyway, it's good to remind all that. I will let u know as soon as I find out the solution about packages. Till then, code this way.
 
Old 05-29-2006, 02:53 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Wouldn't it be easier to put main() in the Factorial class, or is there a specific need to use a package here?
 
Old 05-29-2006, 02:59 PM   #4
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
Quote:
Originally Posted by Nylex
Wouldn't it be easier to put main() in the Factorial class, or is there a specific need to use a package here?
It is considered a good coding practice to group related classes into packages. But, yes while learning, the practice to use package is not required.
When I used to learn java, I put all the classes of a program in one single file to avoid confusions among several programs.
 
Old 05-29-2006, 03:43 PM   #5
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
Alright. I found it. If u want to complie/run your programs with package <packagename>;, u can see this example. I have tested these methods on ur program itself and following is what I have come up with.
When u use
Code:
package javaapplication8;
you should have to put the Factorial.java, Main.java files in a directory called "javaapplication8" and this directory should be inside ur current working directory.
Then, u can compile these files by using
Code:
mann@Manish:~$ javac javaapplication8/Factorial.java
mann@Manish:~$ javac javaapplication8/Main.java
or simply by using
Code:
mann@Manish:~$ javac javaapplication8/*.java
or by using
Code:
mann@Manish:~$ cd javaapplication8
mann@Manish:~/javaapplication8$ javac *.java
But, u will always run the program from the parent directory and not the package directory
Code:
mann@Manish:~$ java javaapplication8/Main
or u may also use
Code:
mann@Manish:~$ java javaapplication8.Main
But, I would still advice u to not to use package as u don't want to do all this while u r learning java. Just go for the regular way without package.

Last edited by manishsingh4u; 05-29-2006 at 04:05 PM.
 
Old 05-30-2006, 01:25 PM   #6
chief_officer
Member
 
Registered: Mar 2006
Location: Istanbul, TR
Distribution: Red Hat, CentOS, Ubuntu
Posts: 181

Original Poster
Rep: Reputation: 30
[Java] ')' expected???

Friends,

Thank you all for your replies. Tonight I came home and once tried to run the code and it worked fine. I don't know why I was getting the error message. Weird

I tried to modify some of my code, so that the factorial is computed for the number that the user has entered. So, here is my modified code:

main.java
Code:
package javaapplication8;
        
public class Main
{
   
    public Main()
    {
    }
    
    
    public static void main(String[] args)
        throws java.io.Exception
        
    {
    char n;
            Factorial f = new Factorial();              
            
            do
            {
            System.out.println("Enter a number to compute its factorial: ");
            n = (char) System.in.read();
            }
            while (n == 0);
            
            System.out.println("\n");
        
        System.out.println("Factorial of " +n " is " + f.fact(int n));
                            
    }
    
}
Factorial.java
Code:
package javaapplication8;

public class Factorial
{
    
int fact (int n)
{
int result;
            
        if(n==1) return 1;
        result = fact(n-1)*n;
        return result;
}
   
}
Now, the red line is line 30 in my code. The error message is as follows:

Quote:
init:
deps-jar:
Compiling 1 source file to /home/diablo/java_codes/JavaApplication8/build/classes
/home/diablo/java_codes/JavaApplication8/src/javaapplication8/Main.java:30: ')' expected
System.out.println("Factorial of " +n " is " + f.fact(n));
1 error
BUILD FAILED (total time: 0 seconds)
There is no *unmatching* parantheses in the subject line 30. Whatever I do, I couldn't get rid of the error.

A couple of things:
  1. Do not assume that I know what throws java.io.Exception is. I just learnt in the book that it is required to use System.in.read() to read user input from the console.
  2. System.out.println("Factorial of " +n " is " + f.fact(int n)); does not work whether f.fact(int n) or f.fact(n). The error message is the same.
  3. If my code worked, I thought the do-while statement would do much better if I could test if the value entered was integer. So, I had written while (n != int). But it didn't work. Seems like I am making a logical or syntax error.
  4. I am using NetBeans 5.0 on a SuSE 9.3 Pro box.
  5. I am a complete n00b in Java.

Edit: Added item no 2 in the list.

Last edited by chief_officer; 05-30-2006 at 01:28 PM.
 
Old 05-30-2006, 01:58 PM   #7
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
I have marked the changes which I have done to Main.java
Code:
package javaapplication8;

import java.io.*;        

public class Main {
   
    public Main() {
    }
    
    
    public static void main(String[] args)
        throws Exception {
            
            char n;
            Factorial f = new Factorial();              
            
            do {
                 System.out.println("Enter a number to compute its factorial: ");
                 n = (char) System.in.read(); } while (n == 0);
            
            System.out.println("\n");
        
            System.out.println("Factorial of " + n + " is " + f.fact(n));
                            
    }
    
}
Now, it compliles and run well. But, u need to check the logic to find the factorial as it's not working as expected. This is what I see here.
Code:
mann@Manish:~$ javac javaapplication8/Factorial.java
mann@Manish:~$ javac javaapplication8/Main.java
mann@Manish:~$ java javaapplication8/Main
Enter a number to compute its factorial:
5


Factorial of 5 is 0
mann@Manish:~$

Last edited by manishsingh4u; 05-30-2006 at 01:59 PM.
 
Old 05-31-2006, 01:46 PM   #8
chief_officer
Member
 
Registered: Mar 2006
Location: Istanbul, TR
Distribution: Red Hat, CentOS, Ubuntu
Posts: 181

Original Poster
Rep: Reputation: 30
Dear manishsingh4u,

Thank you very much for your kind assistance and most importantly, your patience.

I have rechecked the file and saw that the logic behind the factorial is fine. However reading the input from the console does not pass an integer value to the fact class. See the following code and the outputs:

Code:
package javaapplication8;

import java.io.*;

public class Main
{
   
    public Main()
    {
    }
    
    
    public static void main(String[] args)
        throws Exception
        
    {
    int n;
            Factorial f = new Factorial();              
            
            do
            {
            System.out.println("Enter a number to compute its factorial: ");
            n = (int) System.in.read();
            }
            while (n == 1);
            
            System.out.println("\n");
        

        System.out.println("Factorial of " + n + " is " + f.fact(n));
        System.out.println("Factorial of 5 is " + f.fact(5));                    
    }
    
}
Here I added the System.out.println("Factorial of 5 is " + f.fact(5)); just to see what is going on. The results are as follows:

Quote:
input value=0 output=48
input value=1 output=49
input value=2 output=50
input value=3 output=51
....
input value=n output=48+n
While factorial of 5 is computed successfully.

Now, I change the lines in red as follows:

Quote:
int n ---> char n;
n = (int) System.in.read() ---> n = (char) System.in.read();
The output becomes Factorial of 3 is 0 for n=3. The 0 is independent of the n value; if I input 8, then the output is Factorial of 8 is 0. I even tried with character x, and the output is Factorial of x is 0.

Both is wrong.

I went back to the former case, where I receive 48+n as the computed output of the factorial and tried it with a character. For x, the output is Factorial of 120 is 0. For d, the output is Factorial of 100 is 0. So, the code somehow changes my integer input to its ASCII value. How does it do it when I force the input to be integer, I couldn't figure out...
 
Old 05-31-2006, 01:59 PM   #9
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Can you not use Integer.parseInt(System.in.read())?
 
Old 05-31-2006, 03:28 PM   #10
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
Well, I never got stuck with this problem yet because I always used the following way. For some reason (which I am not able to figure out), n is storing the ASCII value for 5 instead of it's decimal value. Anyway, this code works fine.
Code:
package javaapplication8;

import java.io.*;

public class Main {
   
    public Main() {
    }
    
    public static void main(String[] args)
        throws Exception {
    
        int n;
        Factorial f = new Factorial();              
            
        do {
            System.out.println("Enter a number to compute its factorial: ");
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
	    n = Integer.parseInt (br.readLine());
        } while (n == 1);
            
        System.out.println("\n");
        System.out.println("Factorial of " + n + " is " + f.fact(n));
    }
    
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java exception: java.lang.NoClassDefFoundError kath Programming 12 05-11-2006 04:37 AM
java.lang.NoclassDefFoundError rlnd Programming 5 02-18-2006 04:54 AM
java.lang.InstantiationException: java.sql.Connection poeta_boy Programming 2 07-06-2005 08:26 PM
Exception in thread "main" java.lang.NoClassDefFoundError: Lobais Linux - Software 19 06-23-2004 08:42 AM
Exception in thread "main" java.lang.NoClassDefFoundError: melinda_sayang Programming 2 04-27-2004 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration