Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6

02192011, 12:43 AM #1
 Join Date
 Nov 2010
 Location
 canada
 Posts
 131
 Thanks
 51
 Thanked 0 Times in 0 Posts
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; }
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(); } }
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)
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
Thanks all. and thanks for all feedback.

02192011, 04:39 AM #2
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 17,027
 Thanks
 4
 Thanked 2,668 Times in 2,637 Posts
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');

Users who have thanked FouLu for this post:
worldtraveller (02192011)

02192011, 03:48 PM #3
 Join Date
 Nov 2010
 Location
 canada
 Posts
 131
 Thanks
 51
 Thanked 0 Times in 0 Posts
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?

02192011, 04:19 PM #4
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 17,027
 Thanks
 4
 Thanked 2,668 Times in 2,637 Posts
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');

02192011, 08:40 PM #5
 Join Date
 Nov 2010
 Location
 canada
 Posts
 131
 Thanks
 51
 Thanked 0 Times in 0 Posts
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

08032011, 01:55 AM #6
 Join Date
 Dec 2010
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
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.