Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-19-2011, 12:43 AM   PM User | #1
worldtraveller
Regular Coder

 
Join Date: Nov 2010
Location: canada
Posts: 131
Thanks: 51
Thanked 0 Times in 0 Posts
worldtraveller is an unknown quantity at this point
program not work in command line but works in eclipse

Good evening all I have some posted code here for a fraction program in java.

Problem is it works only in Eclipse but i am having a hard time to run in it in Command line.

Here are the 2 parts of the programs

one is called Fraction and second one is Tester

Here is the code for Fraction
Code:
package fractions;
public class Fraction
{

	// Constructor: sets numerator and denominator
	public Fraction(int num, int denom) {

		this.numerator=num;
		this.denominator=denom;
	}


	// Methods
	///////


	// Returns object plus frac1
	public Fraction add(Fraction frac1) {

		int num=this.getNum();    
		int denom=this.getDenom(); 
		
		// Check that we have the same denominator for both fractions
		if (frac1.getDenom()!=denom)	
		{
			int _denom=denom;      
			num*=frac1.getDenom(); 
			denom*=frac1.getDenom();
			frac1=frac1.mult(new Fraction(_denom,_denom)); 
		}

		num+=frac1.getNum();
		return new Fraction(num,denom);
		
	}


	// Returns object minus frac1
	public Fraction minus(Fraction frac1) {
		int num=this.getNum();
		int denom=this.getDenom();
		
		// Check that we have the same denominator for both fractions
		if (frac1.getDenom()!=denom)	
		{
			int _denom=denom;
			num*=frac1.getDenom();
			denom*=frac1.getDenom();
			frac1=frac1.mult(new Fraction(_denom,_denom));
		}

		num-=frac1.getNum();
		return new Fraction(num,denom);
	}


	// Returns object times frac1
	public Fraction mult(Fraction frac1) { 
		
		int num=this.getNum();
		int denom=this.getDenom();
		
		num*=frac1.getNum();
		denom*=frac1.getDenom();
		
		return new Fraction(num,denom);
	}


	// Returns object divided by frac1
	public Fraction div(Fraction frac1) { 
		
		int num=this.getNum();
		int denom=this.getDenom();
		
		num*=frac1.getDenom();
		denom*=frac1.getNum();
		
		return new Fraction(num,denom);
	}





	// Returns numerator of frac1
	public int getNum() { return this.numerator; }


	// Returns denominator of frac1
	public int getDenom() { return this.denominator; }



	// Data members
	private int numerator;
	private int denominator;
}
Ok here is the code for Tester
Code:
package fractions;
public class Tester 
{

	public static void main(String[] args) {
		Fraction frac1=new Fraction(1,3);
		Fraction frac2=new Fraction(2,5);

		Fraction fracAdd=frac2.add(frac1);
		Fraction fracMinus=frac2.minus(frac1);
		Fraction fracMult=frac2.mult(frac1);
		Fraction fracDiv=frac2.div(frac1);
		System.out.println(toString(frac2)+" + "+toString(frac1)+" = "+toString(fracAdd));
		System.out.println(toString(frac2)+" - "+toString(frac1)+" = "+toString(fracMinus));
		System.out.println(toString(frac2)+" * "+toString(frac1)+" = "+toString(fracMult));
		System.out.println(toString(frac2)+" / "+toString(frac1)+" = "+toString(fracDiv));
	}
	
	
	public static String toString(Fraction frac) {
		
		return frac.getNum()+"/"+frac.getDenom();
	}

}
So that is that. it somewhat works in Eclipse but not in command line. So i am confused as to what it does not work work there. These are the error messages I get in command line on it

For when i got Javac and java Fraction this message i get for it in command line
Code:
Exception in thread "main" java.lang.NoClassDefFoundError: Fraction (wrong name: fractions/Fraction)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
When i javac Tester this is what i get as message
Code:
Tester.java:20: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
	public static String toString(Fraction frac) {
	                              ^
Tester.java:6: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction frac1=new Fraction(1,3);
		^
Tester.java:6: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction frac1=new Fraction(1,3);
		                   ^
Tester.java:7: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction frac2=new Fraction(2,5);
		^
Tester.java:7: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction frac2=new Fraction(2,5);
		                   ^
Tester.java:9: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction fracAdd=frac2.add(frac1);
		^
Tester.java:10: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction fracMinus=frac2.minus(frac1);
		^
Tester.java:11: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction fracMult=frac2.mult(frac1);
		^
Tester.java:12: cannot find symbol
symbol  : class Fraction
location: class fractions.Tester
		Fraction fracDiv=frac2.div(frac1);
		^
9 errors
So thank you all for taking the time to go over this. It is whacking my mind that it works in one spot but not that other, feel free to run this yourself to see what i mean.
Thanks all. and thanks for all feedback.
worldtraveller is offline   Reply With Quote
Old 02-19-2011, 04:39 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Don't javac Tester unless Fraction has also been compiled. You can compile multiple with javac /path/to/fractions/*.java or by creating an argfile.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
worldtraveller (02-19-2011)
Old 02-19-2011, 03:48 PM   PM User | #3
worldtraveller
Regular Coder

 
Join Date: Nov 2010
Location: canada
Posts: 131
Thanks: 51
Thanked 0 Times in 0 Posts
worldtraveller is an unknown quantity at this point
Regarding the fractions program as first part of TME 4
i am able to compile it in JGRASP but its not working as run in the program. Has anyone got same problem, when i try to run in JGRASP i get error " no main methods or applets found in file"

I have a test file to go with this. So now i am confused. keep in mind it does compile though

So that is what is going on with it. I can not see where i am going wrong on this. I hope someone else can see the problem?
worldtraveller is offline   Reply With Quote
Old 02-19-2011, 04:19 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Have you added your fractions package to the location of your javac as well?
You're not clear as to what the problem is here. First you say it doesn't compile, then you say it does compile. I have no idea what jgrasp is, is it a framework, or an IDE or what? If its a framework and its not working, they likely have an interface you need to obey in order to use it with the framework.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 02-19-2011, 08:40 PM   PM User | #5
worldtraveller
Regular Coder

 
Join Date: Nov 2010
Location: canada
Posts: 131
Thanks: 51
Thanked 0 Times in 0 Posts
worldtraveller is an unknown quantity at this point
Well i was wrong it does compile but not able to run the program. It does compile in JGRASP
but not able to run, JGRASP is like Eclipse

I am not sure what i am doing wrong

so if anyone can can u take my code and run it and see what u can come up with as i am out of options now, not working, and frustrated thanks
worldtraveller is offline   Reply With Quote
Old 08-03-2011, 01:55 AM   PM User | #6
nbecker
New to the CF scene

 
Join Date: Dec 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
nbecker is an unknown quantity at this point
I realize that this is kind of an old thread, but I did a quick search because I'm having the same problem. I have three classes located in the same directory, they all compile and run fine from Eclipse, but when I try to run the program from terminal, I get almost the exact same error messages that worldtraveller was getting. Not sure what I'm doing wrong, as I can compile them all at once, but I cannot run the main program.

If this issue was resolved, I'd love to hear how exactly. Maybe I'm missing something. I'm kind of a newbie at both programming and this forum.
nbecker is offline   Reply With Quote
Reply

Bookmarks

Tags
eclipse, fractions, java

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 PM.


Advertisement
Log in to turn off these ads.